-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cpp
More file actions
213 lines (182 loc) · 7.71 KB
/
Copy pathDatabase.cpp
File metadata and controls
213 lines (182 loc) · 7.71 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
/*
Derek Norman
2364922
norman@chapman.edu
CPSC-350-03
Assignment 6
Using two late days for this assignment!
*/
/*
* Database class, simple representation of a database
*/
#include "Database.h"
Database::Database(){ //constructor
masterStudent = new BST<Student*>();
masterFaculty = new BST<Faculty*>();
}
Database::~Database(){ //destructor
delete masterStudent;
delete masterFaculty;
}
/*
* method addStudent, adds a student to the student table
* Takes single parameter student, representing the student going to be added
*/
void Database::addStudent(Student *student){
if(student->getAdvisorID() == 0){ //insert student without advisor incase no advisors are in table yet
masterStudent->insert(student);
} else if(findFaculty(student->getAdvisorID()) == nullptr){ //if the faculty advisor does not exist
cout << "please try inserting student with ID of advisor who exists!" << endl; //cout error
} else { //faculty advisor exists
masterStudent->insert(student); //add the student
if(student->getAdvisorID() != 0){ //this loop makes sure to add the students ID to the list of advisees for their advisor
Faculty *f = findFaculty(student->getAdvisorID());
f->getAdviseeList()->insertFront(student->getID()); //add student ID to faculy advisee list
}
}
}
/*
* method addFaculty, adds a faculty member to the faculty table
* Takes single parameter faculty, representing the faculty member going to be added
*/
void Database::addFaculty(Faculty *faculty){
masterFaculty->insert(faculty); //adds faculty member to faculty table
}
/*
* method deleteStudent, deletes a student from the student table
* Takes a single parameter id, of type int representing the id of the student to be deleted
*/
void Database::deleteStudent(int id){
Student *studentToDelete = new Student(id, "", "", "", 0.0, 0); //copy of student they want to delete
masterStudent->deleteNode(studentToDelete); //deletes the student if found
delete studentToDelete;
}
/*
* method deleteFaculty, deleted a faculty member from the faculty table
* Takes a single parameter id, of type int representing the id of the faculty to be deleted
*/
void Database::deleteFaculty(int id){
Faculty *facultyToDelete = new Faculty(id, "", "", "", {});
masterFaculty->deleteNode(facultyToDelete); //delete faculty if they are found
delete facultyToDelete;
}
/*
* method findStudent, finds a student given the student id
* Takes single parameter, id, of type int representing the id of the student
* Returns Student* representing the student that was found or null if no student was found
*/
Student* Database::findStudent(int id){
Student *studentToFind = new Student(id, "", "", "", 0.0, 0);
studentToFind = masterStudent->contains(studentToFind); //see if the student to find exists
if(studentToFind != NULL){ //student exists
return studentToFind; //return student
} else { //student does not exist
cout << "No student found with that ID!" << endl;
return nullptr; //return null
}
}
/*
* method findFaculty, finds a faculty member given the faculty id
* Takes single parameter, id, of type int representing the id of the faculty member
* Returns Faculty* representing the faculty that was found or null if no faculty was found
*/
Faculty* Database::findFaculty(int id){ //find a faculty member
Faculty *facultyToFind = new Faculty(id, "", "", "", {});
facultyToFind = masterFaculty->contains(facultyToFind); //see if the faculty member exists
if(facultyToFind != NULL){ //if they exist
return facultyToFind; //return the faculty member
} else { //if they do not exist
cout << "No faculty found with that ID!" << endl;
return nullptr; //return null
}
}
/*
* method printStudentDatabase, prints a visual representation of the student table
*/
void Database::printStudentDatabase(){
masterStudent->printNodesInorder();
}
/*
* method printFacultyDatabase, prints a visual representation of the faculty table
*/
void Database::printFacultyDatabase(){
masterFaculty->printNodesInorder();
}
/*
* method printFacultyAdvisor, prints the advisor of a student given the student id
* Takes single parameter studentID, of type int representing the id of the student
*/
void Database::printFacultyAdvisor(int studentID){
Student *student = findStudent(studentID); //find student
Faculty *faculty = findFaculty(student->getAdvisorID()); //find advisor
if(student == nullptr){ //if student does not exist
cout << "No Student with ID " << studentID << " found" << endl;
return; //return nothing
}
if (faculty == nullptr) { //if faculty advisor does not exist
cout << "Faculty advisor does not exist!" << endl;
cout << "Please consider changing students advisor" << endl;
} else { //else if student and faculty advisor exist then print the faculty advisor
faculty->Print();
}
}
/*
* method printAdviseesOfFaculty, prints the list of advisees for a faculty member given the faculty id
* Takes single parameter facultyID, of type int representing the faculty members id
*/
void Database::printAdviseesOfFaculty(int facultyID){
Faculty *faculty = findFaculty(facultyID); //find faculty from id
if(faculty == nullptr){ //if faculty does not exist
cout << "Faculty advisor does not exis please try again!" << endl;
} else { // if faculty does exist, then print the list of advisees
cout << "Advisees of " << faculty->getFacultyName() << ": " << endl;
SingleLinkedList<int>* temp = faculty->getAdviseeList();
ListNode<int>* curr = temp->getFront();
while (curr != NULL){
Student *s = findStudent(curr->data);
if(s == nullptr){ //if the faculty members advisee does not exist in student table
cout << "Student with ID '" << curr->data << "' not found." << endl;
curr = curr->next;
} else { //if student in advisee list doest exist
s->Print(); //print the student information
curr = curr->next;
}
}
}
}
/*
* method changeStudentsAdvisor, changes the students advisor given the student ID and the new advisor id
* Takes two parameters, studentID representing the id of the student, facultyID representing the id of the new faculty member to be used as advisor.
*/
void Database::changeStudentsAdvisor(int studentID, int facultyID){
Student *student = findStudent(studentID);
if(student == nullptr){
cout << "Student with that ID does not exist..." << endl;
} else {
Faculty *faculty = findFaculty(facultyID);
if(faculty == nullptr){
cout << "Cannot change students advisor ID because a Faculty member with " << facultyID << " does not exist" << endl;
} else {
student->setAdvisorID(facultyID);
faculty->getAdviseeList()->insertFront(studentID);
}
}
}
/*
* method removeAdvisee, removes an advisee from a faculty advisee id list given the faculty id and student id
* Takes two parameters, facultyID representing the id of the faculty member, studentID representing the id to remove from the faculty members advisee id list.
*/
void Database::removeAdvisee(int facultyID, int studentID){
Faculty *faculty = findFaculty(facultyID);
if(faculty == nullptr){
cout << "Faculty with that ID does not exist..." << endl; ///
} else {
Student *student = findStudent(studentID);
if(student == nullptr){
cout << "Cannot remove advisee because the student does not exist in database" << endl;
} else {
faculty->getAdviseeList()->removeNode(studentID);
student->setAdvisorID(0);
}
}
}