-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
207 lines (164 loc) · 6.21 KB
/
database.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "database.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#define PRINT(x) std::cout << x;
std::vector<CHNJAR003::Student> CHNJAR003::StudentRecords;
void CHNJAR003::addStudent(const std::string fName, const std::string sName, const std::string studentNumber, const std::string classRecord)
{
bool recordAlreadyExists = false;
//iterate through the existing records to check whether it exists already
for (auto &aRecord : CHNJAR003::StudentRecords)
{
std::string tempRecordStudentNumber = aRecord.studentNumber;
std::transform(tempRecordStudentNumber.begin(), tempRecordStudentNumber.end(), tempRecordStudentNumber.begin(), ::tolower);
std::string newRecordStudentNumber = studentNumber;
std::transform(newRecordStudentNumber.begin(), newRecordStudentNumber.end(), newRecordStudentNumber.begin(), ::tolower);
if (tempRecordStudentNumber.compare(newRecordStudentNumber) == 0) //if the student record already exists, replace it
{
recordAlreadyExists = true;
aRecord.fName = fName;
aRecord.sName = sName;
aRecord.classRecord = classRecord;
}
}
if (!recordAlreadyExists) //if the record does not exist, add it
{
CHNJAR003::Student tempRecord;
//entered details stored in temporary object to be added
tempRecord.fName = fName;
tempRecord.sName = sName;
tempRecord.studentNumber = studentNumber;
tempRecord.classRecord = classRecord;
//add the record
CHNJAR003::StudentRecords.push_back(tempRecord);
}
}
void CHNJAR003::readDatabase(const std::string fileName)
{
std::ifstream databaseFile;
databaseFile.open(fileName.c_str());
if (!databaseFile)
{ //If databaseFile could not be opened
std::cerr << "databaseFile: " << fileName << " could not be opened for reading.";
}
else
{
CHNJAR003::StudentRecords.clear();
while (!databaseFile.eof())
{
std::string line;
getline(databaseFile, line);
if (line.size() > 1) //line is not empty
{
CHNJAR003::StudentRecords.push_back(parseFileLine(line));
}
}
PRINT("All the records are:\n")
for (CHNJAR003::Student temp : CHNJAR003::StudentRecords)
{
PRINT("\nFirst Name:\t" + temp.fName +
"\nLast Name:\t" + temp.sName +
"\nStudent Number:\t" + temp.studentNumber +
"\nClass Record:\t" + temp.classRecord + "\n\n");
}
PRINT("\n")
}
databaseFile.close();
}
void CHNJAR003::saveDatabase(const std::string fileName)
{
/*for (CHNJAR003::Student temp : CHNJAR003::StudentRecords)
{
PRINT(temp.fName);
}*/
std::ofstream databaseFile;
databaseFile.open(fileName.c_str());
if (databaseFile.is_open())
{
for (CHNJAR003::Student temp : CHNJAR003::StudentRecords)
{
databaseFile << serialiseStudent(temp) << std::endl;
}
databaseFile.close();
}
}
std::string CHNJAR003::displayStudentData(const std::string studentNumber)
{
std::string studentNumberLowerCase = studentNumber;
std::transform(studentNumberLowerCase.begin(), studentNumberLowerCase.end(), studentNumberLowerCase.begin(), ::tolower);
for (CHNJAR003::Student temp : CHNJAR003::StudentRecords)
{
std::string tempStudentNoLowerCase = temp.studentNumber;
std::transform(tempStudentNoLowerCase.begin(), tempStudentNoLowerCase.end(), tempStudentNoLowerCase.begin(), ::tolower);
if (studentNumberLowerCase.compare(tempStudentNoLowerCase) == 0) //if the student numbers match
{
return "\nFirst Name:\t" + temp.fName +
"\nLast Name:\t" + temp.sName +
"\nStudent Number:\t" + temp.studentNumber +
"\nClass Record:\t" + temp.classRecord + "\n\n";
}
}
return "Student record could not be found.\n";
}
float CHNJAR003::gradeStudent(const std::string studentNumber)
{
std::string studentNumberLowerCase = studentNumber;
std::transform(studentNumberLowerCase.begin(), studentNumberLowerCase.end(), studentNumberLowerCase.begin(), ::tolower);
for (CHNJAR003::Student temp : CHNJAR003::StudentRecords)
{
std::string tempStudentNoLowerCase = temp.studentNumber;
std::transform(tempStudentNoLowerCase.begin(), tempStudentNoLowerCase.end(), tempStudentNoLowerCase.begin(), ::tolower);
if (studentNumberLowerCase.compare(tempStudentNoLowerCase) == 0) //if the student numbers match
{
std::istringstream iss(temp.classRecord);
std::string token;
std::vector<std::string> marks;
while (std::getline(iss, token, ' '))
{
marks.push_back(token);
}
float average;
for (std::string mark : marks)
{
average += atof(mark.c_str());
}
return average / marks.size();
}
}
return -1;
}
//Function to process a student record line from the textfile and resolve the record into its individual data parts
CHNJAR003::Student CHNJAR003::parseFileLine(const std::string line)
{
CHNJAR003::Student temp;
std::istringstream iss(line);
std::string token;
std::vector<std::string> tokens;
//Break up the line delimited by commas
while (std::getline(iss, token, ','))
{
tokens.push_back(token);
}
temp.fName = tokens[0];
temp.sName = tokens[1];
temp.studentNumber = tokens[2];
temp.classRecord = tokens[3];
/*for (int i = 0; i < 4; i++)
{
PRINT(std::to_string(i) + ":" + tokens[i] + "\n");
}
for (std::string temp : tokens)
{
PRINT(temp + '\n');
}*/
return temp;
}
//Function to process a Student record from its struct form into a string that can be stored in a textfile.
std::string CHNJAR003::serialiseStudent(const CHNJAR003::Student tempStudent)
{
std::string serialOut;
serialOut = tempStudent.fName + "," + tempStudent.sName + "," + tempStudent.studentNumber + "," + tempStudent.classRecord;
return serialOut;
}