-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calendar.cpp
302 lines (235 loc) · 7.27 KB
/
Calendar.cpp
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Calendar.cpp
* Author: Bahati Boniface
*
* Created on April 25, 2017, 4:18 PM
*/
#include "Calendar.h"
#include <ctime>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Date.h"
#include "Event.h"
#include <conio.h>
Calendar::Calendar() {
}
void Calendar::Run() {
bool run = true;
/*****************************/
/* Menu system */
/********************************/
while (run) {
char op;
string file;
cout
<< "\nN: Show Next Month"
<< "\tP: Show Previous Month " << "\tI: Show Specific Date "
<< "\nA: Create An Event" << "\tL: List All Events"
<< "\tD: Delete An Event" << "\nS: Save All Events"
<< "\tR: Load Event" << "\tQ: Quit" << "\nInput your command ";
cin >> op;
if (op >= 'a' && op <= 'z')
op += ('A' - 'a');
switch (op) {
case 'N':
NextMonth();
Draw();
break;
case 'P':
PreviousMonth();
Draw();
break;
case 'I':
InputDate();
Draw();
break;
case 'A':
CreateEvent();
break;
case 'L':
ListEvents();
break;
case 'D':
DeleteEvent();
break;
case 'S':
Save();
break;
case 'R':
Load();
break;
case 'Q':
run = false;
break;
default:
cout << "Invalid input." << endl;
break;
}
}
}
void Calendar::Draw() {
int year = currentDate.getYear();
int month = currentDate.getMonth();
cout << " " << Date::MonthNames[month - 1];
cout << " " << year << endl;
cout << "**************************" << endl;
//Print all the startingDay days Mon-Sun
for (int i = 0; i < sizeof Date::DayNames / sizeof Date::DayNames[0]; ++i) {
cout << Date::DayNames[i] << " ";
}
cout << endl;
/**********************************************/
/* Calculate the starting day for each month */
/*********************************************/
// Tomohiko Sakamoto's Algorithm
int y = year - (month < 3);
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
int startingDay;
int d = 1;
//Nasty trick to solve febuary starting day
if (month == 2) {
d = 0;
}
startingDay = ((year + year / 4 - year / 100 + year / 400 + t[month - 1] + d) % 7);
cout << startingDay << endl;
int dayCount = Date::MonthDays[month - 1];
cout << dayCount << endl;
/**********************************************/
/* Printing the calendar */
/*********************************************/
//Print and empty space if the day doesn't have a corresponding day
for (int x = 0; x < startingDay; ++x) {
cout << " ";
}
//Print the date corresponding with the day
for (int d = 0; d < dayCount; ++d) {
if (d < 9)
cout << "0" << d + 1 << " ";
else
cout << d + 1 << " ";
++startingDay;
if (startingDay == 7) {
startingDay = 0;
cout << endl;
}
}
cout << endl;
}
//Display next month
void Calendar::NextMonth() {
currentDate.IncreaseMonth();
}
//Display previous month
void Calendar::PreviousMonth() {
currentDate.DecreaseMonth();
}
/********************************************************/
/* Print a calendar coressponding to the user input */
/********************************************************/
// Get date from user
void Calendar::InputDate() {
int year, month, day;
do {
cout << "Year:";
cin >>year;
} while (year < 0);
do {
cout << "Month:";
cin>>month;
} while (1 > month || month > 12);
do {
cout << "day:";
cin>>day;
} while (1 > day || day > 31);
currentDate.setDate(day, month, year);
;
}
/***************************************************/
/* Get a date from a user and store as an event */
/***************************************************/
void Calendar::CreateEvent() {
Event event;
int year, month, day;
cout << "Input Year:";
cin >> year;
cout << "Input Month:";
cin >> month;
cout << "Input day:";
cin >> day;
event.date.setDate(year, month, day);
string desc;
cout << "Input description: ";
cin >> event.description;
events.push_back(event);
}
void Calendar::ListEvents() {
for (int i = 0; i < events.size(); ++i) {
cout << setw(4) << i + 1 << " " << events[i].ToString() << endl;
}
}
/***************************************************************/
/* Delete and event corresponding to the user selected index */
/***************************************************************/
void Calendar::DeleteEvent() {
int id;
cout << "Input event ID: ";
cin >> id;
if (id < events.size() + 1) {
events.erase(events.begin() + id - 1);
} else {
cerr << "Invalid event ID" << endl;
}
}
/*****************************************************/
/* Create and event file and store all the events */
/*****************************************************/
void Calendar::Save() {
//string filename = ;
ofstream file;
file.open("db.event", ios::app);
for (int i = 0; i < events.size(); ++i) {
file << events[i].date.getYear()
<< events[i].date.getMonth()
<< events[i].date.getDay()
<< events[i].description << endl;
}
file.close();
cout << "Save to file success." << endl;
}
/**********************************************/
/* Load all event the the event file */
/*********************************************/
void Calendar::Load() {
events.clear();
ifstream file;
file.open("db.event", ios::app);
file.seekg(0, ios::end); // put the "cursor" at the end of the file
int length = file.tellg(); // find the position of the cursor
if (!file.is_open()) {
cerr << "Unable to open the file: " << endl;
cerr << "Abort loading events." << endl;
return;
}
while (true) {
Event event;
int year, month, day;
file >> year >> month>> day;
if (file.eof())
break;
event.date.setDate(year, month, day);
string desc;
file >> event.description;
events.push_back(event);
}
file.close();
cout << "Load from file success." << endl;
if (length == 0) {
cout << "The are no event currently stored\n";
}
}