Implement a class Bank . This bank has two objects, checking and savings of type
ID: 3744896 • Letter: I
Question
Implement a class Bank. This bank has two objects, checking and savings of type Account that was developed (will post underneath). Implement four member functions:
deposit(double amount, string account)
withdraw(double amount, string account)
transfer(double amount, string account)
print_balance()
Here is the account string is "S" or " C". For the deposit or withdrawl, it indicates which account is affected. For a transfer it indicates the account from which the money the money is taken; the money is automatically transfer to the other account.
Test your program by using the main function below.
int main()
{
Bank my_bank;
cout << "Inital bank balances: ";
my_bank.print_balances(); /* set up empty accounts */
cout << "Adding some money to accounts: ";
my_bank.deposit(1000, "S"); /* deposit $1000 to savings */
my_bank.deposit(2000, "C"); /* deposit $2000 to checking */
my_bank.print_balances();
cout << "Taking out $1500 from checking,and moving $200 from";
cout << " savings to checking. ";
my_bank.withdraw(1500, "C"); /* withdraw $1500 from checking */
my_bank.transfer(200, "S"); /* transfer $200 from savings */
my_bank.print_balances();
cout << "Trying to transfer $900 from Checking. ";
my_bank.transfer(900,"C");
my_bank.print_balances();
cout << "trying to transfer $900 from Savings. ";
my_bank.transfer(900,"S");
my_bank.print_balances();
return 0;
}
And here is the Account type:
#include <iostream>
#include <cmath>
using namespace std;
class Account{
public:
double amount, rate;
Account(){
amount = 10000;
rate = 6;
}
Account(double a, double r){
amount = a;
rate = r;
}
double findDoublingTime() {
double monthlyRate = rate / 1200;
double months = log(2)/log(1 + monthlyRate);
double years = months/12;
return years;
}
void deposit(double a){
if(a < 0){
return;
}
amount += a;
}
void withdraw(double a){
if(amount-a < 0){
return;
}
amount -= a;
}
double get_balance(){
return amount;
}
};
int main()
{
double balance, rate;
cout << "Enter initial balance: $";
cin >> balance;
cout << "Enter annual interest rate: ";
cin >> rate;
Account my_account(balance, rate); // Set up my account
cout << "The time required to double the amount is: " << my_account.findDoublingTime() << " years." << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
class Bank
{
public:
double savingAmount, checkingAmount, rate;
Bank()
{
savingAmount = 10000;
checkingAmount = 10000;
rate = 6;
}
Bank(double a, double r)
{
savingAmount = a;
rate = r;
}
double findDoublingTime()
{
double monthlyRate = rate / 1200;
double months = log(2) / log(1 + monthlyRate);
double years = months / 12;
return years;
}
void deposit(double a, string account)
{
if (a < 0)
{
return;
}
if (account == "S")
savingAmount += a;
if (account == "C")
checkingAmount += a;
}
void withdraw(double a, string account)
{
if (savingAmount - a < 0)
{
return;
}
if (account == "S")
savingAmount -= a;
if (account == "C")
checkingAmount -= a;
}
void print_balances()
{
cout << "Saving Amount : " << savingAmount << " Checking Amount is : " << checkingAmount << endl;
}
void transfer(double amount, string account)
{
if (account == "C")
{
checkingAmount -= amount;
savingAmount += amount;
}
if (account == "S")
{
savingAmount -= amount;
checkingAmount += amount;
}
}
};
int main()
{
Bank my_bank;
double balance, rate;
cout << "Enter initial balance: $";
cin >> balance;
cout << "Enter annual interest rate: ";
cin >> rate;
Bank my_account(balance, rate); // Set up my account
cout << "Inital bank balances: ";
my_bank.print_balances(); /* set up empty accounts */
cout << "Adding some money to accounts: ";
my_bank.deposit(1000, "S"); /* deposit $1000 to savings */
my_bank.deposit(2000, "C"); /* deposit $2000 to checking */
my_bank.print_balances();
cout << "Taking out $1500 from checking,and moving $200 from";
cout << " savings to checking. ";
my_bank.withdraw(1500, "C"); /* withdraw $1500 from checking */
my_bank.transfer(200, "S"); /* transfer $200 from savings */
my_bank.print_balances();
cout << "Trying to transfer $900 from Checking. ";
my_bank.transfer(900, "C");
my_bank.print_balances();
cout << "trying to transfer $900 from Savings. ";
my_bank.transfer(900, "S");
my_bank.print_balances();
cout << "The time required to double the amount is: " << my_account.findDoublingTime() << " years." << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.