-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudents.cpp
228 lines (188 loc) · 6.24 KB
/
students.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
//Define the name of the file that will be used to save and load student data in
const string STUDENT_FILENAME = "students-database.txt";
//Define the Student Class
class Student {
private:
//Members
long studentId;
string name;
double GPA;
public:
//Constructor
Student(long id, string name, double GPA) {
this->studentId = id;
this->name = name;
this->GPA = GPA;
}
//Getters
long getId() {
return this->studentId;
}
string getName() {
return this->name;
}
double getGPA() {
return this->GPA;
}
};
//Overload the << Operator to print the Student Class
ostream& operator<<(ostream &strm, Student &s);
//COMMAND FUNCTIONS
void printRecords(vector<Student> students);
Student createStudent();
void deleteStudentById(vector<Student> &students, long id);
void saveFile(string filename, vector<Student> students);
void loadFile(string filename, vector<Student> &students);
void printHelpMenu();
int main(void) {
cout << "Welcome to the Student Manager\n\n";
printHelpMenu();
vector<Student> students;
string input;
while(1) {
//Obtain the command from the user
cout << "\n\nEnter a command: ";
cin >> input;
//--------------PRINT--------------
if (input.compare("print") == 0) {
printRecords(students);
//--------------CREATE--------------
} else if (input.compare("create") == 0) {
Student s = createStudent();
cout << s << " created successfully." << endl;
students.push_back(s);
//--------------DELETE--------------
} else if (input.compare("delete") == 0) {
long id;
cout << "Enter the student ID of the student to delete: ";
cin >> id;
deleteStudentById(students, id);
//--------------SAVE--------------
} else if (input.compare("save") == 0) {
saveFile(STUDENT_FILENAME, students);
//--------------LOAD--------------
} else if (input.compare("load") == 0) {
loadFile(STUDENT_FILENAME, students);
//--------------HELP--------------
} else if (input.compare("help") == 0) {
printHelpMenu();
//--------------EXIT--------------
} else if (input.compare("exit") == 0) {
cout << "Good bye!\n";
break;
} else {
//--------------INVALID COMMAND--------------
cout << "Unknown command. Type 'exit' to exit or 'help' for options.\n";
}
}
return 0;
}
//--------------PRINT--------------
void printRecords(vector<Student> students){
//Check if the number of students is 0, don't print anything if so
if (students.size() == 0) {
cout << "No records available" << endl;
return;
}
//If not 0 then print the existing students
for (int i = 0; i < students.size(); i++) {
cout << students.at(i) << endl;
}
}
//--------------CREATE--------------
Student createStudent() {
//Create variables that we will be using to collect information
long studentId;
string name;
double GPA;
//Collect the student information from the user
cout << "\nEnter Student ID: ";
cin >> studentId;
cout << "\nEnter Name: ";
cin >> name;
cout << "\nEnter GPA: ";
cin >> GPA;
//Create the student object with the collected information
Student newStudent = Student(studentId, name, GPA);
//Return the object
return newStudent;
}
//--------------DELETE--------------
void deleteStudentById(vector<Student> &students, long id){
//Iterate through all the students available
for (int i = 0; i < students.size(); i++) {
//Obtain the current student
Student currStudent = students.at(i);
//Check if current student has the matching id provided by the user
if (id == currStudent.getId()) {
//Found the correct id, delete the student from the vector
students.erase(students.begin() + i);
cout << currStudent << " deleted." << endl;
}
}
}
//--------------SAVE--------------
void saveFile(string filename, vector<Student> students){
//Create an output file stream
ofstream studentFile;
//Check if there are no students, if none then just exit - nothing to save
if(students.size() == 0){
cout << "No existing records to save!" << endl;
return;
}
//open the file for writing
studentFile.open(filename);
//Iterate through all the students in the vector
for (int i = 0; i < students.size(); i++){
//Obtain the current student
Student currStudent = students.at(i);
//Save the current student information to the file in the csv format: id,name,gpa
cout << "saving student.... " << currStudent << endl;
studentFile << currStudent.getId() << " " << currStudent.getName() << " " << currStudent.getGPA() << endl;
}
cout << "Saved to file " << filename << endl;
//make sure to close the output file stream
studentFile.close();
}
//--------------LOAD--------------
void loadFile(string filename, vector<Student> &students){
//create and open an input file stream
ifstream studentFile;
studentFile.open(filename);
cout << "Loading from file: " << filename << endl << endl;;
//Define strings to store respective fields of file.
string idString, name, GPAString;
//Read the file line by line
while(studentFile >> idString >> name >> GPAString){
long id = stol(idString);
double GPA = stod(GPAString);
//Create the student object from the information read above
Student newStudent = Student(id, name, GPA);
cout << "Loaded student..." << newStudent << endl;
//Update the students vector
students.push_back(newStudent);
}
//make sure you close the input file stream
studentFile.close();
}
//--------------HELP--------------
void printHelpMenu() {
cout << "Enter one of the following commands:\n";
cout << " print - Print all records.\n";
cout << " create - Create a new record.\n";
cout << " delete - Delete a record by ID.\n";
cout << " save - Save all records to file.\n";
cout << " load - Load all records from file.\n";
cout << " help - Reprint this menu.\n";
cout << " exit - Exit this program.\n\n";
}
//----<< overload------
ostream& operator<<(ostream &strm, Student &s) {
return strm << "Student(ID: " << s.getId() << ", Name: "
<< s.getName() << ", GPA: " << s.getGPA() << ")";
}