#include "account.h" #include using namespace std; // present a menu and make sure user enters a valid choice int menu() { int answ; // always ask for choice at least once do { cout << "Choose:" << endl; cout << " 1) Deposit 2) Withdrawl 3) Account Balance 4) Quit " << endl; cin >> answ; } while ((answ < 1) || (answ > 4)); // make sure valid return answ; } // try to find an account number in a an array of accounts // Given: // account number to look for (a string) // array of accounts // number of accounts in the array int FindAccount(const string ¤tAcct,Account all[],int num) { int find = -1; // default to not found // loop through the array for (int cnt = 0; cnt < num;cnt++) { // see if current account matches the account number we're looking for bool here = all[cnt].ThisAcct(currentAcct); // if the account number was found return where (we're returning the first place it is found // (we'd like to think that there won't be duplicates) if (here) { find = cnt; return find; } } // end for // if it gets here the account wasn't found - -1 will be returned return find; } // ask for a number within a range // not allowing an exit until a valid number is entered // Given: // the lowest valid value // the highest valid value // the prompt to ask the user (a string) double GetValid(double low, double high, string prompt) { cout << prompt << " (between " << low << " and " << high << ") " << flush; int res; cin >> res; // keep on asking until they give a valid value while ((res < low) || (res > high)) { cout << "That number was invalid " << endl; cout << prompt << " (between " << low << " and " << high << ") " << flush; cin >> res; } return res; } int main() { // build an array of 10 random accounts - each with a 0 balance Account all[10]; cout << "In this dummy bank there are only 10 valid accounts. Work with these: " << endl; for (int cnt = 0;cnt < 10;cnt++){ // display it cout << " " << all[cnt] << endl; } char quit = 'n'; // loop until the user is ready to quit while (quit != 'y') { int index; // ask for account number - loop until account number given exists do { cout << "Enter your account number: " << flush; string currentAcct; cin >> currentAcct; // search whole array of accounts for the entered acct number index = FindAccount(currentAcct,all,10); } while (index < 0); // stay in as long as acct num not found in array // now we have a valid account and know where it is // ask for customers choice int toDo = menu(); // loop until customer says to quit while (toDo != 4) { double amount; bool ok; // handle customers request switch (toDo) { case 1: // deposit - ask for valid amount then do it amount = GetValid(1.0,2000.0,"Please enter the deposit amount "); ok = all[index].Deposit(amount); break; case 2: // withdraw - ask for valid amount then do it amount = GetValid(20.0,200.0,"Please enter the withdrawl amount "); ok = all[index].Withdraw(amount); break; case 3: // account inquiry - show status cout << "Your Account " << all[index] << endl; break; } // ask customer for next choice toDo = menu(); } // see if user wants to exit // (loop until they give us a valid answer to the question) do { cout << "Is it time to quit the program? " << flush; cin >> quit; // force all to lower case to simplify while test below if (quit == 'Y') quit = 'y'; if (quit == 'N') quit = 'n'; } while ((quit != 'n') && (quit != 'y')); } // end outer while // display all accounts cout << "Updated Accounts: " << endl; for (int cnt2 = 0;cnt2 < 10;cnt2++){ cout << " " << all[cnt2] << endl; } cout << "Goodbye!" << endl; return 0; }