Results 1 to 3 of 3
  1. #1
    subiedude101 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default Im so close I can feel it. help

    I have an assignment that I've been working on for a while that want me to create a debit method that withdraws cash from an account. I also have to make sure that the debit amount does not exceed the accounts balance. If it does, it needs to display a message. So far when I put in the debit amount it automatically takes the account balance to zero. I've been making subtle changes here and there, but i'm still having no luck. Can someone point me in the right direction? Thanks

    Java Code:
     public class Account
    {   
       private double balance; // instance variable that stores the balance
       private double newBalance;
       // constructor  
       public Account( double initialBalance )
       {
          // validate that initialBalance is greater than 0.0; 
          // if it is not, balance is initialized to the default value 0.0
          if ( initialBalance > 0.0 ) 
             balance = initialBalance; 
       } // end Account constructor
    
       // credit (add) an amount to the account
       public void credit( double amount )
       {      
          balance = balance + amount; // add amount to balance 
       } // end method credit
    
       // return the account balance
       public double getBalance()
       {
          return balance; // gives the value of balance to the calling method
       } // end method getBalance
     // end class Account
    
    public void debit( double amount)
    {
    	balance = balance - amount;
    	
    	if (newBalance < 0.0 )
    	 System.out.println( "Debit amount exceeded account balance" );
    	}
    public double getNewBalance()
    {
    	return newBalance;
    }
    }
    Java Code:
    import java.util.Scanner;
    
    public class AccountTest
    {
       // main method begins execution of Java application
       public static void main( String[] args ) 
       {
          Account account1 = new Account( 50.00 ); // create Account object
          Account account2 = new Account( -7.53 ); // create Account object
    
          // display initial balance of each object
          System.out.printf( "account1 balance: $%.2f\n", 
             account1.getBalance() );
          System.out.printf( "account2 balance: $%.2f\n\n", 
             account2.getBalance() );
          
          // create Scanner to obtain input from command window
          Scanner input = new Scanner( System.in );
          double depositAmount; // deposit amount read from user
    double debitAmount;
    
          System.out.println( "Enter deposit amount for account1: " ); // prompt
          depositAmount = input.nextDouble(); // obtain user input
          System.out.printf( "\nadding %.2f to account1 balance\n\n", 
             depositAmount );
          account1.credit( depositAmount ); // add to account1 balance
    
          // display balances
          System.out.printf( "account1 balance: $%.2f\n", 
             account1.getBalance() );
          System.out.printf( "account2 balance: $%.2f\n\n", 
             account2.getBalance() );
          
          System.out.print( "Enter deposit amount for account2: " ); // prompt
          depositAmount = input.nextDouble(); // obtain user input
          System.out.printf( "\nadding %.2f to account2 balance\n\n", 
             depositAmount );
          account2.credit( depositAmount ); // add to account2 balance
    
          // display balances
          System.out.printf( "account1 balance: $%.2f\n", 
             account1.getBalance() );
          System.out.printf( "account2 balance: $%.2f\n", 
             account2.getBalance() );
    
    //display balances
    	    System.out.println( "Enter withdraw amount for account1: " );
    	  debitAmount = input.nextDouble();
    	  System.out.printf( "\ndeducting %.2f to account1 balance\n\n", 
             debitAmount );
          account1.debit( debitAmount );
    
    	  
    	   System.out.printf( "account1 balance: $%.2f\n",
    		  account1.getNewBalance() );
    	   
      System.out.println( "Enter the withdraw ammount for account2:" );
          debitAmount = input.nextDouble();
    	   System.out.printf( "\ndeducting %.2f to account2 balance\n\n",
    		  debitAmount );
    	  account2.debit( debitAmount );
    
    	   System.out.printf( "account2 balance: $%.2f\n\n",
    		  account2.getNewBalance() );
    
    	 
       } // end main
    } // end class AccountTest

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    Java Code:
    public void debit( double amount)
    {
    	balance = balance - amount;
    	
    	if (newBalance < 0.0 )
    	 System.out.println( "Debit amount exceeded account balance" );
    }
    I don't think this does what you intend it to do. Shouldn't it test to see if the debit amount exceeds the balance before it tries to deduct the amount? Also, it might be worth making the method a boolean, so it can return true if the debit is completed successfully or false if no change is made to the balance.

  3. #3
    subiedude101 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Awesome, I finally got it... It took me a while, but I did it ,lol.

    Thank you

Similar Threads

  1. Help on Look and Feel
    By mine0926 in forum NetBeans
    Replies: 4
    Last Post: 05-05-2010, 05:27 AM
  2. Look and Feel of GUI
    By titir in forum AWT / Swing
    Replies: 9
    Last Post: 04-21-2010, 02:34 PM
  3. Replies: 13
    Last Post: 04-09-2010, 12:15 PM
  4. About look and feel
    By makpandian in forum AWT / Swing
    Replies: 6
    Last Post: 02-13-2009, 03:55 AM
  5. Look and Feel
    By arun_kumar in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2007, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •