#include // for cout and cin #include // for setiosflags class BankAccount { public: void Withdraw(double); double GetBalance(void); void Deposit(double); BankAccount(double initial = 0); protected: double balance; private: }; // ---------------------------------------------------------------------------- BankAccount::BankAccount(double initial) { balance = initial; } // ---------------------------------------------------------------------------- void BankAccount::Deposit(double to_deposit) { if (to_deposit >= 0) { balance += to_deposit; } else { cout << endl << endl << "Invalid transaction" << endl; } } // ---------------------------------------------------------------------------- double BankAccount::GetBalance(void) { return balance; } // ---------------------------------------------------------------------------- void BankAccount::Withdraw(double to_withdraw) { if ((to_withdraw >= 0) && (to_withdraw <= balance)) { balance -= to_withdraw; } else { cout << endl << endl << "Invalid transaction" << endl; } } // ============================================================================ #define FEE 20.0 class MoneyMarketAccount : public BankAccount { public: void AddInterest(void); MoneyMarketAccount(double initial = 0, double interest = 0.025, double min_balance = 500); protected: double interest_rate; double minimum_balance; private: }; // ---------------------------------------------------------------------------- void MoneyMarketAccount::AddInterest(void) { if (balance >= minimum_balance) { balance *= (1.0 + interest_rate); } else { balance -= FEE; } } // ---------------------------------------------------------------------------- MoneyMarketAccount::MoneyMarketAccount(double initial, double interest, double min_balance) : BankAccount(initial) { interest_rate = interest; minimum_balance = min_balance; } // ===================================================================================== void get_info(double &balance, double &interest_rate, double &min_balance) { cout << "Please enter starting balance (XXX.XX format): "; cin >> balance; cout << "Please enter interest rate (0.XX format): "; cin >> interest_rate; cout << "Please enter minimum balance (XXX.XX format): "; cin >> min_balance; } // ----------------------------------------------------------------------------- void print_menu_read_choice(int &choice) { int input = 0; while ((input < 1) || (input > 5)) { cout << endl << endl << "MENU" << endl; cout << "------------------------------" << endl; cout << " 1. Show account balance" << endl; cout << " 2. Deposit money" << endl; cout << " 3. Withdraw money" << endl; cout << " 4. Add interest" << endl; cout << " 5. QUIT" << endl << endl; cout << " Your choice? "; cin >> input; if ((input < 1) || (input > 5)) { cout << endl << "INVALD CHOICE" << endl << endl; } } choice = input; } // ----------------------------------------------------------------------------- void show_account_balance(MoneyMarketAccount &Account) { cout.precision(2); cout << setiosflags(ios::fixed); cout << endl << endl << "Balance is $" << Account.GetBalance() << endl; } // ----------------------------------------------------------------------------- void deposit(MoneyMarketAccount &Account) { double ammount; cout << endl << endl << "How much to deposit (XXX.XX format)? "; cin >> ammount; Account.Deposit(ammount); show_account_balance(Account); } // ----------------------------------------------------------------------------- void withdraw(MoneyMarketAccount &Account) { double ammount; cout << endl << endl << "How much to withdraw (XXX.XX format)? "; cin >> ammount; Account.Withdraw(ammount); show_account_balance(Account); } // ----------------------------------------------------------------------------- void add_interest(MoneyMarketAccount &Account) { Account.AddInterest(); show_account_balance(Account); } // ----------------------------------------------------------------------------- void process_choice(MoneyMarketAccount& Account, int choice) { // PRECONDITION: choice is between 1-5 switch (choice) { case 1 : show_account_balance(Account); break; case 2 : deposit(Account); break; case 3 : withdraw(Account); break; case 4 : add_interest(Account); break; // just ignore case 5 - that's quit and there's nothing to do } } // ----------------------------------------------------------------------------- void main (void) { double b, i, m; get_info(b,i,m); MoneyMarketAccount Account(b,i,m); int choice = 0; while (choice != 5) { print_menu_read_choice(choice); process_choice(Account, choice); } cout << endl << endl << "OK - done!" << endl << endl; }