Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: help with java class (deposit and withdraw problem)

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default help with java class (deposit and withdraw problem)

    My program is crashing and I dont understand why ???!??!?!!? help I think it has to do with the input file


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


    Deposit Withdraw

    PHP Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
     
    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?");
     
         
    intRatekeyboard.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 

    heres the saving account class

    PHP Code:

    public class SavingAccount
    {
       private 
    double balance;
       private 
    double interestRate;
       private 
    double lastInterest;
     
     
    // SavingsAccount constructor
     
       
    public SavingAccountdouble 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;
     
        }
     
     
     



  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: help with java class (deposit and withdraw problem)

    Hello pip0!
    It will help if you explain what do you mean by "crashing"(exceptions, errors, etc).
    Just a notice in your DepositWithdraw class:
    // 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; 
           }
    You accidentally put a semicolon after the while statement. The statements inside the loop won't execute.

Similar Threads

  1. Java deposit and Withdraw problem
    By ravsau in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2011, 10:32 PM
  2. JAVA problem : Create a Fan Class
    By broo7198 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 9th, 2011, 12:23 AM
  3. Java certificate of deposit calculator project
    By jimbo357 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 17th, 2011, 02:18 AM
  4. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM
  5. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 02:25 AM