-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
283 lines (269 loc) · 7.72 KB
/
Menu.cpp
File metadata and controls
283 lines (269 loc) · 7.72 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// Created by seraf on 28.03.2021.
//
#include "Menu.h"
#include "Bill.h"
#include "Birthday.h"
#include <exception>
#include <cmath>
class MyException: public std::exception{
std::string message;
public:
explicit MyException(const std::string& message): message(message){}
virtual const char* what() const noexcept override{
return message.c_str();
}
};
Menu::Menu(std::vector <std::unique_ptr <Reminder>> R, std::vector<ShoppingList> S, std::vector<ToDoList> T, std::vector<Alarm> A): rList(std::move(R)), sList(S), tdList(T), aList(A) {}
void Menu::listAlarms(){
int n = aList.size();
for(int i = 0; i<n; ++i){
std::cout << aList[i];
}
menuAlarms();
}
void Menu::listReminders(){
int n = rList.size();
for(int i = 0; i<n; ++i){
rList[i] -> print(std::cout);
}
menuReminders();
}
void Menu::menuAlarms(){
std::cout << "1. List alarms\n" <<"2. Add alarm\n" << "3. Delete alarm\n" << "4. Back to main menu\n";
int n, hour, min, i;
bool recurrent;
std::string in;
std::cin >> n;
switch(n){
case 1:
listAlarms();
break;
case 2:
std::cout <<"\nTime in format HH MM: ";
std::cin >> hour >> min;
std::cout <<"\nMessage: ";
std::cin >> in;
std::cout<<"\nRecurrent alarm?(1 for yes, 0 for no): ";
std::cin >> recurrent;
aList.push_back(Alarm( hour,min, in, recurrent));
menuAlarms();
break;
case 3:
//listAlarms();
std::cout << "\nEnter index: ";
std::cin >> i;
aList.erase(aList.begin() + i - 1);
menuAlarms();
break;
default:
menu();
}
}
std::unique_ptr<Reminder> addReminder(){
int choice;
double value, age;
DateTime time;
std::string name, in;
std::cout <<"\nReminder type: 1 for usual reminder, 2 for bill, 3 for birthday: ";
std::cin >> choice;
if(choice < 1 || choice > 3)
throw MyException("Invalid choice in addReminder!");
bool recurrent;
if(choice == 3){
std::cout << "\nName: ";
std::cin >> name;
std::cout << "\nAge: ";
std::cin >> age;
if(age < 0)
throw MyException("Age cannot be negative!");
if(floor(age) != age)
throw MyException("Age must be a whole number!");
}
if(choice == 2){
std::cout << "\nName of company: ";
std::cin >> name;
std::cout << "\nValue of bill: ";
std::cin >> value;
if(value < 0)
throw MyException("Value cannot be negative!");
}
std::cout << "\nTime in format DD MM YYYY HH MM: ";
std::cin >> time;
std::cout << "\nMessage: ";
std::cin >> in;
std::cout << "\nImportant reminder?(1 for yes, 0 for no): ";
std::cin >> recurrent;
switch(choice){
case 1:
return std::make_unique<Reminder>(in, time, recurrent);
break;
case 2:
return std::make_unique<Bill>(value, name, in, time, recurrent);
break;
case 3:
return std::make_unique<Birthday>(name, age, in, time, recurrent);
break;
default:
throw MyException("Invalid choice in addReminder!"); // primeam warning fara throw-ul asta, e redundant aici oricum
break;
}
}
void Menu::menuReminders(){
std::cout << "1. List reminders\n" <<"2. Add reminder\n" << "3. Delete reminder\n" << "4. Back to main menu\n";
int n, i, choice;
DateTime time;
std::string in;
std::cin >> n;
switch(n){
case 1:
listReminders();
break;
case 2:
try{
rList.push_back(addReminder());
}
catch(MyException& ex){
std::cout << ex.what() <<"\n";
}
menuReminders();
break;
case 3:
std::cout << "\nEnter index: ";
std::cin >> i;
std::cout << "\nPostpone reminder? (10 minutes for reminders, 1 month for bills, 1 year for birthdays) (1 for yes, 0 for no): ";
std::cin >> choice;
if(choice == 0)
rList.erase(rList.begin() + i - 1);
else
rList[i] -> delay();
menuReminders();
break;
default:
menu();
}
}
void Menu::listSLists(){
int n = sList.size();
for(int i = 0; i<n; ++i){
sList[i].printList(std::cout);
}
menuSLists();
}
void Menu::menuSLists(){
std::cout << "1. Print shopping lists\n" <<"2. Add shopping list\n" << "3. Delete shopping list\n" << "4. Back to main menu\n";
int n, i;
DateTime time;
std::string in;
std::cin >> n;
switch(n){
case 1:
listSLists();
break;
case 2: {
std::cout << "\nList name: ";
std::cin >> in;
std::cout << "\nNo of items: ";
std::cin >> i;
ShoppingList aux(in);
std::cout << "\nEnter items: ";
for (int j = 0; j < i; ++j) {
std::cin >> in;
aux.addItem(in);
}
sList.push_back(aux);
menuSLists();
break;
}
case 3:
//listSLists();
std::cout << "\nEnter index: ";
std::cin >> i;
sList.erase(sList.begin() + i - 1);
menuSLists();
break;
default:
menu();
}
}
void Menu::listTDLists(){
int n = tdList.size();
for(int i = 0; i<n; ++i)
std::cout <<i+1<<". " << tdList[i];
menuTDLists();
}
void Menu::menuTDLists(){
std::cout << "1. Print ToDos\n" <<"2. Add ToDo\n" << "3. Delete ToDo\n" <<"4. Mark task completed\n"<<"5. Print completed tasks\n"<< "6. Back to main menu\n";
int n, i, j;
DateTime time;
std::string in;
std::cin >> n;
switch(n){
case 1:
listTDLists();
break;
case 2: {
std::cout << "\nToDo name: ";
std::cin >> in;
std::cout << "\nNo of tasks: ";
std::cin >> i;
ToDoList aux(in);
std::cout << "\nEnter tasks: ";
for (j = 0; j < i; ++j) {
std::cout << "\nTask name: ";
std::cin >> in;
std::cout << "\nDeadline in format DD MM YYYY HH MM: ";
std::cin >> time;
aux.addTask(in, time);
}
tdList.push_back(aux);
menuTDLists();
break;
}
case 3:
//listTDLists();
std::cout << "Enter index: ";
std::cin >> i;
tdList.erase(tdList.begin() + i - 1);
menuTDLists();
break;
case 4:
//listTDLists();
std::cout << "Which list?";
std::cin >> i;
std::cout << "\nWhich index?";
std::cin >> j;
tdList[i-1].completedTask(j);
menuTDLists();
break;
case 5:
std::cout <<"\nFrom which list?";
std::cin >> i;
tdList[i-1].printCompleted(std::cout);
menuTDLists();
break;
default:
menu();
}
}
void Menu::menu(){
std::cout << "1. Alarms\n" << "2. Reminders\n" << "3. Shopping lists\n" << "4. ToDo lists\n" << "5. Exit\n";
int n;
std::cin >> n;
switch(n){
case 1:
menuAlarms();
break;
case 2:
menuReminders();
break;
case 3:
menuSLists();
break;
case 4:
menuTDLists();
break;
default:
return ;
}
}