-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRegistrationSystem.cpp
270 lines (242 loc) · 6.71 KB
/
RegistrationSystem.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
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
#include <iostream>
#include <string>
using namespace std;
#include "Student.h"
#include "RegistrationSystem.h"
RegistrationSystem::RegistrationSystem()
{
this->studentHead = NULL;
this->numStudents = 0;
}
RegistrationSystem::~RegistrationSystem()
{
StudentNode* cur = this->studentHead;
while( cur != NULL )
{
StudentNode* tmp = cur;
cur = cur->next;
delete tmp->self;
tmp->next = NULL;
tmp->prev = NULL;
tmp->self = NULL;
delete tmp;
}
}
void RegistrationSystem::addStudent( const int studentId, const string firstName, const string lastName )
{
if( this->getStudent(studentId) == NULL)
{
StudentNode* cur = studentHead;
//creating new student node and initiliazing its data with Student pointer
StudentNode* st = new StudentNode;
st->self = new Student(studentId, firstName, lastName);
// if the list is empty make it head
if( this->studentHead == NULL )
{
this->studentHead = st;
st->prev = NULL;
st->next = NULL;
}
else
{
//it exits from loop when the student is bigger or cur is the last item
while(cur->next != NULL && (cur->self->getStudentId() <= studentId) )
{
cur = cur->next;
}
//cur is the first student who has a bigger id than given or the last item
//if cur is gonna be placed on the first index
if ( cur == studentHead )
{
st->next = cur;
st->prev = NULL;
cur->prev = st;
this->studentHead = st;
}
//if the list is not empty, and the cur has no item next, and the item is not the head
else if ( cur->next == NULL)
{
cur->next = st;
st->prev = cur;
st->next = NULL;
}
else
{
st->prev = cur->prev;
st->next = cur;
cur->prev = st;
st->prev->next = st;
}
}
this->numStudents++;
cout << "Student " << st->self->getStudentId() << " has been added " << endl;
}
else
{
cout << "Student " << studentId << " already exists " << endl;
}
}
StudentNode* RegistrationSystem::getStudent( const int studentId)
{
StudentNode* cur = studentHead;
while( cur != NULL)
{
if(cur->self->getStudentId() == studentId)
{
break;
}
cur = cur->next;
}
return cur; //returns NULL if not found
}
void RegistrationSystem::deleteStudent( const int studentId )
{
StudentNode* st = this->getStudent(studentId);
//if not found
if( st == NULL )
{
cout << "Student " << studentId << " is not found " << endl;
}
//if it is found and it's head and it has an item next
else
{
if( st == studentHead && st->next != NULL)
{
st->next->prev = NULL;
studentHead = st->next;
st->next = NULL;
st->prev = NULL;
delete st->self;
st->self = NULL;
delete st;
}
// if it's the only item in the list
else if( st == studentHead && st->next == NULL)
{
delete st->self;
st->self = NULL;
delete st;
studentHead = NULL;
}
// if it's the last item on the list
else if( st->next == NULL )
{
st->prev->next = NULL;
st->prev = NULL;
delete st->self;
st->self = NULL;
delete st;
}
//if it's between two items
else
{
st->prev->next = st->next;
st->next->prev = st->prev;
st->next = NULL;
st->prev = NULL;
delete st->self;
st->self = NULL;
delete st;
}
this->numStudents--;
cout << "Student " << studentId << " is deleted" << endl;
}
}
void RegistrationSystem::addCourse( const int studentId, const int courseId, const string courseName )
{
StudentNode* st = this->getStudent(studentId);
if ( st == NULL)
{
cout << "Student " << studentId << " does not exist " << endl;
}
else
{
st->self->addCourse(courseId, courseName);
}
}
void RegistrationSystem::withdrawCourse( const int studentId, const int courseId )
{
StudentNode* st = this->getStudent(studentId);
if ( st == NULL)
{
cout << "Student " << studentId << " does not exist " << endl;
}
else
{
st->self->withdrawCourse(courseId);
}
}
void RegistrationSystem::cancelCourse( const int courseId )
{
bool isFound = false;
Course* tmpCourse;
for(StudentNode* cur = studentHead; cur != NULL; cur = cur->next)
{
tmpCourse = cur->self->getCourse(courseId);
if( tmpCourse != NULL )
{
isFound = true;
break;
}
}
if(isFound)
{
for(StudentNode* cur = studentHead; cur != NULL; cur = cur->next)
{
cur->self->withdrawCourse(courseId);
}
}
else
cout << "Course " << courseId << " doesn't exist" << endl;
}
void RegistrationSystem::showStudent( const int studentId )
{
StudentNode* st = this->getStudent(studentId);
if( st != NULL)
st->self->show();
else
cout << "Student " << studentId << " does not exist" << endl;
}
void RegistrationSystem::showCourse( const int courseId )
{
bool isFound = false;
Course* tmpCourse;
for(StudentNode* cur = studentHead; cur != NULL; cur = cur->next)
{
tmpCourse = cur->self->getCourse(courseId);
if( tmpCourse != NULL )
{
isFound = true;
break;
}
}
if(isFound)
{
cout << "Course id\tCourse name" << endl;
cout << tmpCourse->courseId << "\t" << tmpCourse->courseName << endl;
for(StudentNode* cur = studentHead; cur != NULL; cur = cur->next)
{
tmpCourse = cur->self->getCourse(courseId);
if( tmpCourse != NULL )
{
cout << "\t";
cur->self->showTabbedRepresentation();
}
}
}
else
cout << "Course " << courseId << " does not exist" << endl;
}
void RegistrationSystem::showAllStudents()
{
if( numStudents > 0)
{
cout << "Student id\tFirst Name\tLast Name" << endl;
for(StudentNode* cur = studentHead; cur != NULL; cur = cur->next)
{
cur->self->showTabbedRepresentation();
}
}
else
cout << "There is not any student" << endl;
}