Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problems #10 and #11, page 350 10. SavingsAccount Class Design a SavingsAccount

ID: 3574888 • Letter: P

Question

Problems #10 and #11, page 350

10. SavingsAccount Class
Design a SavingsAccount class that stores a savings account’s annual interest rate and balance. The class constructor should accept the amount of the savings account’s starting balance. The class should also have methods for subtracting the amount of a withdrawal, adding the amount of a deposit, and adding the amount of monthly interest to the balance. The monthly interest rate is the annual interest rate divided by 12. To add the monthly interest to the balance, multiply the monthly interest rate by the balance and add the result to the balance.
Test the class in a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, performing the following:
a. Ask the user for the amount deposited into the account during the month. Use the class method to add this amount to the account balance.
b. Ask the user for the amount withdrawn from the account during the month. Use the class method to subtract this amount from the account balance.
c. Use the class method to calculate the monthly interest.  
After the last iteration, the program should display the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned.

11. Deposit and Withdrawal Files
Use Notepad or another text editor to create a text file named Deposits.txt. The file should contain the following numbers, one per line:
100.00
125.00
78.92
37.55
Next, create a text file named Withdrawals.txt. The file should contain the following numbers, one per line:
29.88
110.00
27.52
50.00
12.90
The numbers in the Deposits.txt file are the amounts of deposits that were made to a savings account during the month, and the numbers in the Withdrawals.txt file are the amounts of withdrawals that were made during the month. Write a program that creates an instance
of the SavingsAccount class that you wrote in Programming Challenge 10. The starting balance for the object is 500.00. The program should read the values from the Deposits.txt file and use the object’s method to add them to the account balance. The program should read the values from the Withdrawals.txt file and use the object’s method to subtract them from the account balance. The program should call the class method to calculate the monthly interest, and then display the ending balance and the total interest earned.

Explanation / Answer

//program 1

#include<iostream>

using namespace std;

class SavingsAccount
{
   double interest_rate;
   double balance;
public:
   SavingsAccount()
   {
       interest_rate = 0;
       balance = 0;
   }
   SavingsAccount(double bal)
   {
       balance = bal;
   }
   void withdraw(double amt)
   {
       balance -= amt;
   }
   void deposit(double amt)
   {
       balance += amt;
   }
   void set_interest_rate(double rate)
   {
       interest_rate = rate;
   }
   double get_rate()
   {
       return interest_rate;
   }
   double get_balance()
   {
       return balance;
   }
   double monthly_interest()
   {
       return (interest_rate / (12*100));
   }
};

int main()
{
   double balance, annual_rate;
   double deposit_amt, withdraw_amt, monthly_rate;
   int no_months;
   //declare variable to hold total_deposit and total_withdraw
   double total_deposit = 0, total_withdraw = 0,total_interest_earned = 0;

   cout << "Enter Starting balance = ";
   cin >> balance;
   cout << "Enter annual interest rate = ";
   cin >> annual_rate;
   //create savings account
   SavingsAccount account(balance);
   //set annual_interest rate for account
   account.set_interest_rate(annual_rate);
   //calculate monthly interest rate
   monthly_rate = account.monthly_interest();
   cout << " The number of months that have passed since the account was established = ";
   cin >> no_months;
   //go thrugh each month and calculate balance , loop through 12 months
  
   for (int i = 0; i < 12; i++)
   {
       cout << "Enter amount to be deposited, enter any number or 0 if you dont want to deposit: ";
       cin >> deposit_amt;
       cout << "Enter amount to be withdraw, enter any number or 0 if you dont want to deposit: ";
       cin >> withdraw_amt;
       if (deposit_amt > 0 && withdraw_amt > 0)
       {
               account.deposit(deposit_amt);
               account.withdraw(withdraw_amt);
               total_interest_earned += account.get_balance() * monthly_rate;
               total_deposit += deposit_amt;
               total_withdraw += withdraw_amt;

               //interest earned will add up to balance , so call deposit method to add monthly interest
               account.deposit(total_interest_earned);
       }
      

   }
   cout << "After 12 months ";
   cout << "ending balance = " << account.get_balance() << endl;
   cout << "the total amount of deposits = " << total_deposit << endl;
   cout << "the total amount of withdrawals = " << total_withdraw << endl;
   cout << "the total interest earned = " << total_interest_earned<<endl;
     
}

---------------------------------------------------------------

//program 2

#include<iostream>
#include<fstream>
//define MAX input to be from file
#define MAX 100
using namespace std;

class SavingsAccount
{
   double interest_rate;
   double balance;
public:
   SavingsAccount()
   {
       interest_rate = 0;
       balance = 0;
   }
   SavingsAccount(double bal)
   {
       balance = bal;
   }
   void withdraw(double amt)
   {
       balance -= amt;
   }
   void deposit(double amt)
   {
       balance += amt;
   }
   void set_interest_rate(double rate)
   {
       interest_rate = rate;
   }
   double get_rate()
   {
       return interest_rate;
   }
   double get_balance()
   {
       return balance;
   }
   double monthly_interest()
   {
       return (interest_rate / (12*100));
   }
};

int main()
{
   //declare two array to hold deposits and withdrawals read from files
   double deposits[MAX];
   double withdraw[MAX];
   //input stream object
   ifstream in1;
   ifstream in2;
   in1.open("Deposits.txt");
   in2.open("Withdrawals.txt");
   if (!in1 || !in2)
   {
       cout << "Not able to open input files" << endl;
       return -1;
   }
   //read from Deposit file into deposit array
   int count1 = 0;
   while (!in1.eof())
   {
       in1 >> deposits[count1++];
   }
   //read from withdrawal file into withdrawal array
   int count2 = 0;
   while (!in2.eof())
   {
       in2 >> withdraw[count2++];
   }
   //create savings account with 500 balance
   SavingsAccount account(500);
   //set annual_interest rate for account as 5%
   account.set_interest_rate(5);
   //variable to hold total interest eacrned
   double total_interest_earned = 0, monthly_rate;
   //calculate monthly interest rate
   monthly_rate = account.monthly_interest();
   int i, j;
   if (count1 <= count2)
   {
       for (i = 0, j = 0; i < count1; i++, j++)
       {
           account.deposit(deposits[i]);
           account.withdraw(withdraw[i]);
           total_interest_earned += account.get_balance() * monthly_rate;
           //interest earned will add up to balance , so call deposit method to add monthly interest
           account.deposit(total_interest_earned);
       }
       while (j < count2)
       {
           account.withdraw(withdraw[j++]);
           total_interest_earned += account.get_balance() * monthly_rate;
           //interest earned will add up to balance , so call deposit method to add monthly interest
           account.deposit(total_interest_earned);
       }
   }
   if (count1 > count2)
   {
       for ( i = 0, j = 0; i < count2; i++, j++)
       {
           account.deposit(deposits[i]);
           account.withdraw(withdraw[i]);
           total_interest_earned += account.get_balance() * monthly_rate;
           //interest earned will add up to balance , so call deposit method to add monthly interest
           account.deposit(total_interest_earned);
       }
       while (i < count1)
       {
           account.withdraw(withdraw[i++]);
           total_interest_earned += account.get_balance() * monthly_rate;
           //interest earned will add up to balance , so call deposit method to add monthly interest
           account.deposit(total_interest_earned);
       }

   }
  
   cout << "ending balance = " << account.get_balance() << endl;
   cout << "the total interest earned = " << total_interest_earned << endl;
  
}

-----------------------------

output2

ending balance = 648.859
the total interest earned = 12.8173

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote