My code is complete. It runs properly and produces the correct output. I did calculations by hand to check and then ran the program and it gives me the same result. I just want a second opinion. Are there ways for my code to be more efficient? Are there small details that I need to change? My professor marked me off for tiny errors, so I want to cover all the bases. I included the instructions down below just in case.
Design a SavingsAccount class that stores a savings account's balance, annual interest rate. The class constructor should accept the amount of savings account's starting balance and annual interest rate. 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 amount to the balance. The class should also has mutator and accessor methods for each data field.
Then write a test program that calculate the balance of a savings account at the end of a period of time. The test program should ask the user the annual interest rate, the starting balance, and the number of months that have passed since the account was established. Then a loop should iterate once for every month, performing the following:
- ask the user the amount deposited into the account during that month. Then add the amount to the account balance.
- ask the user for the amount withdrawn from the account during the month. The subtract the amount from the balance
- 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.
public class SavingsAccount
{
//Data fields
private double balance; //Account Balance
private double annualInterestRate; //Account annual interest rate
private double monthlyInterestRate;
private double totalDeposits;
private double totalWithdraws;
private double totalInterest;
/**
* Constructor
* @param startBalance The account's balance.
* @param annual_Interest_Rate The annual interest rate.
*/
public SavingsAccount(double startBalance, double annual_Interest_Rate)
{
balance = startBalance;
annualInterestRate = annual_Interest_Rate;
}
//end of Constructor
/**
* setAnnualInterestRate method sets the annual interest
* rate and calculates the monthly interest rate
* @param annual_Interest_Rate The annual interest rate.
*/
public void setAnnualInterestRate(double annual_Interest_Rate)
{
monthlyInterestRate = annualInterestRate / 12;
}
//end of setAnnualInterestRate method
/**
* The deposit method adds the amount to the balance
* and calculates the total deposit
* @param amount
*/
public void setDeposit(double amount)
{
balance += amount;
totalDeposits += amount;
}
//end of deposit method
/**
* The withdraw method subtracts the amount to the balance
* and calculates the total withdraws
* @param amount
*/
public void setWithdraw(double amount)
{
balance -= amount;
totalWithdraws += amount;
}
//end of withdraw method
/**
* The calculateMonthlyInterest method calculates the total
* interest and adds the monthly interest to the balance
*/
public void calculateMonthlyInterest()
{
totalInterest = totalInterest + balance * monthlyInterestRate;
balance = balance + balance * monthlyInterestRate;
}
//end of calculateMonthlyInterest method
/**
* The getBalance method returns the account's balance.
* @return The value of the balance field.
*/
public double getBalance()
{
return balance;
}
/**
* The getAnnualInterestRate method returns the annual interest rate.
* @return The value of the annual interest rate field.
*/
public double getAnnualInterestRate()
{
return annualInterestRate;
}
/**
* The getMonthlyInterestRate method returns the monthly interest rate.
* @return The value of the monthly interest rate field.
*/
public double getMonthlyInterestRate()
{
return monthlyInterestRate;
}
/**
* The getTotalDeposits method returns the total deposit amount.
* @return The value of the total deposits field.
*/
public double getTotalDeposits()
{
return totalDeposits;
}
/**
* The getTotalWithdraws method returns the total withdraws amount.
* @return The value of the total withdraws field.
*/
public double getTotalWithdraws()
{
return totalWithdraws;
}
/**
* The getTotalInterest method returns the total interest amount.
* @return The value of the total interest field.
*/
public double getTotalnterest()
{
return totalInterest;
}
/* displayData method displays the ending details of the savings account */
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: $" + totalDeposits);
System.out.println("Total amount of withdraws: $" + totalWithdraws);
System.out.println("Total interest earned: $" + totalInterest);
}
//end of displayData method
}
//end of SavingsAccount class
Test class:
import java.util.Scanner;
public class SavingsAccountTest
{
public static void main(String[] args)
{
double startBalance;
double annual_Interest_Rate;
int months;
double deposit_Amount;
double withdraw_Amount;
//Create an object for Scanner class
Scanner input = new Scanner(System.in);
//Prompt user for starting balance
System.out.print("Enter starting balance: $");
startBalance = input.nextDouble();
//Prompt user for annual interest rate
System.out.print("Enter annual interest rate: ");
annual_Interest_Rate = input.nextDouble();
//Prompt user for number of months
System.out.print("Enter the number of months: ");
months = input.nextInt();
/* Create an object for SavingsAccount class */
SavingsAccount sa = new
SavingsAccount(startBalance, annual_Interest_Rate);
//Call to setAnnualInterestRate method
sa.setAnnualInterestRate(annual_Interest_Rate);
//Loop
for (int i = 0; i < months; i++)
{
/* Prompt user for deposit amount */
System.out.print("Enter amount to deposit for the month " + (i+1) + ":$");
deposit_Amount = input.nextDouble();
//Call to deposit method
sa.setDeposit(deposit_Amount);
/* Prompt user for amount to withdraw */
System.out.print("Enter amount to withdraw for the month " + (i+1) + ":$");
withdraw_Amount = input.nextDouble();
//Call to withdraw method
sa.setWithdraw(withdraw_Amount);
/* Call to calculateMonthlyInterest method */
sa.calculateMonthlyInterest();
}
//end of loop
//Call to displayData method
sa.displayData();
}
//end of main method
}
//end of SavingsAccountTest class