Help with my code....

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 37
Reputation: l_03 has a little shameless behaviour in the past 
Solved Threads: 0
l_03 l_03 is offline Offline
Light Poster

Help with my code....

 
0
  #1
Nov 17th, 2008
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....
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,625
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Help with my code....

 
0
  #2
Nov 18th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC