#include "account.h" #include #include using namespace std; // ask yes no question - not allowing to continue until y/Y/n/N entered // returns a lower case y or n (always! nothing else) char askYN (const string & prompt) { char answ; // (loop until they give us a valid answer to the question) do { // ask for and get answer cout << prompt << " (y or n): " << flush; cin >> answ; // force all to lower case to simplify while test below if (answ == 'Y') answ = 'y'; if (answ == 'N') answ = 'n'; } while ((answ != 'n') && (answ != 'y')); return answ; } // 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 a vector of accounts // Given: // account number to look for (a string) // vector of accounts // DONT NEED number of accounts in the vector!! int FindAccount(const string ¤tAcct,vector &all) { int find = -1; // default to not found // find out number of accounts in the vector int num = all.size(); // loop through the vector 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 a vector of 10 random accounts - each with a 0 balance vector all(10); // unfortunately - all elements in vector initialized with same default account // instead of different ones // go back and change all after the first for (int cnt = 1;cnt < 10;cnt++){ // create a random account number Account curr; // assign it into the vector all[cnt] = curr; } cout << "In this dummy bank there are only 10 valid accounts. Work with these: " << endl; for (int cnt1 = 0;cnt1 < 10;cnt1++){ // display it cout << " " << all[cnt1] << 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); // if the account is not found give user option to create one if (index < 0) { // ask user if it should be created char create = askYN("Account number does not exist, Should it be created?"); if (create == 'y') { // add a zero balance account with the acct num entered Account newDummy(currentAcct); all.push_back(newDummy); // note location of new account index = all.size() - 1; // if size is 11, index is 10 } // if answer was no, they will be reprompted for acct num because // will have index < 0 and will stay in the do-while loop } // end if index not found } 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) quit = askYN("Is it time to quit the program? "); } // end outer while // display all accounts cout << "Updated Accounts: " << endl; for (int cnt2 = 0;cnt2 < all.size();cnt2++){ cout << " " << all[cnt2] << endl; } cout << "Goodbye!" << endl; return 0; }