Results 1 to 3 of 3
Thread: Im so close I can feel it. help
- 01-30-2011, 08:29 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
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
- 01-30-2011, 09:27 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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.Java Code:public void debit( double amount) { balance = balance - amount; if (newBalance < 0.0 ) System.out.println( "Debit amount exceeded account balance" ); }
- 01-30-2011, 10:25 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Help on Look and Feel
By mine0926 in forum NetBeansReplies: 4Last Post: 05-05-2010, 04:27 AM -
Look and Feel of GUI
By titir in forum AWT / SwingReplies: 9Last Post: 04-21-2010, 01:34 PM -
Will the connection.close() and statement.close() ever be called???
By Stephen Douglas in forum New To JavaReplies: 13Last Post: 04-09-2010, 11:15 AM -
About look and feel
By makpandian in forum AWT / SwingReplies: 6Last Post: 02-13-2009, 02:55 AM -
Look and Feel
By arun_kumar in forum AWT / SwingReplies: 1Last Post: 11-17-2007, 06:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks