-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentDatabase.h
More file actions
85 lines (71 loc) · 2.16 KB
/
Copy pathStudentDatabase.h
File metadata and controls
85 lines (71 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef STUDENTDATABASE_H
#define STUDENTDATABASE_H
#include <set>
#include <fstream>
#include <StudentRecord.h>
const int SUCCESS = 0;
const int ERR_NOT_FOUND = 1;
const int ERR_OUT_OF_RANGE = 2;
const int ERR_DUPLICATE_ID = 3;
/**
* @brief The StudentDatabase class
*/
class StudentDatabase
{
public:
StudentDatabase(){
m_sys_id = 1;
}
StudentDatabase(std::string filePath){
m_sys_id = 1;
loadSaveFile(filePath);
}
std::set<StudentRecord, StudentRecordCompare> getDatabase() const {return m_records;}
const char* getYearString(Year year) const {return yearStrings[year];}
int addRecord(std::string,std::string,Year,Gender);
int addRecord(int,std::string,std::string,Year,Gender);
int deleteRecord(int);
int updateRecord(int,std::string,std::string,Year,Gender);
const StudentRecord* findRecord(int);
bool inRange(int);
int getCurrentId() const {return m_sys_id;}
void printStudentsInYear(Year);
void printAllRecords();
void printRecord(const StudentRecord*);
int saveAllRecords(std::string);
/**
* @brief loadSaveFile - Attempt to load a file given its path
* @param filePath
* @returns 0 if success, 1 if failure
*/
int loadSaveFile(std::string filePath)
{
// no file given
if(filePath.empty())
return ERR_NOT_FOUND; // failure
std::ifstream inFile(filePath);
// file given is not valid
if(!inFile)
{
return ERR_NOT_FOUND; // failure
}
// else load file
m_records.clear(); // in case of loading a new db from the user app
int id = 0;
std::string firstName, lastName;
int year;
int gender;
while (inFile.good())
{
inFile >> id; inFile >> firstName >> lastName >> year >> gender;
m_records.insert(StudentRecord(id,firstName,lastName,(Year)year,(Gender)gender));
m_sys_id = id + 1;
}
inFile.close();
return SUCCESS; // success
}
private:
std::set<StudentRecord, StudentRecordCompare> m_records;
int m_sys_id;
};
#endif // STUDENTDATABASE_H