// Author : Rex Forsyth // Date : May 15, 2006 // client program to test the Date class -- dateTest.cc #include #include #include #include using namespace std; //#include //using namespace std::rel_ops; #include "date.h" #include "myio.h" // program constant const int MAX = 10; // maximum number of dates allowed // menu function prototypes void printMenu(); int choice(); void doIt(int c, Date t[]); // utility function prototypes void getValid(Date& t); void getInRange(int& i); // prototypes for functions to manipulate the dates in the array void printAll(Date t[]); void read(Date t[]); void print(Date t[]); void wordPrint(Date t[]); void diffInYMD(Date t[]); void diffInDays(Date t[]); void compare(Date t[]); //**************************************** //**************************************** int main() { Date dateList[MAX]; cout << "Setting first date to today" << endl; dateList[0] = Date::today(); int what; printMenu(); cout << " Enter your choice -- "; do { what = choice(); doIt(what,dateList); if (what != 9) cout <<" What now ? "; } while (what != 9); // test the read with prompts method and print in month day, year format Date d1; d1.read(); cout << "The date read is "; d1.print(); cout << endl; return 0; } //**************************************** // function to print available options //**************************************** void printMenu() { cout << "You may choose one of the following options:" << endl; cout << endl; cout << " 1. (R)ead a new date. " << endl; cout << " 2. (P)rint a date in yyyy/mm/dd format." << endl; cout << " 3. (W)rite a date in mon dd, yyyy format." << endl; cout << " 4. (D)ifference in days, months, & years."<< endl; cout << " 5. (N)umber of days between." << endl; cout << " 6. Test (C)omparison methods." << endl; cout << " 7. (L)ist all dates." << endl; cout << " 8. (H)elp -- print this list. " << endl; cout << " 9. (Q)uit -- exit the program. " << endl; cout << endl; } //**************************************** // function to obtain the users choice //**************************************** int choice() { char ans; ans = cin.get(); if (ans != '\n') clrStream(); switch (toupper(ans)) { case '1': case 'R': return 1; case '2': case 'P': return 2; case '3': case 'W': return 3; case '4': case 'D': return 4; case '5': case 'N': return 5; case '6': case 'C': return 6; case '7': case 'L': return 7; case '8': case '?': case 'H': return 8; // Help or '?' case '9': case 'E': case 'Q': return 9;// Quit or Exit default : cout << "Invalid option entered!! "; return 8; // cause menu to be printed } } //**************************************** // function to do users choice //**************************************** void doIt(int c, Date t[]) { switch (c) { case 1 : read(t); break; case 2 : print(t); break; case 3 : wordPrint(t); break; case 4 : diffInYMD(t); break; case 5 : diffInDays(t); break; case 6 : compare(t); break; case 7 : printAll(t); break; case 8 : printMenu(); break; case 9 : break; // do nothing } } //**************************************** // function to obtain a valid date //**************************************** void getValid(Date& t) { while (true) { cin >> t; if (cin.good()) break; cout << "Invalid date!! " << endl << "Dates must have one of the following formats :" << endl << " yyyy/mm/dd" << endl << " yyyy:mm:dd" << endl << " yyyy-mm-dd" << endl << "where yyyy >= Date::baseYear; 1 <= mm <=12;" << " 1 <= dd <= days in mm" << endl << "Note: no spaces are allowed in the date" << endl << "Enter the date -- "; cin.clear(); clrStream(); } } //**************************************** // function print all dates //**************************************** void printAll(Date t[]) { cout << "This is a list of the dates available." << endl; cout << "NUMBER date" << endl; for (int i=0; i< MAX; i++) cout << setw(4) << i+1 << " : " << t[i] << endl; } //**************************************** // function to obtain a valid date //**************************************** void read(Date t[]) { int loc; cout << "Which location do you wish to put it in (1--" << MAX << ")? "; getInRange(loc); clrStream(); cout << "Enter the date -- "; getValid(t[loc-1]); clrStream(); } //***************************************************** // function to print a specific date //***************************************************** void print(Date t[]) { int loc; cout << "Which date do you wish to print (1--" << MAX << ")? "; getInRange(loc); clrStream(); cout << "date " << loc << " is : " << t[loc-1] << endl; } //************************************************************* // function to print a specific date using words for the month //************************************************************* void wordPrint(Date t[]) { int loc; cout << "Which date do you wish to print (1--" << MAX << ")? "; getInRange(loc); clrStream(); cout << "date " << loc << " is : "; t[loc-1].print(); cout << endl; } //************************************************************** // function to test the difference in years, months and days //************************************************************** void diffInYMD(Date t[]) { int loc1, loc2; cout << "Enter number of first date to use (1--" << MAX << ")? "; getInRange(loc1); clrStream(); cout << "Enter number of second date to use (1--" << MAX << ")? "; getInRange(loc2); clrStream(); int years, months, days; Date before, after; if (t[loc1-1] < t[loc2-1]) { before = t[loc1-1]; after = t[loc2-1]; } else { before = t[loc2-1]; after = t[loc1-1]; } years = after.year() - before.year(); months = after.month() - before.month(); days = after.day() - before.day(); if (days < 0) { days += Date::daysIn(before.month()-1,after.year()); months--; } if (months < 0) { months+=12; years--; } cout << "There are " << years << " years " << months << " months and " << days << " days between " << t[loc1-1] << " and " << t[loc2-1] << endl; } //************************************************************** // function to test the difference in days //************************************************************** void diffInDays(Date t[]) { int loc1, loc2; cout << "Enter number of first date to use (1--" << MAX << ")? "; getInRange(loc1); clrStream(); cout << "Enter number of second date to use (1--" << MAX << ")? "; getInRange(loc2); clrStream(); Date first, second; if (t[loc1-1] < t[loc2-1]) { first = t[loc1-1]; second = t[loc2-1]; } else { first = t[loc2-1]; second = t[loc1-1]; } int totalDays; if (first.year() == second.year() && first.month() == second.month()) totalDays = second.day()-first.day(); else { // start with the number of days left in the first month totalDays=Date::daysIn(first.month(), first.year())- first.day(); first.addMonth(); // move to next month while (first < second) { if (first.year() != second.year() || first.month() != second.month()) totalDays += Date::daysIn(first.month(), first.year()); first.addMonth(); } totalDays+= second.day(); } cout << "There are " << totalDays << " days between " << t[loc1-1] << " and " << t[loc2-1] << endl; } //********************************************* // function to get an int in the range 1 to MAX //********************************************* void getInRange(int& i) { while (true) { getValid(i); // obtain a valid int if (i >= 1 && i <= MAX) break; // if in range, we are happy; outta here cout << "The integer must be between 1 and " << MAX << ". Try again -- "; clrStream(); } } //******************************************* // function to test the comparison operators //******************************************* void compare(Date t[]) { int loc1, loc2; string op; bool result; cout << "Enter number of first date to use (1--" << MAX << ")? "; getInRange(loc1); clrStream(); cout << "Enter number of second date to use (1--" << MAX << ")? "; getInRange(loc2); clrStream(); cout << "Enter the comparison to use (==, !=, <, <=, >, >=) -- "; cin >> op; clrStream(); if (op == "==") result = t[loc1-1] == t[loc2-1]; else if (op == "!=") result = t[loc1-1] != t[loc2-1]; else if (op == "<") result = t[loc1-1] < t[loc2-1]; else if (op == "<=") result = t[loc1-1] <= t[loc2-1]; else if (op == ">") result = t[loc1-1] > t[loc2-1]; else if (op == ">=") result = t[loc1-1] >= t[loc2-1]; else { cout << op << " is not a valid comparison operator" << endl; return; } t[loc1-1].print(); cout << " " << op << " "; t[loc2-1].print(); cout << " is "; if (result) cout << "true."; else cout << "false."; cout << endl; }