Part1:Create a SavingsAccount class and write a program that uses it, by carring
ID: 3621985 • Letter: P
Question
Part1:Create a SavingsAccount class and write a program that uses it, by carring out the following steps.
Create a C++ class declaration for the SavingAccount class which has the following private member data:
long dollars,
cents;
and which has the following public member functions:
1. A default constructor to create an account with an initial balance of 100 dollars and 0 cents.
2. a constructor to create an account with the initial dollars and cents passed to it.
3. A function that makes a deposit. This adds the dollars and cents passed to it onto the balance.
4. A function that makes a withdrawal. Providing the balance is greater than or equal to the amount to be withdrawn, the dollars and cents passed to it are substracted from the balance; otherwise an insufficient funds message is printed.
5. A function to return the balance converted to dollars. That is, if dollars is 95 and cents is 2, it would return 95.02
Write the implementation code for all the member functions. Be sure to perform normalization on cents. This means that if cents is 100 more, it must increment dollars by the appropriate amount. Example if cents is 234, then dollars must be increased by 2 and reduced to 34.Be sure also your program can correctly handle actions like withdrawing 10 dollars and 85 cents from balance of 135 dollars and 12 cents.
Write the code for a main function that creates an object of your class called acct1 with an initial balance of $200.50. Deposit $40.50.Withdraw $100.98. Get and print the balance. These amounts may be hard coded into the program. The output should display with 2 decimal points and look like the following.
balance: $140.02
additional information: The class looks llike the following:
Class SavingsAccount
{
public:
SavingsAccount();
SavingsAccount(int newDol, int newCent);
void deposit (int depDol,int depCent);
void withdrawal(int wdDol, int wdCent);
double showBalance();
private:
long dollars;
long cents;
};
Part2: Modify the program to have the user input the initial balance and all deposit and withdrawal amounts. Place the deposit statements in a loop and the withdrawal statements in another loop so that users can make as many deposits and then as many withdrawals as they wish. After all deposits and withdrawals have been made, the final balance should be displayed.
Sample Run:
Input the initial dollars: 402
Input the initial cents: 78
Would you like to make a deposit(Y or y for yes)? y
input the dollars to be deposited: 35
input the cents to be deposited: 67
Would you like to make a deposit(Y or y for yes)? y
input the dollars to be deposited: 19
input the cents to be deposited: 150
Would you like to make a deposit(Y or y for yes)? n
would you like to make a withdrawal(Y or y for yes)? y
Input the dollars to be withdrawn: 28
Input the cents to be withdrawn: 8
would you like to make a withdrawal(Y or y for yes)? n
Balance: $430.87
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
class SavingsAccount{private:
long dollars,cents;
public:
SavingsAccount()
{dollars=0;
cents=0;
}
SavingsAccount(int newDol, int newCent)
{dollars=newDol;
cents=newCent;
}
void deposit (int depDol,int depCent)
{dollars+=depDol;
cents+=depCent;
int extra=cents/100;
dollars+=extra;
cents=cents-extra*100;
}
void withdrawal(int wdDol, int wdCent)
{int extra;
if(wdCent>100)
{extra=wdCent%100;
wdDol+=extra;
wdCent-=(wdCent/100);
}
if(wdDol>dollars)
{cout<<"insufficient funds ";
return;
}
dollars-=wdDol;
cents-=wdCent;
if(cents<0)
{dollars--;
cents+=100;
}
}
double showBalance()
{double amt=dollars+cents/100.;
return amt;
}
};
int main()
{
char choice='y';
int d,c;
cout<<"Input the initial dollars: ";
cin>>d;
cout<<"Input the initial cents: ";
cin>>c;
SavingsAccount a(d,c);
cout<<"Would you like to make a deposit(Y or y for yes)?";
cin>>choice;
while(choice=='y'||choice=='Y')
{cout<<"Input the dollars to be deposited: ";
cin>>d;
cout<<"Input the cents to be deposited: ";
cin>>c;
a.deposit(d,c);
cout<<"Would you like to make another deposit(Y or y for yes)?";
cin>>choice;
}
cout<<"Would you like to make a withdrawal(Y or y for yes)?";
cin>>choice;
while(choice=='y'||choice=='Y')
{cout<<"Input the dollars to be withdrawn: ";
cin>>d;
cout<<"Input the cents to be withdrawn: ";
cin>>c;
a.withdrawal(d,c);
cout<<"Would you like to make another withdrawal(Y or y for yes)?";
cin>>choice;
}
cout<<"Balance: $"<<a.showBalance()<<endl;
system("pause");
return 0;
}