Write a class to keep track of a balance in a bank account w
Write a class to keep track of a balance in a bank account with a variable for annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero). The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not allow more money to be withdrawn then what is available in the account. Finally, there should be a function that adds interest to the balance (interest accrual) at the current interest rate. This function should have a parameter indicating how many years’ worth of interest are to be added (for example, 0.5 indicate the account should have 6 months’ added). Use the class as part of an interactive program that allows the user to determine how long it will take an initial balance will take to grow to a given value. The program should allow the user to specify the initial balance, the interest rate, and whether there are additional yearly deposits. (Main & Savitch, 2011) Your main function should act as a driver with a menu looping the program.
Solution
public class Account
{
private double balance;
private double annualInterestRate;
private double monthlyInterestRate;
private double deposits;
private double withdrawal;
private double totalInterest;
public Account(double startBalance, double annual_Interest_Rate)
{
balance = startBalance;
annualInterestRate = annual_Interest_Rate;
}
public void setAnnualInterestRate(double annual_Interest_Rate)
{
monthlyInterestRate = annualInterestRate / 12;
}
public void setDeposit(double amount)
{
balance += amount;
deposits += amount;
}
public void setWithdraw(double amount)
{
balance -= amount;
withdrawal += amount;
}
public void calculateMonthlyInterest()
{
totalInterest = totalInterest + balance * monthlyInterestRate;
balance = balance + balance * monthlyInterestRate;
}
public double getBalance()
{
return balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public double getMonthlyInterestRate()
{
return monthlyInterestRate;
}
public double getdeposits()
{
return deposits;
}
public double getwithdrawal()
{
return withdrawal;
}
public double getTotalnterest()
{
return totalInterest;
}
public void displayData()
{
balance = Math.round(balance * 100.0) / 100.0;
totalInterest = Math.round(totalInterest * 100.0) / 100.0;
System.out.println();
System.out.println(\"The ending balance is: $\" + balance);
System.out.println(\"Total amount of deposits: $\" + deposits);
System.out.println(\"Total amount of withdraws: $\" + withdrawal);
System.out.println(\"Total interest earned: $\" + totalInterest);
}
}
import java.util.Scanner;
public class AccountTest
{
public static void main(String[] args)
{
double startBalance;
double annual_Interest_Rate;
int months;
double deposit_Amount;
double withdraw_Amount;
Scanner input = new Scanner(System.in);
System.out.print(\"Enter starting balance: $\");
startBalance = input.nextDouble();
System.out.print(\"Enter annual interest rate: \");
annual_Interest_Rate = input.nextDouble();
System.out.print(\"Enter the number of months: \");
months = input.nextInt();
Account sa = new
Account(startBalance, annual_Interest_Rate);
sa.setAnnualInterestRate(annual_Interest_Rate);
for (int i = 0; i < months; i++)
{
System.out.print(\"Enter amount to deposit for the month \" + (i+1) + \":$\");
deposit_Amount = input.nextDouble();
sa.setDeposit(deposit_Amount);
System.out.print(\"Enter amount to withdraw for the month \" + (i+1) + \":$\");
withdraw_Amount = input.nextDouble();
sa.setWithdraw(withdraw_Amount);
sa.calculateMonthlyInterest();
}
sa.displayData();
}
}


