-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistrationSystem.cpp
More file actions
213 lines (189 loc) · 5.14 KB
/
RegistrationSystem.cpp
File metadata and controls
213 lines (189 loc) · 5.14 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
#include <iostream>
#include "RegistrationSystem.h"
using namespace std;
RegistrationSystem::RegistrationSystem()
{
students = NULL;
size = 0;
noOfStudents = 0;
}
RegistrationSystem::~RegistrationSystem()
{
if (students)
delete[] students;
}
void RegistrationSystem::addStudent(const int studentId, const string firstName, const string lastName)
{
for (int i = 0; i < noOfStudents; i++) {
if ((students[i].getStudentId() == studentId)) {
cout << "Student " << studentId << " already exists." << endl;
return;
}
}
Student studentToAdd(firstName, lastName, studentId);
Student* temp = new Student[noOfStudents + 1];
for (int i = 0; i < noOfStudents; i++) {
temp[i] = students[i];
}
delete[] students;
students = new Student[noOfStudents + 1];
for (int i = 0; i < noOfStudents; i++) {
students[i] = temp[i];
}
students[size] = studentToAdd;
delete[] temp;
noOfStudents++;
size++;
sortStudents(students, size);
cout << "Student " << studentToAdd.getStudentId() << " has been added." <<endl;
}
void RegistrationSystem::deleteStudent(const int studentId)
{
int indexOfStudentToRemove = -1;
for (int i = 0; i < noOfStudents; i++) {
if (students[i].getStudentId() == studentId) {
indexOfStudentToRemove = i;
}
}
if (indexOfStudentToRemove == -1) {
cout << "Student " << studentId << " doesn't exist" << endl;
return;
}
Student* temp = new Student[noOfStudents - 1];
for (int i = 0; i < indexOfStudentToRemove; i++) {
temp[i] = students[i];
}
for (int i = indexOfStudentToRemove + 1; i < noOfStudents; i++) {
temp[i - 1] = students[i];
}
delete[] students;
students = new Student[noOfStudents - 1];
for (int i = 0; i < noOfStudents - 1; i++) {
students[i] = temp[i];
}
delete[] temp;
noOfStudents--;
size--;
cout << "Student " << studentId << " has been deleted" << endl;
sortStudents(students, size);
}
void RegistrationSystem::sortStudents(Student*& students, const int size)
{
if (size <= 0)
return;
int i, j, min;
Student temp;
for (i = 0; i < size - 1; i++) {
min = i;
for (j = i + 1; j < size; j++)
if (students[j].getStudentId() < students[min].getStudentId())
min = j;
temp = students[i];
students[i] = students[min];
students[min] = temp;
}
}
void RegistrationSystem::addCourse(const int studentId, const int courseId, const string courseName)
{
int indexOfStudent = -1;
for (int i = 0; i < noOfStudents; i++) {
if ((students[i].getStudentId() == studentId)) {
indexOfStudent = i; }
}
if (indexOfStudent == -1) {
return;
}
if ((students[indexOfStudent]).addCourse(courseId, courseName)) {
cout << "Course " << courseId << " has been added to student " << studentId << endl;
return;
}
else {
return;
}
}
void RegistrationSystem::withdrawCourse(const int studentId, const int courseId)
{
int indexOfStudent = -1;
// Find the student to delete from
for (int i = 0; i < noOfStudents; i++) {
if ((students[i].getStudentId() == studentId)) {
indexOfStudent = i;
i = noOfStudents + 1;
}
}
if (indexOfStudent == -1) {
cout << "Student doesnt exist" << endl;
return;
}
//Find course in the student
int numCourses = students[indexOfStudent].getNoOfCourses();
bool courseExists = false;
for (int i = 0; i < numCourses; i++) {
if (students[indexOfStudent].getCourses()[i].getCourseId() == courseId) {
courseExists = true;
i = numCourses + 1;
}
}
if (!courseExists) {
cout << "The course" << courseId << " does not exist" << endl;
return;
}
//bool withdrawed;
for (int i = 0; i < noOfStudents; ++i) {
if (students[indexOfStudent].withdrawCourse(courseId)) {
cout << "The course " << courseId << " has been withdrawn" << endl;
}
}
return;
}
void RegistrationSystem::cancelCourse(const int courseId)
{
bool cancelled = false;
int count = 0;
for (int i = 0; i < noOfStudents; i++) {
cancelled = students[i].withdrawCourse(courseId);
if (cancelled) {
count++;
}
}
if (count == 0) {
cout << "The Course " << courseId << " does not exist" << endl;
return;
}
if (count != 0) {
cout << "The Course " << courseId << " has been cancelled" << endl;
}
}
void RegistrationSystem::showStudent(const int studentId)
{
bool shown = false;
for (int i = 0; i < size; i++) {
if (students[i].getStudentId() == studentId) {
cout << students[i];
shown = true;
}
}
if (!shown) {
cout << "There exists no Student " << studentId << endl;
}
}
void RegistrationSystem::showCourse(const int courseId)
{
bool shown = false;
cout << "CourseID " << courseId << "\n";
for (unsigned int i = 0; i < noOfStudents; i++) {
if (students[i].getCourses()[i].getCourseId() == courseId) {
cout <<"\t" << "ID: " << students[i].getStudentId() << "\tName: "<< students[i].getFirstName() << " " << students[i].getLastName() << endl;
shown = true;
}
}
if (!shown) {
cout << "There exists no Course " << courseId << endl;
}
}
void RegistrationSystem::showAllStudents()
{
for (unsigned int i = 0; i < noOfStudents; i++) {
cout << this->students[i];
}
}