Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Date {
- static const string fileBD;
- static string getNameMonth(int month) {
- if (month < 1)
- throw "A month cannot be less than 1";
- if (month > 12)
- throw "A month cannot be more than 12";
- string m[12] = {"january","february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
- return m[month - 1];
- }
- int year, month, day;
- int maxDayInMonth(int month) {
- switch (month)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- return 31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- return 30;
- break;
- case 2:
- if (year % 4 == 0)
- return 29;
- else
- return 28;
- break;
- }
- return 0;
- }
- public:
- Date() {
- day = 1;
- month = 1;
- year = 1900;
- }
- void setDay(int i) {
- if (i < 1)
- throw "Day cannot be less than 1";
- if (maxDayInMonth(month) < i)
- throw "A day cannot be more than this month";
- day = i;
- }
- void setMonth(int i ) {
- if (i < 1)
- throw "A month cannot be less than 1";
- if (i > 12)
- throw "A month cannot be more than 12";
- month = i;
- if (day > maxDayInMonth(i))
- setDay(maxDayInMonth(i));
- }
- void setYear(int i) {
- if (i < 1900)
- throw "A year cannot be less than 1900";
- year = i;
- }
- void addInBD() {
- ofstream bd(fileBD, ios::app);
- bd << day << " " << month << " " << year << endl;
- bd.close();
- }
- void print() { print(false); }
- void print(bool a) {
- if (a) {
- cout << day << " " << getNameMonth(month) << " " << year << endl;
- } else {
- if (day < 10) cout << "0";
- cout << day << ".";
- if (month < 10) cout << "0";
- cout << month << "." << year << endl;
- }
- }
- static void printAllDates() {
- ifstream bd(fileBD);
- Date a; int i, l = 0;
- while (bd >> i) {
- a.setDay(i);
- bd >> i; a.setMonth(i);
- bd >> i; a.setYear(i);
- a.print();
- l++;
- }
- if (l == 0)
- cout << "The database is empty" << endl;
- }
- static void printAllSpringDates() {
- ifstream bd(fileBD);
- Date a; int i, n, l = 0;
- while (bd >> i) {
- a.setDay(i);
- bd >> n; a.setMonth(n);
- bd >> i; a.setYear(i);
- if (n >= 3 && n <= 5) {
- a.print();
- l++;
- }
- }
- if (l == 0)
- cout << "There are no spring dates" << endl;
- }
- static void printAllDatesWithStartLetter(char s) {
- ifstream bd(fileBD);
- Date a; int i, n, l = 0;
- while (bd >> i) {
- a.setDay(i);
- bd >> n; a.setMonth(n);
- bd >> i; a.setYear(i);
- if (getNameMonth(n)[0] == s) {
- a.print(true);
- l++;
- }
- }
- if (l == 0)
- cout << "No dates starting with letter " << s << endl;
- }
- };
- const string Date::fileBD = "bd.txt";
- void addNewDate();
- void showAllDates();
- void showAllSpringDates();
- void showAllDatesWithStartLetter();
- int main()
- {
- system("cls");
- int m;
- cout << "Menu:" << endl
- << "1 - Add new date" << endl
- << "2 - Show all dates" << endl
- << "3 - Show all spring dates" << endl
- << "4 - Show all dates whose month begins with the specified letter" << endl
- << "0/5 - Exit" << endl;
- cin >> m;
- switch (m)
- {
- case 1: addNewDate(); break;
- case 2: showAllDates(); break;
- case 3: showAllSpringDates(); break;
- case 4: showAllDatesWithStartLetter(); break;
- case 0: case 5: system("cls"); break;
- default: main(); break;
- }
- }
- void addNewDate() {
- int i;
- Date a;
- system("cls");
- cout << "Add new date" << endl
- << "0 - Exit in menu" << endl
- << "Enter day: ";
- try {
- cin >> i;
- if (i == 0) return (void)main();
- a.setDay(i);
- cout << "Enter the month number: ";
- cin >> i;
- if (i == 0) return (void)main();
- a.setMonth(i);
- cout << "Enter year: ";
- cin >> i;
- if (i == 0) return (void)main();
- a.setYear(i);
- } catch (const char* e) {
- cerr << e << endl;
- system("pause");
- return addNewDate();
- }
- a.addInBD();
- cout << "Date added to database" << endl;
- system("pause");
- addNewDate();
- }
- void showAllDates() {
- system("cls");
- cout << "Show all dates" << endl;
- Date::printAllDates();
- system("pause");
- main();
- }
- void showAllSpringDates() {
- system("cls");
- cout << "Show all spring dates" << endl;
- Date::printAllSpringDates();
- system("pause");
- main();
- }
- void showAllDatesWithStartLetter() {
- char a;
- system("cls");
- cout << "Show all dates whose month begins with the specified letter" << endl
- << "0 - Exit in Menu" << endl
- << "Enter a letter: ";
- cin >> a;
- if (a == '0') return (void)main();
- Date::printAllDatesWithStartLetter(a);
- system("pause");
- showAllDatesWithStartLetter();
- }
Add Comment
Please, Sign In to add comment