Practice

Account



What is the exact output of the following program?  Show your intermediate work.


 #include <iostream>
 #include <iomanip>
 using namespace std;
 #define D_RATE 0.0365

 // Date is a utility class that manages dates in day/mon/year format
 class Date {
     int day;
     int mon;
     int year;
     int daysinmonth(int mon, int year) const {
         int days;
         switch (mon) {
         case 2:
             days = daysinyear(year) - 337;
             break;
         case 4:
         case 6:
         case 9:
         case 11:
             days = 30;
             break;
         default:
             days = 31;
         }
         return days;
     }
     int daysinyear(int year) const { return year % 400 || year % 4 ? 365 : 366; } 
 public:
     Date() {
         day = 1;
         mon = 1;
         year = 2000;
     }
     Date(int d, int m, int y) {
         day = d;
         mon = m;
         year = y;
     }
     friend ostream& operator<<(ostream& os, const Date& date) {
         os << setfill('0') << setw(2) << date.day << '/' <<
               setw(2) << date.mon << '/' << setw(4) << date.year <<
               ' ' << setfill(' ');
         return os;
     }
     friend bool operator>(const Date& left, const Date& right) {
         return left.day + left.mon * 31 + left.year * 400 >
                right.day + right.mon * 31 + right.year * 400;
     }
     friend int operator-(const Date& left, const Date& right) {
         int days = 0;
         if (left.year >= right.year) {
             days = left.day - right.day;
             for (int i = left.mon; i > 1; i--)
                 days += left.daysinmonth(i, left.year);
             for (int i = right.mon; i > 1; i--)
                 days -= right.daysinmonth(i, right.year);
             for (int i = right.year; i > left.year; i--)
                 days -= right.daysinyear(i);
             if (days < 0)
                 days = 0;
         } else
             days = 0;
         return days;
     }
 };

 class Account {
     double bal;
 public:
     Account() {
         bal = 0.0;
         cout << 'C' << endl;
     }
     ~Account() {
         cout << '~';
         display();
         cout << endl;
     }
     double balance() const { return bal; }
     void deposit(double amt) { bal += amt; }
     bool withdraw(double amt) {
         bool withdrawn;
         if(bal >= amt) {
             bal -= amt;
             withdrawn = true;
         } else {
             withdrawn = false;
             cout << "w Request rejected: Insufficient funds" << endl;
         }
         return withdrawn;
     }
     void display() const { cout << setw(11) << bal; }
 };

 class Savings : public Account {
     double rate;
     double acc;
     Date last;
     Date today;
 public:
     Savings() {
         rate  = D_RATE;
         acc   = 0.0;
         last  = Date();
         today = Date();
     }
     Savings(const Date& date, double r) {
         rate  = r;
         acc   = 0.0;
         last  = date;
         today = date;
     }
     ~Savings() {
         cout << '~';
         display();
     }
     void update(const Date& date) {
         if(date > today)
             today = date;
     }
     void compound() {
         deposit(acc);
         acc = 0.0;
     }
     void accrue() {
         int days = today - last;
         acc += days * rate * balance() / 365;
         last = today;
     }
     void deposit(double amt) {
         accrue();
         Account::deposit(amt);
     }
     bool withdraw(double amt) {
         accrue();
         return Account::withdraw(amt);
     }
     void display() const {
         cout << ' ' << today << setw(3) << acc << setw(5) << rate; 
         cout << ' ';
         Account::display();
         cout << endl;
     }
 };

 int main() {
     cout << fixed << setprecision(2);
     Savings s[2];

     s[0].update(Date(10,3,2008));
     s[1] = Savings(Date(11,3,2008), 0.073);
     cout << 'a';
     s[0].display();
     cout << 'b';
     s[1].display();
     s[0].deposit(100.00);
     s[1].deposit(200.00);
     s[0].update(Date(10,4,2008));
     s[1].update(Date(11,4,2008));
     cout << 'c';
     s[0].display();
     cout << 'd';
     s[1].display();
     s[0].accrue();
     cout << 'e';
     s[0].display();
     cout << 'f';
     s[1].display();
     s[0].compound();
     s[1].compound();
     cout << 'g';
     s[0].display();
     cout << 'h';
     s[1].display();
     s[0].withdraw(100.00);
     s[1].withdraw(201.00);
     cout << 'i';
     s[0].display();
     cout << 'j';
     s[1].display();

     return 0;
 }

Show your output here:









































   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site