Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- /*
- 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.
- */
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws FileNotFoundException {
- double startingBalance;
- double annualInterestRate;
- double deposit;
- double withdraw;
- double totalDeposits = 0;
- double totalWithdraws = 0;
- double totalInterest = 0;
- double months = 0;
- Scanner scanner = new Scanner( System.in );
- System.out.print( "Starting balance :" );
- startingBalance = scanner.nextDouble();
- com.company.SavingsAccount account1 = new com.company.SavingsAccount( startingBalance );
- account1.setAnnualInterestRate( .1 ); // .1 is 10%
- double totalInterestEarned = 0;
- File depositFile = new File ( "Deposits.txt");
- File withdrawlsFile = new File ( "Withdrawls.txt");
- Scanner inputFile = new Scanner ( depositFile );
- while( inputFile.hasNext() ) {
- account1.deposit( inputFile.nextDouble() );
- totalDeposits += account1.deposit( inputFile.nextDouble() );
- }
- inputFile.close();
- inputFile = new Scanner ( withdrawlsFile );
- while( inputFile.hasNext() ) {
- account1.withdraw( inputFile.nextDouble() );
- //account1.withdraw( totalWithdraws );
- totalWithdraws += account1.withdraw( inputFile.nextDouble() );
- }
- inputFile.close();
- /*
- inputFile = new Scanner ( withdrawlsFile );
- while( inputFile.hasNext() ) {
- account1.withdraw( inputFile.nextDouble() );
- account1.withdraw( totalWithdraws );
- }
- inputFile.close();
- */
- account1.setAnnualInterestRate( .10 );
- totalInterestEarned = account1.monthlyInterest();
- System.out.printf( "Total deposits: $%,.2f\nTotal withdraws: $%,.2f\nEnding balance: $%,.2f \nTotal interest earned: $%,.2f\n",
- totalDeposits, totalWithdraws, account1.getBalance(), account1.monthlyInterest() );
- System.out.println( totalWithdraws );
- /*
- for ( int month = 1; month <= months; month ++ ){
- System.out.print( "Deposit in month " + month + ":" );
- deposit = scanner.nextDouble();
- totalDeposits += deposit;
- account1.deposit( deposit );
- System.out.print( "Withdraw in month " + month + ":" );
- withdraw = scanner.nextDouble();
- totalWithdraws += withdraw;
- account1.withdraw( withdraw );
- account1.monthlyInterest();
- totalInterest += account1.monthlyInterest();
- }
- */
- }
- }
Add Comment
Please, Sign In to add comment