Deposit and Withdrawal files

The problem

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
124.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 the 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.

This exercise will use java 8 to to calculate the totals of withdrawals and deposits from a file. If you cannot use java 8, you can read all the lines of the file and then call the deposit or withdrawal for each line item.

List<String> deposits = Files.readAllLines(depositPath);
List<String> withdrawals = Files.readAllLines(withdrawlsPath);

Breaking it down

SavingsAccount 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;
    }
}

Main program

public static void main(String args[]) throws IOException {

    // Create a Scanner to read keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Ask user to enter annual interest rate
    System.out.print("Enter the savings account's "
            + "annual interest rate: ");
    double interestRate = keyboard.nextDouble();

    // close keyboard
    keyboard.close();

    DepositAndWithdrawalFiles depositAndWithdrawalFiles = new DepositAndWithdrawalFiles();
    SavingsAccount savingsAccount = depositAndWithdrawalFiles.new SavingsAccount(
            500, interestRate);

    // create a path to the deposit file
    Path depositPath = Paths
            .get("src/main/resources/com/levelup/java/exercises/beginner/Deposits.txt")
            .toAbsolutePath();

    // sum all lines in file and setting value in saving account
    double totalDeposits = Files.lines(depositPath)
            .mapToDouble(Double::valueOf).sum();

    savingsAccount.deposit(totalDeposits);

    // create a path to the withdrawls file
    Path withdrawlsPath = Paths
            .get("src/main/resources/com/levelup/java/exercises/beginner/Withdrawls.txt")
            .toAbsolutePath();

    // sum all lines in file and setting value in saving account
    double totalWithdrawls = Files.lines(withdrawlsPath)
            .mapToDouble(Double::valueOf).sum();

    savingsAccount.withdraw(totalWithdrawls);

    // Get the balance before adding interest.
    double priorBalance = savingsAccount.getAccountBalance();

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

    // Get the interest earned.
    double interestEarned = savingsAccount.getLastAmountOfInterestEarned();

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

    // Show user interest earned and balance.
    System.out
            .println("Interest earned: $" + dollar.format(interestEarned));

    System.out.println("Prior balance: $" + dollar.format(priorBalance));

    System.out.println("Ending balance: $"
            + dollar.format(savingsAccount.getAccountBalance()));

}

Output

Enter the savings account's annual interest rate: 20
Interest earned: $1,016.95
Prior balance: $610.17
Ending balance: $1,627.12