Timetabler
slot.cpp
1 #include "fields/slot.h"
2 
3 #include <iostream>
4 #include <string>
5 #include <vector>
6 #include "fields/is_minor.h"
7 #include "global.h"
8 
15 Time::Time(unsigned hours, unsigned minutes) {
16  this->hours = hours;
17  this->minutes = minutes;
18 }
19 
26 Time::Time(std::string time) {
27  this->hours = std::stoul(time.substr(0, 2));
28  this->minutes = std::stoul(time.substr(3, 5));
29 }
30 
38 Time &Time::operator=(const Time &other) {
39  this->hours = other.hours;
40  this->minutes = other.minutes;
41  return *this;
42 }
43 
52 bool Time::operator==(const Time &other) {
53  return (this->hours == other.hours) && (this->minutes == other.minutes);
54 }
55 
64 bool Time::operator<(const Time &other) {
65  if (this->hours == other.hours) {
66  return this->minutes < other.minutes;
67  } else {
68  return this->hours < other.hours;
69  }
70 }
71 
80 bool Time::operator<=(const Time &other) {
81  return (*this < other) || (*this == other);
82 }
83 
92 bool Time::operator>=(const Time &other) { return !(*this < other); }
93 
102 bool Time::operator>(const Time &other) { return !(*this <= other); }
103 
111 std::string Time::getTimeString() {
112  return std::to_string(hours) + ":" + std::to_string(minutes);
113 }
114 
124  if (hours >= 0 && hours < 13) {
125  return true;
126  }
127  return false;
128 }
129 
137 SlotElement::SlotElement(Time &startTime, Time &endTime, Day day)
138  : startTime(startTime), endTime(endTime) {
139  this->day = day;
140 }
141 
153  if (this->day != other.day) {
154  return false;
155  }
156  if (this->startTime < other.startTime) {
157  return !(this->endTime <= other.startTime);
158  } else if (this->startTime > other.startTime) {
159  return !(this->startTime >= other.endTime);
160  }
161  return true;
162 }
163 
171 bool SlotElement::isMorningSlotElement() { return startTime.isMorningTime(); }
172 
180 Slot::Slot(std::string name, IsMinor isMinor,
181  std::vector<SlotElement> slotElements)
182  : isMinor(isMinor) {
183  this->name = name;
184  this->slotElements = slotElements;
185 }
186 
195 bool Slot::operator==(const Slot &other) { return (this->name == other.name); }
196 
208  for (unsigned i = 0; i < slotElements.size(); i++) {
209  for (unsigned j = 0; j < other.slotElements.size(); j++) {
210  if (slotElements[i].isIntersecting(other.slotElements[j])) {
211  return true;
212  }
213  }
214  }
215  return false;
216 }
217 
224  slotElements.push_back(slotElement);
225 }
226 
232 FieldType Slot::getType() { return FieldType::slot; }
233 
240  return isMinor.getMinorType() == MinorType::isMinorCourse;
241 }
242 
248 std::string Slot::getTypeName() { return "Slot"; }
249 
255 std::string Slot::getName() { return name; }
256 
266  for (unsigned i = 0; i < slotElements.size(); i++) {
267  if (!slotElements[i].isMorningSlotElement()) {
268  return false;
269  }
270  }
271  return true;
272 }
bool operator==(const Slot &other)
Checks if two Slot objects are identical, which is if they have the same name.
Definition: slot.cpp:195
std::string getTypeName()
Gets the type name, which is "Slot".
Definition: slot.cpp:248
MinorType getMinorType()
Gets the minor type of the Course.
Definition: is_minor.cpp:38
std::string getName()
Gets the name of the Slot.
Definition: slot.cpp:255
bool operator<=(const Time &)
Checks if a Time is before or identical to another.
Definition: slot.cpp:80
bool isMorningTime()
Determines if the Time is a morning time.
Definition: slot.cpp:123
bool isIntersecting(Slot &other)
Determines if two Slots are intersecting.
Definition: slot.cpp:207
Class for "is minor".
Definition: is_minor.h:32
Class for a slot element.
Definition: slot.h:59
SlotElement(Time &, Time &, Day)
Constructs the SlotElement object.
Definition: slot.cpp:137
Class for a time unit.
Definition: slot.h:30
void addSlotElements(SlotElement)
Adds a slot element to the Slot.
Definition: slot.cpp:223
bool operator>=(const Time &)
Checks if a Time is after or identical to another.
Definition: slot.cpp:92
bool operator<(const Time &)
Checks if a Time is strictly before another.
Definition: slot.cpp:64
bool isMinorSlot()
Determines if the Slot is a minor Slot.
Definition: slot.cpp:239
Day
Enum Class to represent a day of the week.
Definition: slot.h:15
bool isMorningSlotElement()
Determines if the SlotElement is a morning slot element.
Definition: slot.cpp:171
FieldType
Enum that represents all the field types.
Definition: global.h:9
bool isIntersecting(SlotElement &other)
Determines if two slot elements are intersecting.
Definition: slot.cpp:152
bool isMorningSlot()
Determines if the Slot is a morning Slot.
Definition: slot.cpp:265
std::string getTimeString()
Gets the time as a string.
Definition: slot.cpp:111
FieldType getType()
Gets the type under the FieldType enum.
Definition: slot.cpp:232
Class for a slot.
Definition: slot.h:82
Slot(std::string, IsMinor, std::vector< SlotElement >)
Constructs the Slot object.
Definition: slot.cpp:180
bool operator==(const Time &)
Checks if two Time objects are identical, which is whether they represent the same hours and minutes...
Definition: slot.cpp:52
Time(unsigned, unsigned)
Constructs the Time object.
Definition: slot.cpp:15
bool operator>(const Time &)
Checks if a Time is strictly after another.
Definition: slot.cpp:102
Time & operator=(const Time &)
Assigns a time object to this.
Definition: slot.cpp:38