/* Name: account.cpp * Purpose: Implementation File for Bank Account Class * Author: Dr. Michael A. Redmond * Date Written: 05/04/98 * Date Revised: * NOTES: */ // in account.cpp #include // allows string handling #include // allows rough pre-condidtion checking #include "account.h" // First some non-object-oriented functions useful to the class // obtain a random digit char GetRandDigit() { char dig; int x = rand() % 10; switch (x) { case 0: dig = '0'; break; case 1: dig = '1'; break; case 2: dig = '2'; break; case 3: dig = '3'; break; case 4: dig = '4'; break; case 5: dig = '5'; break; case 6: dig = '6'; break; case 7: dig = '7'; break; case 8: dig = '8'; break; case 9: dig = '9'; break; default: dig = 'X'; // should never happen } return dig; } // Develop a random account number by randomly generating chars and // concatenating them together // problem is that it doesn't result in number that is // guaranteed to not already be being used string GetRandomValidAcctNum () { string acct = ""; // assume 4 digit account numbers for (int cnt = 0;cnt < 4;cnt++) { // generate a random digit char newDig = GetRandDigit(); // add to the end of account number so far acct += newDig; } // really should add a check digit return acct; } // for now just a dummy function string DetermineNextAvailValidAcctNum () { // dont know how to this; temporarily just return any old string return string("1345"); } // default constructor - initializes to zero balance and random acct num //Account::Account() : acctNum(DetermineNextAvailValidAcctNum()), Account::Account() : acctNum(GetRandomValidAcctNum()), balance(0) { } // parameterized constructor - client specifies the account number and // (possibly) the balance (defaults to 0) Account::Account(const string& numb, double bal) : acctNum(numb), balance(bal) { } // deposit money into the account bool Account::Deposit (double amt) { // assert(amt > 0); // ensure valid amount if (amt <= 0) { cerr << "Tried to deposit zero or negative amount - Sorry" << endl; return false; // cannot deposit } else { balance = balance + amt; // update account balance return true; } return true; // should never hit } // withdraw money from the account bool Account::Withdraw (double amt) { // assert(amt > 0); // ensure enough money in count if (amt > balance) { cerr << "Tried to withdraw more " << amt << " than in account " << balance << " - Sorry" << endl; return false; } // ensure amount is valid else if (amt <= 0) { cerr << "Tried to deposit zero or negative amount - Sorry" << endl; return false; } // valid amount else { balance = balance - amt; // update balance return true; } return true; // should never get here } // report account balance double Account::GetBal () { return balance; } // report account number string Account::GetAcctNum () { return acctNum; } // report whether a given account number is the one for an account bool Account::ThisAcct (const string & acctNum) { string curr = GetAcctNum(); if (curr == acctNum) { return true; } else { return false; } return false; // should never execute } // allows outputting all info about account ostream& operator << (ostream & strOut, Account & myAcct) { strOut << "Acct# " << myAcct.GetAcctNum() << " Bal: " << myAcct.GetBal() << " "; return strOut; }