// Oldham, Jeffrey D. // Modified by: Massingill, B. // 1999 Nov 15 // CS 1320 // Bank Account Class and Program // Megalarge Bank/Insurance/Investment Co, Inc, Ltd. requested us to // write software for an electronic checkbook to be given to // customers. The checkbook should permit the following: // 1) creation of an account by "depositing" money, // 2) "depositing money" into the account, and // 3) writing sequentially numbered checks on the account. // The checking account should always have a nonnegative balance to // ensure that Megalarge Bank/Insurance/Investment Co, Inc, Ltd. does // go into multibillion dollar debt. // (Another way to say this: MBIIC is not planning to cover its // customers' bad checks.) #include #include // -------- class definition -------- class checkingAcct { private: // ---- member variables ---- // We decide to maintain the balance in terms of cents to avoid // precision problems. unsigned int balance; // measured in US cents unsigned int checkNum; public: private: public: // ---- constructor function ---- // Create an account with the specified balance and initial //check number. checkingAcct( const unsigned int initialDollars, const unsigned int initialCents, const unsigned int beginningCheckNum) { balance = initialDollars * 100 + initialCents; checkNum = beginningCheckNum; } // ---- member functions ---- // Write a check for the given amount. The check's number is // returned. If the check will bounce, an error message is // printed and the returned value is zero. unsigned int writeCheck(const unsigned int dollars, const unsigned int cents) { if (dollars * 100 + cents > balance) { const unsigned int overdrawAmt = dollars * 100 + cents - balance; cerr << "Balance is smaller than check amount; " << "the check will bounce!\n"; cerr << '$' << overdrawAmt / 100 << '.' << overdrawAmt % 100 << " more needed.\n"; cerr << "Transaction refused.\n"; return 0; } balance -= dollars * 100 + cents; ++checkNum; return checkNum-1; } // Deposit money in the account. void deposit(const unsigned int dollars, const unsigned int cents) { const unsigned int increase = dollars * 100 + cents; if (increase >= 10000*100) cerr << "Federal regulations require reporting your " << "deposit to the U.S. Treasury.\n"; balance += increase; return; } // Query the balance. void queryBalance(unsigned int & dollars, unsigned int & cents) { dollars = balance / 100; cents = balance % 100; return; } // Print the balance. void print(ostream & out) { out << '$' << balance / 100 << '.' << setw(2) << setfill('0') << balance % 100; return; } // ---- destructor function ---- // When the checking account is being destroyed, warn if the // user will lose all the money in the account. ~checkingAcct(void) { if (balance > 0) { cerr << "\nYou are losing access to all of " << "your money.\n"; print(cerr); cerr << " will be sent to the " << "president/monarch/dictator of\n"; cerr << "Megalarge Bank/Insurance/Investment Co, Inc, Ltd.\n"; } return; } }; // -------- main program -------- int main() { checkingAcct Peter(500,0,1001); checkingAcct Paul = checkingAcct(250,34, 51); cout << "Peter has "; Peter.print(cout); cout << ".\n"; cout << "Paul has "; Paul.print(cout); cout << ".\n\n"; cout << "Peter works hard and deposits his paycheck.\n"; Peter.deposit(123, 23); cout << "Peter has "; Peter.print(cout); cout << ".\n\n"; cout << "Paul spends money here, there, and everywhere.\n"; cout << "Check numbers:\n"; cout << "\t" << Paul.writeCheck(73,00) << endl; cout << "\t" << Paul.writeCheck(135,59) << endl; cout << "\t" << Paul.writeCheck(93,12) << endl; cout << "Paul has "; Paul.print(cout); cout << ".\n\n"; // can we rob Peter to pay Paul? no; balance is private. // (uncomment these lines and try to compile to see what // goes wrong.) // Peter.balance -= 5138; // Paul.balance += 5138; // cout << "Robbing Peter to pay Paul.\n"; cout << "Both Peter and Paul die of natural causes.\n"; cout << "Peter had "; Peter.print(cout); cout << " unspent.\n"; cout << "Paul had "; Paul.print(cout); cout << " unspent.\n\n"; return 0; }