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
|
#include <iostream>
using namespace std;
class SavingsAccount
{
private:
int dollars, cents;
void normalize();
public:
SavingsAccount();
void makeDeposit(int, int); //Adds value to dollars and cents
void makeWithdrawal(int, int); //Subtracts values to dollars and cents
void showBalance(); //Displays current balance in dollars and cents
};
SavingsAccount::SavingsAccount() //Default constructor
{
dollars = cents = 0;
}
void SavingsAccount::makeDeposit(int dol, int cnt)
{
dollars += dol; //Adds deposit to the account
cents += cnt;
normalize();
}
void SavingsAccount::makeWithdrawal(int dol, int cnt)
{
dollars -= dol; //Subtracts withdrawal from account
cents -= cnt;
normalize();
}
void SavingsAccount::showBalance()
{
cout << "The balance is " << dollars << " dollars and " << cents << " cents" << endl;
}
void SavingsAccount::normalize() // Converts cents into dollars & cents
{
if (cents < 0)
{
dollars--;
cents += 100;
}
else
{
dollars = dollars + (cents / 100);
cents = cents % 100;
}
}
int main()
{
SavingsAccount bank1;
int depositDollar=40, depositCents=25, withdrawDollar=10, withdrawCents=75; //Variables to hold account, deposit, and withdrawal
int startingCents, startingDollars;
char option;
cout << "Please input the initial dollars" << endl; //Prompt for starting amount in dollars and cents
cin >> depositDollar;
cout << "Please input the initial cents" << endl;
cin >> depositCents;
bank1.makeDeposit(depositDollar, depositCents);
cout << "Would you like to make a deposit? Y or y for yes." << endl; //Prompt for deposit
cin >> option;
while (option == 'Y' || 'y')
{
cout << "Please input the dollars to be deposited" << endl;
cin >> depositDollar;
cout << "Please input the cents to be deposited" << endl;
cin >> depositCents;
bank1.makeDeposit(depositDollar, depositCents);
cout << "Would you like to make a deposit?" << endl;
cin >> option;
if (option == 'N' || 'n')
{
cout << "Would you like to make a withdrawal?" << endl;
cin >> option;
}
else if (option == 'Y' || 'y')
{
cout << "Please input the dollars to be withdrawn" << endl;
cin >> withdrawDollar;
cout << "Please input the cents to be withdrawn" << endl;
cin >> withdrawCents;
bank1.makeWithdrawal(withdrawDollar, withdrawCents);
}
}
bank1.showBalance();
return 0;
}
|