-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.h
96 lines (81 loc) · 2.7 KB
/
Task.h
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
#include <utility>
//
// Created by ryanz on 4/24/2022.
//
#ifndef SCHEDULER_TASK_H
#define SCHEDULER_TASK_H
#include "Time.h"
class Task {
public:
std::string task;
Time start;
Time end;
int profit;
Task(int h1, int m1, int h2, int m2, int p, std::string task = "None")
: start(h1, m1), end(h2, m2), profit(p), task(std::move(task)) {
// validate
if (p < 0) {
throw std::invalid_argument("invalid profit");
}
}
Task(Time start, Time end, int profit, std::string name = "None")
: start(start), end(end), profit(profit), task(std::move(name)) {
// validate
if (profit < 0) {
throw std::invalid_argument("invalid profit");
}
}
Task(const std::string& startStr, const std::string& endStr, int p, std::string task = "None")
: profit(p), task(std::move(task)) {
// Now parse the times:
try {
start = Time::parse(startStr);
end = Time::parse(endStr);
} catch (const std::invalid_argument& e) {
std::cerr << "Error parsing time: " << e.what() << std::endl;
throw; // Re-throw the exception.
}
// validate
if (p < 0) {
throw std::invalid_argument("invalid profit");
}
}
std::string toString() const {
std::stringstream ss;
if (task == "None") {
ss << "Task: " << start.toString() << " - " << end.toString() << " Profit: " << profit;
} else {
ss << "Task: " << task << " " << start.toString() << " - " << end.toString() << " Profit: " << profit;
}
return ss.str();
}
bool operator>(const Task& rhs) const {
if (profit == rhs.profit) {
return start.getHour() > rhs.start.getHour()
|| (start.getHour() == rhs.start.getHour() && start.getMinute() > rhs.start.getMinute());
}
return profit > rhs.profit;
}
bool operator<(const Task& rhs) const {
if (profit == rhs.profit) {
return start < rhs.start;
}
return profit < rhs.profit;
}
bool operator>=(const Task& rhs) const {
return !(*this < rhs);
}
bool operator<=(const Task& rhs) const {
return !(*this > rhs);
}
// overload the << operator
friend std::ostream& operator<<(std::ostream& os, const Task& t) {
os << t.toString();
return os;
}
};
// to sort tasks in increasing order by starting time
bool taskComparator(const Task& lhs, const Task& rhs) {
return lhs.end < rhs.end;
}
#endif //SCHEDULER_TASK_H