943,851 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 460
  • Java RSS
Nov 17th, 2008
0

Help with my code....

Expand Post »
actually,,this is not my whole code..i just post the important one,,i am having problem with my code....i dont know how to figure out,,,

everytime i am trying to withdraw and deposit..,,the balance will always be zero,, and i dont know how to figure it out....
Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. public class BankAccount{
  3.  
  4.  
  5.  
  6. String deposit = "";
  7. String withdraw = "";
  8. int withdrawAmount;
  9. float depositAmount;
  10. float balance = (float) 0.0;
  11. float newBalance;
  12. ATM atm = new ATM();
  13.  
  14.  
  15.  
  16. public void depositCash(){
  17.  
  18.  
  19. try{
  20. deposit = JOptionPane.showInputDialog(null,"How much you want to deposit?");
  21. depositAmount = Float.parseFloat(deposit);
  22. newBalance = newBalance +depositAmount;
  23.  
  24.  
  25. JOptionPane.showMessageDialog(null, "You have deposit an amount of :\nP"+depositAmount);
  26. atm.optionList();
  27.  
  28. }catch(Exception e){
  29. JOptionPane.showMessageDialog(null,"Invalid input");
  30. depositCash();
  31. }
  32.  
  33. }
  34.  
  35.  
  36.  
  37.  
  38. public boolean withdrawCash(){
  39.  
  40. do{
  41. try{
  42. withdraw = JOptionPane.showInputDialog(null, "How much you want to withdraw?");
  43. withdrawAmount = Integer.parseInt(withdraw);
  44. newBalance = balance - withdrawAmount;
  45.  
  46.  
  47. if (withdrawAmount>100){
  48. JOptionPane.showMessageDialog(null, "You have withdraw for a total amount of:" +
  49. "\nP"+withdrawAmount);
  50. atm.optionList();
  51. return withdrawCash();
  52.  
  53. }
  54. else if(withdrawAmount<100){
  55. JOptionPane.showMessageDialog(null,"Invalid amount. Any withdrawal must be at least" +
  56. "P100.00.\nPlease try again.");
  57. continue;
  58.  
  59. }else if(withdrawAmount>newBalance){
  60. JOptionPane.showMessageDialog(null, "Sorry, you have insufficient balance to" +
  61. " withdraw the amount you want. Please check your balance first. Thank you.");
  62. continue;
  63.  
  64. }
  65.  
  66. }catch(Exception e){
  67. JOptionPane.showMessageDialog(null, "invalid input.");
  68. continue;
  69. }
  70. }while(true);
  71. }
  72. public float getBalance(){
  73.  
  74. JOptionPane.showMessageDialog(null,"You have a balance of :\nP" +newBalance+"\nin your" +
  75. " savings account.");
  76. atm.optionList();
  77.  
  78. return newBalance;
  79. }
  80. }
Last edited by ~s.o.s~; Nov 17th, 2008 at 11:04 pm. Reason: Added code tags, learn to use them.
Similar Threads
Reputation Points: 5
Solved Threads: 0
Light Poster
l_03 is offline Offline
37 posts
since Oct 2008
Nov 18th, 2008
0

Re: Help with my code....

float balance = (float) 0.0;

Saying (float) is unnecessary here, the compiler knows you are declaring a float.

The 'continue' statements are unnecessary, as far as I know, so you should consider that. (It isn't your problem, I don't think)

However, one way to limit down your problem is to add some print statements. For example, did you check to make sure you're reading in what the user entered correctly? That is a big step towards figuring out where your problem is. Try it and tell me what you get
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How many users checked a link in 15 mins?
Next Thread in Java Forum Timeline: repositioning buttons!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC