I have written a solution to the following assignment but I wont get any output.
I used System.out.println randomly to see where my program stops and I figured out it is righ after the while (inputFile.hasNext()) method. Any suggestion will be great. Thanks!
my assignment:
build a program that uses 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 substract 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.
Deposits and withdrawals
/** designed by saurav */ import java.io.*; // needed for File and IOException import java.util.Scanner; //imports scanner class import java.text.DecimalFormat; public class DepositWithdraw { public static void main(String[] args)throws IOException { double intRate ; double interestEarned; double amount=0.00; double bal=500.00; // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Get the annual interest rate. System.out.print("What is the annual interest rate?"); intRate= keyboard.nextDouble(); // Create a SavingAccount object. SavingAccount SavingAccount1= new SavingAccount(bal,intRate); // Open the Deposits.txt file. File file = new File ("Deposits.txt"); System.out.println("works1"); Scanner inputFile = new Scanner(file); // Get the deposits from the Deposits.txt file while (inFile.hasNext()) while (inputFile.hasNext()); { System.out.println("works1"); //Read numbers double num1 = inputFile.nextDouble(); //Add the numbers amount += num1; } System.out.println("works2"); //Deposit the file input. SavingAccount1.deposit(amount); //Close the Deposits.txt file inputFile.close(); // Open the Withdrawals.txt file. //Open Withdrawal file File file2 = new File("Withdrawal.txt"); Scanner inputFile2 = new Scanner(file2); System.out.println("works3"); // Get the withdrawals from the Withdrawals.txt file while (inFile.hasNext()) while (inputFile2.hasNext()); { //Read numbers double num2 = inputFile2.nextDouble(); //Subtract amount-= num2; } System.out.println("works4"); inputFile2.close(); // Close the Withdrawals.txt file. // Get the balance before adding interest. SavingAccount1.getBalance(); // Add the interest. SavingAccount1.addInterest(); // Calculate the interest earned. SavingAccount1.getLastInterest(); System.out.println("works5"); // Display the interest earned and the balance. System.out.println("Account balance $" + SavingAccount1.getBalance()); System.out.println("Total interest earned $" + SavingAccount1.getLastInterest()); } }//end class
Savingacount class
/** designed by Saurav Sharma */ public class SavingAccount { private double balance; private double interestRate; private double lastInterest; // SavingsAccount constructor public SavingAccount( double bal,double intRate) { balance = bal; interestRate = intRate; lastInterest = 0.0; } // The withdraw method withdraws an amount from the account. public void withdraw(double amount) { balance -= amount; } //The deposit method deposits an amount into the account. public void deposit(double amount) { balance+=amount; } /** * The addInterest method calculates the monthly * interest and adds it to the account balance. */ public void addInterest() { // Get the monthly interest rate. double monthlyInterestRate = interestRate / 12; // Calculate the last amount of interest earned. lastInterest = monthlyInterestRate * balance; // Add the interest to the balance. balance += lastInterest; } //The getBalance method returns the account balance. public double getBalance() { return balance; } /** * The getInterestRate method returns the annual * interest rate. */ public double getIntrestrate() { return interestRate; } //The getLastInterest method returns the last amount of interest earned. public double getLastInterest() { return lastInterest; } }