-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
221 lines (202 loc) · 3.92 KB
/
Copy pathmain.cpp
File metadata and controls
221 lines (202 loc) · 3.92 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <vector>
#include <sstream>
#include <set>
#include <map>
#include <tuple>
#include <iomanip>
class Date
{
public:
Date(const int year, const int month, const int day) : m_Year(year), m_Month(month), m_Day(day)
{
if (m_Month < 1 || m_Month > 12)
{
throw std::out_of_range("Month value is invalid: " + std::to_string(m_Month));
}
if (m_Day < 1 || m_Day > 31)
{
throw std::out_of_range("Day value is invalid: "+ std::to_string(m_Day));
}
}
int GetYear() const noexcept
{
return m_Year;
}
int GetMonth() const noexcept
{
return m_Month;
}
int GetDay() const noexcept
{
return m_Day;
}
private:
int m_Year;
int m_Month;
int m_Day;
};
Date parse_date(const std::string& date_str)
{
std::istringstream is(date_str);
int year, month, day;
is >> year;
if (is.peek() != '-')
{
throw std::logic_error("Wrong date format: " + date_str);
}
else
{
is.ignore(1);
}
is >> month;
if (is.peek() != '-')
{
throw std::logic_error("Wrong date format: " + date_str);
}
else
{
is.ignore(1);
}
if (!(is >> day && is.eof()))
{
throw std::logic_error("Wrong date format: " + date_str);
}
return Date(year, month, day);
}
bool operator<(const Date& lhs, const Date& rhs) noexcept
{
return std::make_tuple(lhs.GetYear(), lhs.GetMonth(), lhs.GetDay()) <
std::make_tuple(rhs.GetYear(), rhs.GetMonth(), rhs.GetDay());
}
std::ostream& operator<<(std::ostream& ostream, const Date& date)
{
ostream << (date.GetYear() < 0 ? "-" : "") << std::setw(4) << std::setfill('0') <<
std::abs(date.GetYear()) << "-" << std::setw(2) << std::setfill('0') <<
date.GetMonth() << "-" << std::setw(2) << std::setfill('0') <<
date.GetDay();
return ostream;
}
class Database
{
public:
void AddEvent(const Date& date, const std::string& event) noexcept
{
m_map[date].insert(event);
}
bool DeleteEvent(const Date& date, const std::string& event) noexcept
{
if (m_map[date].count(event))
{
m_map[date].erase(event);
return true;
}
return false;
}
int DeleteDate(const Date& date) noexcept
{
if (m_map.count(date))
{
size_t events_count = m_map[date].size();
m_map.erase(date);
return int(events_count);
}
return 0;
}
std::set<std::string> Find(const Date& date) const noexcept
{
if (m_map.count(date))
{
return m_map.at(date);
}
return std::set<std::string>();
}
void Print() const noexcept
{
for (const auto& e : m_map)
{
for (const auto& event : e.second)
{
std::cout << e.first << " " << event << std::endl;
}
}
}
private:
std::map<Date, std::set<std::string>> m_map;
};
void execut_command(const std::string& commandLine, Database& db)
{
std::istringstream is (commandLine);
std::string command;
is >> command;
if (command.empty())
{
return;
}
if (command == "Add")
{
std::string date_str, event;
is >> date_str >> event;
Date date = parse_date(date_str);
db.AddEvent(date, event);
}
else if (command == "Del")
{
std::string date_str;
is >> date_str;
if (is.eof())
{
Date date = parse_date(date_str);
std::cout << "Deleted " <<db.DeleteDate(date) << " events" << std::endl;
}
else
{
std::string event;
is >> event;
Date date = parse_date(date_str);
if (db.DeleteEvent(date, event))
{
std::cout << "Deleted successfully" << std::endl;
}
else
{
std::cout << "Event not found"<< std::endl;
}
}
}
else if (command == "Find")
{
std::string date_str;
is >> date_str;
Date date = parse_date(date_str);
for (const auto& row : db.Find(date))
{
std::cout << row << std::endl;
}
}
else if (command =="Print")
{
db.Print();
} else
{
throw std::runtime_error("Unknown command: " + command);
}
}
int main()
{
Database db;
std::string commandLine;
while (std::getline(std::cin, commandLine))
{
try
{
execut_command(commandLine, db);
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
return 0;
}
}
return 0;
}