forked from Adriano-7/feup-aed-project-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassSchedule.cpp
169 lines (151 loc) · 5.78 KB
/
ClassSchedule.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
#include "ClassSchedule.h"
#include <iostream>
#include <algorithm>
using namespace std;
/**@brief Default Constructor, calls the UcClass default constructor and initializes the slots vector as empty
* @details Time complexity: O(1)
*/
ClassSchedule::ClassSchedule() {
this->ucClass = UcClass();
this->slots = vector<Slot>();
}
/**@brief Constructor, sets ucClass to the given one. The vector of slots is initialized as empty
* @details Time complexity: O(1)
* @param UcClass the UcClass of the schedule
*/
ClassSchedule::ClassSchedule(const UcClass &ucClass) {
this->ucClass = ucClass;
this->slots = vector<Slot>();
}
/**@brief Constructor, given a ucId and a classId, creates a UcClass and the vector of slots is initialized as empty
* @details Time complexity: O(1)
* @param ucId
* @param classId
*/
ClassSchedule::ClassSchedule(const string &ucId, const string &classId) {
this->ucClass = UcClass(ucId, classId);
this->slots = vector<Slot>();
}
/** @brief Add a slot to the vector of slots
* @details Time complexity: O(1)
* @param slot the slot to be added
*/
void ClassSchedule::addSlot(const Slot &slot) {
slots.push_back(slot);
}
/** @brief Inserts a student in the set of students
* @details Time complexity: O(log q), where q is the number of students in the ClassSchedules set of students
* @param student the student to add
*/
void ClassSchedule::addStudent(const Student &student) {
students.insert(student);
}
/** @brief Removes a student from the set of students
* @details Time complexity: O(log q), where q is the number of students in the ClassSchedules set of students
* @param student the student to remove
*/
void ClassSchedule::removeStudent(const Student &student) {
students.erase(student);
}
/** @brief Boolean function that returns true if the ClassSchedules have the same UcId, false otherwise
* @details Time complexity: O(1)
* @param other the other ClassSchedule
*/
bool ClassSchedule::sameUcId(const ClassSchedule &other) const {
return ucClass.sameUcId(other.getUcClass());
}
/**@brief Prints the ucId and classId
* @details Time complexity: O(1)
*/
void ClassSchedule::printHeader() const {
cout << ">> UC:" << ucClass.getUcId() << " " << ucClass.getClassId() << endl;
}
/**@brief Prints each slot (Weekday, Start time, End time, Type)
* @details Time complexity: O(l), where l is the number of slots in the ClassSchedule
*/
void ClassSchedule::printSlots() const {
cout << ">> Slots:" << endl;
for (const Slot &slot : slots) {
cout << " " << slot.getWeekDay() << " " << slot.getStartTime() << " - " << slot.getEndTime() << " " << slot.getType() << endl;
}
}
/**@brief Prints the students in a given sortType
* @details Prints the number of students, then the students in the given sortType\n
* Time complexity: O(q log q), where q is the number of students in the ClassSchedule
* @param sortType the type of sort, it can be alphabetical, reverse alphabetical, numerical, reverse numerical
*/
void ClassSchedule::printStudents(const string &sortType) const{
auto studentsVector = new vector<Student>(students.begin(), students.end()); //O(q)
if (sortType == "alphabetical") {
sort(studentsVector->begin(), studentsVector->end(), [](const Student &a, const Student &b) { return a.getName() < b.getName(); }); //O(q logQ)
} else if (sortType == "reverse alphabetical") {
sort(studentsVector->rbegin(), studentsVector->rend(), [](const Student &a, const Student &b) { return a.getName() < b.getName(); }); //O(q logQ)
} else if (sortType == "numerical") {
sort(studentsVector->begin(), studentsVector->end());
} else if (sortType == "reverse numerical") {
sort(studentsVector->rbegin(), studentsVector->rend());
} else {
cout << "Invalid sortType" << endl;
return;
}
cout << ">> Number of students: " << students.size() << endl;
cout << ">> Students:" << endl;
for(const Student &student: *studentsVector){ //O(q)
cout << " "; student.printHeader();
}
delete studentsVector;
}
/**@brief Prints the ClassSchedule (calls printHeader(), printSlots() and printStudents())
* @details Time complexity: O(l) + O(q log q), where l is the number of slots in the ClassSchedule and q is the number of students in the ClassSchedule
* @see printHeader()
* @see printSlots()
* @see printStudents()
*/
void ClassSchedule::print() const {
printHeader(); //O(1)
printSlots(); //O(l)
printStudents(); //O(q log q)
cout << endl;
}
/**@brief Returns the UcClass of the ClassSchedule
* @details Time complexity: O(1)
*/
UcClass ClassSchedule::getUcClass() const {
return ucClass;
}
/** @brief Returns the number of students enrolled in the class
* @details Time complexity: O(1)
* @return the number of students
*/
int ClassSchedule::getNumStudents() const {
return students.size();
}
/**
* @brief Returns a reference to the vector of slots
* @details Time complexity: O(1)
*/
const vector<Slot> &ClassSchedule::getSlots() const {
return slots;
}
/**@brief Returns the set of students
* @details Time complexity: O(1)
*/
set<Student> ClassSchedule::getStudents() const {
return students;
}
/** @brief Minor operator. Compares two ClassSchedules by their UcId, then by their classId
* @details Time complexity: O(1)
* @param other The ClassSchedule to compare to
* @return true if the UcClass is smaller, false otherwise
*/
bool ClassSchedule::operator < (const ClassSchedule &other) const {
return this->ucClass < other.getUcClass();
}
/** @brief Equality operator
* @details Time complexity: O(1)
* @param other The ClassSchedule to compare to
* @return true if the UcClass is equal, false otherwise
*/
bool ClassSchedule::operator == (const ClassSchedule &other) const {
return this->ucClass == other.getUcClass();
}