Results 1 to 6 of 6
- 10-11-2010, 02:55 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
How to display "Insufficient amount"
Hello I am woriking on a bank account program, it accept balance amount, deposit amount and wihtraw amount, as a result of this display account balance. I want to add a feature on my withraw method, if withraw amount is greater that balance display "Insufficient amount" message. Below is my withraw method;
I don't know where to placeJava Code://Withraw method public double withraw() { //Check if we can do withraw if (balance >= withraw) { balance = balance - withraw; return withraw; } else return 0.0; }
I tried before and after return 0.0; however I did not see message when I tried to withraw amount bigger than balance .Java Code:System.out.println("Insufficient amount");
If you request all codes I can post it.
- 10-11-2010, 03:15 PM #2
Placing the print statement after the return statement will do nothing, and in fact should result in a compiler error (I think, unless it's just a warning, either way, it won't help).
We don't need to see all of your code, but it would help to see what you tried with the print statements. How are you calling this method? What are you doing with the return value?
- 10-11-2010, 03:16 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
A bank account doesn't print anything; it just has to keep an amount of money; if I were you I'd implement a simple method, say, isSufficient(double amount) that returns a boolean value indicating that the account has enough money to withdraw 'amount' money. Something else that wants to print something can call this method and print something if necessary; this is called 'separation of concerns and/or responsibilities'.
kind regards,
Jos
- 10-11-2010, 04:04 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
is this what you mean?
Java Code://Withraw method public double withraw() { //Check if we can do withraw if (balance >= withraw) { balance = balance - withraw; return withraw; } else //Can not do withraw return isSufficient(); } public String isSufficient() { System.out.println(" InSufficient amount"); }
- 10-11-2010, 04:20 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
- 10-11-2010, 04:20 PM #6
No. Firstly, that doesn't compile: you can't return a String from a method that returns a double. And you can't not return a String from a method that is supposed to.
Secondly, it doesn't separate the bank features from the display features.
I think you should take a step back. Where were you putting the print statement originally that it didn't work?
Similar Threads
-
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
"java -version" doesn't display proper value.
By goldest in forum New To JavaReplies: 8Last Post: 11-06-2009, 09:30 AM -
JtextPane does not display properly when initialized with ""
By orfano in forum AWT / SwingReplies: 0Last Post: 04-20-2009, 11:08 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks