This repository was archived by the owner on Aug 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalTask.cpp
More file actions
176 lines (156 loc) · 3.89 KB
/
FinalTask.cpp
File metadata and controls
176 lines (156 loc) · 3.89 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
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
class BadArgumentException : exception {
public:
explicit BadArgumentException(string const &msg) {message = msg;}
BadArgumentException() {message = "";}
virtual const char* what() const throw() {
return message.c_str();
}
private:
string message;
};
class Date {
public:
Date(int year, int month, int day) {
if (month < 1 || month > 12) {
throw BadArgumentException("Month value is invalid: " + to_string(month));
} else if (day < 1 || day > 31) {
throw BadArgumentException("Day value is invalid: " + to_string(day));
}
this->year = year;
this->month = month;
this->day = day;
}
int GetYear() const {return year;}
int GetMonth() const {return month;}
int GetDay() const {return day;}
private:
int year;
int month;
int day;
Date() {
this->year = 0;
this->month = 0;
this->day = 0;
}
};
bool operator<(const Date& lhs, const Date& rhs) {
return vector<int>{lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()} <
vector<int>{rhs.GetYear(), rhs.GetMonth(), rhs.GetDay()};
}
class Database {
public:
void AddEvent(const Date& date, const string& event) {
m[date].insert(event);
}
bool DeleteEvent(const Date& date, const string& event) {
auto it = m.find(date);
if (it != m.end()) {
auto itSet = it->second.find(event);
if (itSet != it->second.end()) {
it->second.erase(event);
return true;
}
}
return false;
}
int DeleteDate(const Date& date) {
auto it = m.find(date);
int n;
if (it != m.end()) {
n = it->second.size();
m.erase(it);
} else {
n = 0;
}
return n;
}
void Find(const Date& date) const {
auto it = m.find(date);
if (it != m.end())
PrintSet(it->second);
}
void PrintSet(set<string> const &s) const {
for (auto const &i : s) {
cout << i << endl;
}
}
void Print() const {
for (auto const &i : m) {
stringstream ss;
ss << setfill('0') << setw(4) << i.first.GetYear() << '-'
<< setw(2) << i.first.GetMonth() << '-' << setw(2) << i.first.GetDay();
string date = ss.str();
for (auto const &s: i.second) {
cout << date << " " << s << endl;
}
}
}
private:
map<Date, set<string> > m;
};
Date ParseDate(string const &date) {
istringstream input(date);
int year;
char dash1, dash2;
int month;
int day;
input >> year >> dash1 >> month >> dash2 >> day;
if (input.fail() || !input.eof() || (dash1 != '-' || dash2 != '-'))
throw BadArgumentException("Wrong date format: " + date);
return Date(year, month, day);
}
int main() {
Database db;
string command;
try {
while (getline(cin, command)) {
istringstream input(command);
if (input) {
string cmd;
input >> cmd;
if (cmd == "Add") {
string date, event;
input >> date >> event;
db.AddEvent(ParseDate(date), event);
} else if (cmd == "Del") {
string date;
input >> date;
if (!input.eof()) {
string event;
input >> event;
if (db.DeleteEvent(ParseDate(date), event)) {
cout << "Deleted successfully" << endl;
} else {
cout << "Event not found" << endl;
}
} else {
int n = db.DeleteDate(ParseDate(date));
cout << "Deleted " << n << " events" << endl;
}
} else if (cmd == "Find") {
string date;
input >> date;
db.Find(ParseDate(date));
} else if (cmd == "Print") {
db.Print();
} else if (cmd.empty()) {
continue ;
} else {
throw BadArgumentException("Unknown command: " + cmd);
}
}
}
} catch (BadArgumentException &ex) {
cout << ex.what() << endl;
return 1;
}
return 0;
}