Repository navigation
Expand file tree
/
Copy pathDatabase.cpp
More file actions
414 lines (350 loc) · 10.2 KB
/
Copy pathDatabase.cpp
File metadata and controls
414 lines (350 loc) · 10.2 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
a.
Cristiano Firmani
Yiftach Nachman
b.
2272725 (Cristiano)
2270838 (Yiftach)
c.
firma103
nachm100
d.
CSPC-350 Section 2
e.
Assignment 2
*/
#include <iostream>
#include "Database.h"
#include <fstream>
using namespace std;
Database::Database()
{
}
Database::~Database()
{
}
void Database::printStudents()
{
masterStudent.InOrder(masterStudent.getRoot());
}
void Database::printFaculty()
{
masterFaculty.InOrder(masterFaculty.getRoot());
}
void Database::findStudent(int ID)
{
if(masterStudent.find(ID)==1)
masterStudent.returnData(ID).printAll(); //Finds the student, uses a function to show all his data.
}
void Database::findFaculty(int ID)
{
if(masterFaculty.find(ID)==1)
masterFaculty.returnData(ID).printAll();
}
void Database::findStudentAdv(int ID)
{
if(masterStudent.find(ID)==1)
{
int AdvisorID=masterStudent.returnData(ID).getAdvisor();
masterFaculty.returnData(AdvisorID).printAll();
}
}
void Database::printAdvStudents(int ID)
{
if(masterFaculty.find(ID)==1)
masterFaculty.returnData(ID).printStudents();
else
cout<<"Advisor not found."<<endl;
}
void Database::addStudent(string M, double gpa, int A, int id, string name, string level)
{
cout<<"1"<<endl;
Student *tempStudent=new Student(id, name, level, M, gpa, A);
cout<<"2"<<endl;
masterStudent.insert(id, *tempStudent);
cout<<"3"<<endl;
if(masterFaculty.find(A)==1)
masterFaculty.returnData(A).addStudent(*tempStudent);
else
cout<<"Advisor not found."<<endl;
}
void Database::removeStudent(int studentID)
{
Student tempStudent=masterStudent.returnData(studentID);
int advisorID=tempStudent.getAdvisor();
masterFaculty.returnData(advisorID).removeStudent(tempStudent);
masterStudent.deleteNode(studentID);
}
void Database::addAdvisor(string D, int id, string name, string level)
{
Faculty *tempAdvisor= new Faculty(id, name, level, D);
masterFaculty.insert(id, *tempAdvisor);
}
void Database::removeAdvisor(int advisorID, int newAdvisorID)
{
Faculty oldAdvisor=masterFaculty.returnData(advisorID); //Finds the necessary advisors and then moves any of the students from the advisor being deleted
Faculty newAdvisor=masterFaculty.returnData(newAdvisorID);
oldAdvisor.convertStudents(newAdvisor);
}
void Database::changeStudentAdv(int studentID, int advisorID)
{
if(masterStudent.find(studentID)==1)
{
int currentAdvisorID=masterStudent.returnData(studentID).getAdvisor();
Student tempStudent=masterStudent.returnData(studentID);
Faculty oldAdvisor=masterFaculty.returnData(currentAdvisorID);
Faculty newAdvisor=masterFaculty.returnData(advisorID);
if(tempStudent.getAdvisor()!=oldAdvisor.getID())
cout<<"That student is not an advisee to the given advisor. " <<endl;
else
{
newAdvisor.addStudent(tempStudent);
oldAdvisor.removeStudent(tempStudent);
}
}
else
cout<<"Student not found."<<endl;
}
void Database::removeAdvStudent(int advisorID, int studentID)
{
if(masterStudent.find(studentID)==1)
{
Student tempStudent=masterStudent.returnData(studentID);
Faculty tempAdvisor=masterFaculty.returnData(advisorID);
if(tempStudent.getAdvisor()!=advisorID)
cout<<"That student is not an advisee to the given advisor. " <<endl;
else
{
tempAdvisor.removeStudent(tempStudent);
}
}
}
void Database::rollback()
{
if(options.isEmpty()==1)
{
cout<<"Rollback is not possible"<<endl;
}
else if(options.pop()==7 || options.pop()==8)
{
BinaryTree<Student> *tempStudent=studentRollback.pop();
masterStudent=*tempStudent;
}
else if(options.pop()==9 || options.pop()==10)
{
BinaryTree<Faculty> *tempFaculty=facultyRollback.pop();
masterFaculty=*tempFaculty;
}
}
void Database::fixOptionStack(genstack<int> options)
{
if(options.isFull()==1)
{
int tempInt=options.pop(); //Takes the full stack of previously selected options and frees up the oldest one
int tempInt1=options.pop(); //leaving room for the next option.
int tempInt2=options.pop();
int tempInt3=options.pop();
int tempInt4=options.pop();
options.push(tempInt3);
options.push(tempInt2);
options.push(tempInt1);
options.push(tempInt);
}
}
void Database::mainLoop()
{
int option;
while(option!=14)
{
if(ifstream("facultyTable.txt") && ifstream("studentTable.txt"))
{
cout<<"Which option would you like to complete: "<<endl;
cout<< "1. Print all students and their information "<<endl;
cout<< "2. Print all faculty and their information " <<endl;
cout<< "3. Find and display student information given the students id " <<endl;
cout<< "4. Find and display faculty information given the faculty id " <<endl;
cout<< "5. Given a student’s id, print the name and info of their faculty advisor " <<endl;
cout<< "6. Given a faculty id, print ALL the names and info of his/her advisees " <<endl;
cout<< "7. Add a new student " <<endl;
cout<< "8. Delete a student given the id " <<endl;
cout<< "9. Add a new faculty member " <<endl;
cout<< "10. Delete a faculty member given the id " <<endl;
cout<< "11. Change a student’s advisor given the student id and the new faculty id " <<endl;
cout<< "12. Remove an advisee from a faculty member given the ids " <<endl;
cout<< "13. Rollback " <<endl;
cout<< "14. Exit " <<endl;
cout<< "\n Simply type the number you would like to complete. ('1' or '2 or '3' etc.)" <<endl;
cin>> option;
if(option == 1)
{
printStudents();
}
else if(option == 2)
{
fixOptionStack(options);
options.push(2);
printFaculty();
}
else if(option == 3)
{
int studentID;
cout<< "Please input the ID of the student you would like to find: " <<endl;
cin>>studentID;
findStudent(studentID);
}
else if(option == 4)
{
int advisorID;
cout<< "Please input the ID of the advisor you would like to find: "<<endl;
cin>>advisorID;
findFaculty(advisorID);
}
else if(option == 5)
{
int studentID;
cout<<"Please input the ID of the student that you would like to find the advisor to." <<endl;
cin>>studentID;
findStudentAdv(studentID);
}
else if(option == 6)
{
int advisorID;
cout<<"Please input the ID of the advisor that you would like to print the students of. " <<endl;
cin>>advisorID;
printAdvStudents(advisorID);
}
else if(option == 7 || option == 8)
{
studentPrev4=studentPrev3;
studentPrev3=studentPrev2;
studentPrev2=studentPrev1;
studentPrev1=studentPrev;
studentPrev=&masterStudent;
studentRollback.empty();
studentRollback.push(studentPrev4);
studentRollback.push(studentPrev3);
studentRollback.push(studentPrev2);
studentRollback.push(studentPrev1);
studentRollback.push(studentPrev);
if(option==7)
{
fixOptionStack(options);
options.push(7);
int studentID;
string studentName;
string studentLevel;
string studentMajor;
double studentGPA;
int advisorID;
cout<<"Please input the ID of the student"<<endl;
cin>>studentID;
cout<<"Please input the name of the student"<<endl;
cin>>studentName;
cout<<"Please input the level (grade) of the student"<<endl;
cin>>studentLevel;
cout<<"Please input the student's major"<<endl;
cin>>studentMajor;
cout<<"Please input the student's GPA"<<endl;
cin>>studentGPA;
cout<<"Please input the ID of the student's advisor"<<endl;
cin>>advisorID;
addStudent(studentMajor, studentGPA, advisorID, studentID, studentName, studentLevel);
}
else if(option==8)
{
fixOptionStack(options);
options.push(8);
int studentID;
cout<<"Please input the ID of the student you would like to remove."<<endl;
cin>>studentID;
removeStudent(studentID);
}
}
else if(option == 9 || option==10)
{
facultyPrev4=facultyPrev3;
facultyPrev3=facultyPrev2;
facultyPrev2=facultyPrev1;
facultyPrev1=facultyPrev;
facultyPrev=&masterFaculty;
studentRollback.empty();
facultyRollback.push(facultyPrev4);
facultyRollback.push(facultyPrev3);
facultyRollback.push(facultyPrev2);
facultyRollback.push(facultyPrev1);
facultyRollback.push(facultyPrev);
if(option==9)
{
fixOptionStack(options);
options.push(9);
int advisorID;
string advisorName;
string advisorLevel;
string advisorDepartment;
cout<<"Please input the ID of the advisor"<<endl;
cin>>advisorID;
cout<<"Please input the name of the advisor"<<endl;
cin>>advisorName;
cout<<"Please input the level (lecturer, assistant prof, associate prof, etc) of the advisor"<<endl;
cin>>advisorLevel;
cout<<"Please input the advisor's department"<<endl;
cin>>advisorDepartment;
addAdvisor(advisorDepartment, advisorID, advisorName, advisorLevel);
}
else if(option==10)
{
fixOptionStack(options);
options.push(10);
int advisorID;
int newAdvisorID;
cout<<"Please input the ID of the advisor you would like to remove."<<endl;
cin>>advisorID;
cout<<"Please input the ID of the advisor you would like to move the students to."<<endl;
cin>>newAdvisorID;
removeAdvisor(advisorID, newAdvisorID);
}
}
else if(option == 11)
{
int studentID;
int newAdvisorID;
cout<<"Please input the ID of the student you would like to change the advisor of"<<endl;
cin>>studentID;
cout<<"Please input the ID of the new advisor you would like to move the student to"<<endl;
cin>>newAdvisorID;
changeStudentAdv(studentID, newAdvisorID);
}
else if(option == 12)
{
int advisorID;
int studentID;
cout<<"Please input the ID of the Advisor you would like to remove a student from"<<endl;
cin>>advisorID;
cout<<"Please input the ID of the student you would like to remove"<<endl;
cin>>studentID;
removeAdvStudent(advisorID, studentID);
}
else if(option == 13)
{
rollback();
}
else if(option == 14)
{
break;
}
else
{
cout<< "Invalid entry" << endl;
}
}
else
{
ofstream outfile ("FacultyTable.txt");
outfile << "Faculty Table" << endl;
outfile.close();
ofstream outifle("StudentTable.txt");
outfile<< "Student Table"<< endl;
outfile.close();
}
}
}