| | |
Help with my code....
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
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....
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)
import javax.swing.*; public class BankAccount{ String deposit = ""; String withdraw = ""; int withdrawAmount; float depositAmount; float balance = (float) 0.0; float newBalance; ATM atm = new ATM(); public void depositCash(){ try{ deposit = JOptionPane.showInputDialog(null,"How much you want to deposit?"); depositAmount = Float.parseFloat(deposit); newBalance = newBalance +depositAmount; JOptionPane.showMessageDialog(null, "You have deposit an amount of :\nP"+depositAmount); atm.optionList(); }catch(Exception e){ JOptionPane.showMessageDialog(null,"Invalid input"); depositCash(); } } public boolean withdrawCash(){ do{ try{ withdraw = JOptionPane.showInputDialog(null, "How much you want to withdraw?"); withdrawAmount = Integer.parseInt(withdraw); newBalance = balance - withdrawAmount; if (withdrawAmount>100){ JOptionPane.showMessageDialog(null, "You have withdraw for a total amount of:" + "\nP"+withdrawAmount); atm.optionList(); return withdrawCash(); } else if(withdrawAmount<100){ JOptionPane.showMessageDialog(null,"Invalid amount. Any withdrawal must be at least" + "P100.00.\nPlease try again."); continue; }else if(withdrawAmount>newBalance){ JOptionPane.showMessageDialog(null, "Sorry, you have insufficient balance to" + " withdraw the amount you want. Please check your balance first. Thank you."); continue; } }catch(Exception e){ JOptionPane.showMessageDialog(null, "invalid input."); continue; } }while(true); } public float getBalance(){ JOptionPane.showMessageDialog(null,"You have a balance of :\nP" +newBalance+"\nin your" + " savings account."); atm.optionList(); return newBalance; } }
Last edited by ~s.o.s~; Nov 17th, 2008 at 11:04 pm. Reason: Added code tags, learn to use them.
•
•
Join Date: Sep 2008
Posts: 1,625
Reputation:
Solved Threads: 205
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
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
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the Java Forum
- Previous Thread: How many users checked a link in 15 mins?
- Next Thread: repositioning buttons!!
| Thread Tools | Search this Thread |
Tag cloud for Java
2dgraphics addball android api apple applet application arguments array arrays automation bidirectional binary blackberry bluetooth business chat class classes client code collections component database derby dragging draw eclipse educational error event exception formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image ingres input integer internet ip j2me java javadesktopapplications javaprojects jni jpanel julia linux list loop looping map method methods mobile myregfun mysql netbeans newbie number numbers oracle phone print problem producer program programming project recursion scanner screen server set size sms smsspam socket sort sql string swing swt tcp test threads time tree ubuntu ui websites windows






