SavingsAccount class

The problem

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 twelve. 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:

  • 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.
  • 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.
  • 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.

Breaking it down

Saving account class

class SavingsAccount {

    private double accountBalance;
    private double annualInterestRate;
    private double lastAmountOfInterestEarned;

    public SavingsAccount(double balance, double interestRate) {

        accountBalance = balance;
        annualInterestRate = interestRate;
        lastAmountOfInterestEarned = 0.0;
    }

    public void withdraw(double withdrawAmount) {
        accountBalance -= withdrawAmount;
    }

    public void deposit(double depositAmount) {
        accountBalance += depositAmount;
    }

    public void addInterest() {

        // Get the monthly interest rate.
        double monthlyInterestRate = annualInterestRate / 12;

        // Calculate the last amount of interest earned.
        lastAmountOfInterestEarned = monthlyInterestRate * accountBalance;

        // Add the interest to the balance.
        accountBalance += lastAmountOfInterestEarned;
    }

    public double getAccountBalance() {
        return accountBalance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public double getLastAmountOfInterestEarned() {
        return lastAmountOfInterestEarned;
    }
}

Main program

public static void main(String args[]) {

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Ask user to enter starting balance
    System.out.print("How much money is in the account?: ");
    double startingBalance = keyboard.nextDouble();

    // Ask user for annual interest rate
    System.out.print("Enter the annual interest rate:");
    double annualInterestRate = keyboard.nextDouble();

    // Create class
    SavingsAccountClass savingAccountClass = new SavingsAccountClass();
    SavingsAccount savingsAccount = savingAccountClass.new SavingsAccount(
            startingBalance, annualInterestRate);

    // Ask how long account was opened
    System.out.print("How long has the account been opened? ");
    double months = keyboard.nextInt();

    double montlyDeposit;
    double monthlyWithdrawl;
    double interestEarned = 0.0;
    double totalDeposits = 0;
    double totalWithdrawn = 0;

    // For each month as user to enter information
    for (int i = 1; i <= months; i++) {

        // Get deposits for month
        System.out.print("Enter amount deposited for month: " + i + ": ");
        montlyDeposit = keyboard.nextDouble();
        totalDeposits += montlyDeposit;

        // Add deposits savings account
        savingsAccount.deposit(montlyDeposit);

        // Get withdrawals for month
        System.out.print("Enter amount withdrawn for " + i + ": ");
        monthlyWithdrawl = keyboard.nextDouble();
        totalWithdrawn += monthlyWithdrawl;

        // Subtract the withdrawals
        savingsAccount.withdraw(monthlyWithdrawl);

        // Add the monthly interest
        savingsAccount.addInterest();

        // Accumulate the amount of interest earned.
        interestEarned += savingsAccount.getLastAmountOfInterestEarned();
    }

    // close keyboard
    keyboard.close();

    // Create a DecimalFormat object for formatting output.
    DecimalFormat dollar = new DecimalFormat("#,##0.00");

    // Display the totals and the balance.
    System.out.println("Total deposited: $" + dollar.format(totalDeposits));
    System.out.println("Total withdrawn: $" + dollar.format(totalWithdrawn));
    System.out.println("Interest earned: $" + dollar.format(interestEarned));
    System.out.println("Ending balance: $"
            + dollar.format(savingsAccount.getAccountBalance()));
}

Output

How much money is in the account?: 10000
Enter the annual interest rate:5
How long has the account been opened? 4
Enter amount deposited for month: 1: 100
Enter amount withdrawn for 1: 1000
Enter amount deposited for month: 2: 230
Enter amount withdrawn for 2: 103
Enter amount deposited for month: 3: 4050
Enter amount withdrawn for 3: 2334
Enter amount deposited for month: 4: 3450
Enter amount withdrawn for 4: 2340
Total deposited: $7,830.00
Total withdrawn: $5,777.00
Interest earned: $29,977.72
Ending balance: $42,030.72

Level Up

  • If the user has withdrawn more money they they have within the account during a given month, notify them this operation cannot occur.
  • Typically a deposit amount is a positive amount and a withdrawal is a debit to the account. Ensure the user cannot enter a negative amount for the deposit or withdrawal amount.
  • Write a junit test to validate the behavior within the Savings Account class. Is it producing the right results?