-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudents.cpp
More file actions
executable file
·183 lines (148 loc) · 3.21 KB
/
Copy pathStudents.cpp
File metadata and controls
executable file
·183 lines (148 loc) · 3.21 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
#include "Students.h"
Student::Student(int StudentID, int Grade, int Power) : id(StudentID),
grade(Grade), power(Power)
{ }
int Student::GetID()
{
return this->id;
}
int Student::GetGrade()
{
return this->grade;
}
int Student::GetPower()
{
return this->power;
}
void Student::IncreasePower(int power_increase)
{
this->power += power_increase;
}
Team* Student::GetTeam()
{
return this->team;
}
void Student::SwitchTeam(MyTeam* new_team)
{
if (this->team != NULL) {
this->team->RemoveStudent(this);
}
new_team->AddStudent(this);
this->team = new_team;
}
Students::Students(): by_id(), by_power(), strongest_student(-1,-1)
{ }
bool Students::AddStudent(Student* student)
{
if (student == NULL) {
return false;
}
PowerIDPair pair(student->GetID(), student->GetPower());
if (!this->by_id.insert(student->GetID(), student)) {
return false;
}
// if the insertion to the first tree is successfull,
//this will be successfull as well
this->by_power.insert(pair, student);
if (pair > this->strongest_student) {
this->strongest_student = pair;
}
}
bool Students::RemoveStudent(int student_id)
{
if (student_id < 0) {
return false;
}
try {
Student * s = this->by_id.searchInTree(student_id);
return this->RemoveStudent(s);
}
catch (const DataNotFound&) {
return false;
}
}
bool Students::RemoveStudent(Student * student)
{
if (student == NULL) {
return false;
}
// check if the student exists
try {
(void)this->by_id.searchInTree(student->GetID());
}
catch (const DataNotFound&) {
return false;
}
// remove the student
PowerIDPair pair(student->GetID(), student->GetPower());
if (pair == this->strongest_student) {
// in case we remove the strongest student
Student * s = this->by_power.getPreviousInOrderSearchOfMax(
this->strongest_student);
this->strongest_student = PowerIDPair(s->GetID(), s->GetPower());
}
this->by_id.deleteKey(student->GetID());
this->by_power.deleteKey(pair);
return true;
}
Student* Students::GetAllStudents(int * length)
{
if (length == NULL) {
return NULL;
}
*length = this->by_id.getSize();
if (*length == 0) {
return NULL;
}
Student** all = new Student*[*length];
if (all == NULL) {
// report error
*length = -1;
return NULL;
}
this->by_id.getDataInOrder(all);
return *all;
}
//////////////////////////////////////////////////////
// TODO: change this code
//////////////////////////////////////////////////////
Student* Students::GetAllStudentsByPower(int * length)
{
if (length == NULL) {
return NULL;
}
*length = this->by_power.getSize();
if (*length == 0) {
return NULL;
}
Student** all = new Student*[*length];
if (all == NULL) {
// report error
*length = -1;
return NULL;
}
this->by_power.getDataInOrder(all);
return *all;
}
Student* Students::GetStudent(int student_id)
{
return this->by_id.searchInTree(student_id);
}
int Students::GetStrongestStudentID()
{
return this->strongest_student.GetID();
}
Team::Team(int id) : id(id), students()
{ }
bool Team::AddStudent(Student* s)
{
return this->students.AddStudent(s);
}
bool Team::RemoveStudent(Student* s)
{
return this->students.RemoveStudent(s);
}
int Team::GetStrongestStudentID()
{
return this->students.GetStrongestStudentID();
}