1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class BankAcct
{
private:
int _acctNum;
double _balance;
public:
BankAcct( int aNum )
//Constructor
//Pre: None
//Post: BankAcct object is created with account number initialized
// to aNum and balance to 0
{
_acctNum = aNum;
_balance = 0;
}
BankAcct( int aNum, double amt )
: _acctNum( aNum ), _balance( amt )
//Constructor
//Pre: None
//Post: BankAcct object is created with account number initialized
// to aNum and balance to amt
{
_acctNum = aNum;
_balance = 0;
}
int withdraw( double amount )
//Withdrawal Public Method
//Pre: amount should be positive
//Post: If amount is <= to the available balance, balance is
// decreased by amount, else balance remain unchanged
//Return: 1 for successful withdrawal, 0 for failure
{
if (_balance < amount)
return 0;
_balance -= amount;
return 1;
}
void deposit( double amount )
//Deposit Public Method
//Pre: amount should be positive
//Post: If amount is is positive, balance is
// increased by amount, else balance remain unchanged
{
if (amount > 0)
_balance += amount;
}
int getAccountNumber( ) const
//Get Account Number Public Method
//Pre: None
//Post: None (this method doesnt change the object)
//Return: Account number is returned
{
return _acctNum;
}
string toString()
//Return Account Information as a string
//Pre: None
//Post: None (this method doesnt change the object)
// Account number and balance are returned as string
{
ostringstream os;
os << "Account Number: " << _acctNum << endl
<< "Balance: " << _balance << endl;
return os.str();
}
};
/*************** End of BankAcct Class definition *********************/
int findBankAcct( BankAcct* baPtrArr[], int size, int targetAcctNum )
//Purpose: Look for bankAcct object with "targetAccNum" as account
// number.
//Pre: baPtrArr[0...size-1] are valid BankAcct pointers
//Post: This function doesnt change the bank account object(s)
//Return:
// - index (0..size-1) of the BankAcct object if found
// - -1, otherwise
{
for ( int i = 0; i < size; i++ )
{
if ((*baPtrArr + i)->getAccountNumber() == targetAcctNum)
return i;
else
return -1;
}
}
void printAllBankAcct( BankAcct* baPtrArr[], int size )
//Purpose: Print out account information for BankAcct objects
//Pre: baPtrArr[0...size-1] are valid BankAcct pointers
//Post: This function doesnt change the bank account object(s)
{
for ( int i = 0; i < size; i++ )
{
cout << (*baPtrArr + i)->toString();
}
}
int main()
{
//Suggested construct to hold 5 bank account objects eventually
BankAcct* baList[5];
int choice; //for user choice
do {
//Print "Menu" and get user choice
cout << "*************************\n";
cout << "* Bank of Heaven *\n";
cout << "*************************\n";
cout << "Service available: \n";
cout << "(1. New Acct, 2. Deposit,";
cout << " 3. Withdrawal, 4. Print All, 5. exit)\n";
cout << "Your choice: ";
cin >> choice;
//Perform appropriate service;
switch( choice ){
case 1: //New Account
{
int i = 0;
BankAcct(90000001+i);
//Print out result and account info
cout << "Result: [" << i+1 << "/5] account created.";
baList[i]->toString();
}
break;
case 2: //Deposit
{
int accNo, accIndex;
double amt;
cout << "Account Number: " <<endl;
cin >> accNo;
cout << "Amount: " << endl;
cin >> amt;
accIndex = findBankAcct(baList,5,accNo);
if (accIndex == -1)
cout << "Invalid acount number!";
else
baList[accIndex]->deposit(amt);
}
break;
case 3: //Withdrawal
break;
case 4: //Print All
break;
case 5: //Exit
break;
default:
cout << "Result: Invalid choice!\n";
}
} while ( choice != 5 );
return 0;
}
|