This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Bank Account Program Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bank Account Program Help" Watch "Bank Account Program Help" New topic
Author

Bank Account Program Help

Victor Knyt
Greenhorn

Joined: Aug 17, 2005
Posts: 5
I am a new programmer, and I need a program to start me off that can do the following:

1. Get the following input from the user:
a) Beginning bank account balance
b) Ask whether they are depositing or withdrawing
c) Then ask the amount of the deposit or withdrawal

2. It should also keep track of the balance and print the balance out to the screen after every transaction


Thanks to those ahead of time that are willing to help me!

[ August 17, 2005: Message edited by: Victor Knyt ]
[JAM -- I edited the title so that people are less likely to ignore it]
[ August 17, 2005: Message edited by: Joel McNary ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Nobody is going to write your program for you.
If you show us how you would like to implement it (in pseudo code or something similar) then people here will point you in the right direction for what you need to know to implement it in Java.

Just as an aside, are you sure 1a is a correct requirement ? Bank machines usually know what your balance is.


Joanne
Victor Knyt
Greenhorn

Joined: Aug 17, 2005
Posts: 5
Right, but this is a simple program, so for the bank balance I'm going to need to ask the user to enter it in.

And this is what I have so far (I know there is still a lot more to be added):

import java.io.*;
import javax.swing.JOptionPane;

public class Bank
{
public static void main (String args[])
{
float money;

JOptionPane.showInputDialog(null, "Please enter your bank account balance:");

JOptionPane.showInputDialog(null, "Are you depositing or withdrawing?");

JOptionPane.showInputDialog(null, "How much would you like to deposit?");

JOptionPane.showInputDialog(null, "How much would you like to withdrawa?");

/*I know that somewhere in the above statement you need to have a If and Then statement
that way you know if they chose to withdraw, then the "How much would you like to withdrawa?" box will pop up
or deposit, based on what they have chosen*/
}
}
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
JOptionPane.showInputDialog returns a String containing whatever the user typed in, so you have to assign this value to a variable.
e.g.

You will then need to convert this String to a float or double. See the API docs for the Float and Double classes for ways to do this.

Repeat this for the deposit and withdrawal amounts.

For the dialog asking whether the user wants to deposit or withdraw, you are probably better off using JOptionPane.showOptionDialog() method, but if you want to stick with showInputDialog, you will need to compare the value returned.


Have a play around with these and if you have any problems, post your new code.
Victor Knyt
Greenhorn

Joined: Aug 17, 2005
Posts: 5
Thank you for the help! I'm completely lost, so basically I am gonna try to "parse" the questions, and if that doesn't work I'll just call it quits.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Don't give up. Your first post gave the impression that you were just expecting people to do your homework for you. That's probably why you didn't get many answers.
Your later posts show you you have done some work, so I think you'll find folk on here will be happy to help you. Post your new code and I'm sure you'll get plenty of helpful suggestions.
Victor Knyt
Greenhorn

Joined: Aug 17, 2005
Posts: 5
Well this is what I have so far...any help is appreciated.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Okay.
returns a double value, so you need to assign it to a variable.Note, that this can throw a NumberFormatException if what the user entered is not a valid floating point number, so you will need to put this in a try...catch statement.

Assign the values returned from your "how much would you like to deposit/withdraw" dialogs into a variable as I showed you before and convert them to doubles as shown above.

You can then use these values to do calculation e.g.and you can use System.out.println() to output text to the screen.

Let me know how you get on or if you have any problems.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Originally posted by Joanne Neal:
Note, that this can throw a NumberFormatException if what the user entered is not a valid floating point number, so you will need to put this in a try...catch statement.


Sorry. NumberFormatException is an unchecked exception, so if you haven't looked at try...catch statements yet, ignore this part. Just make sure that anything you type in is a legal floating point number.
Victor Knyt
Greenhorn

Joined: Aug 17, 2005
Posts: 5
Joanne Neal, thank you very much for all your help and such quick replies, I'm gonna leave it at what I have (with your corrections ofcourse).

Thanks once more.
[ August 17, 2005: Message edited by: Victor Knyt ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm going to go off on a wild tangent for a bit on a neat way to solve problems. Given a problem like this, it's sometimes cool to work it from the inside out. The real guts of this thing keeps account balances. What if you start by writing this, and then make an Account class that makes it say "true" ...

That should take only a minute. Then you can quickly add these:

In a matter of minutes you can have a very small amount of running, tested code. With confidence that this works, you can build up the bit that translates "100.00" and "deposit" strings into 100.00D and calls the deposit method. Then build up the GUI that gets those values from the user.

Now for this problem you've already gotten past most of this stuff so it's kinda too late. For the next assignment, search for the smallest, simplest thing you could build and test. See if that sounds like a fun approach.

BTW: I used doubles here. They are lousy for money. Any amounts with pennies would likely fail the tests. But that's another topic.
[ August 17, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
If you are building up a project in stages like this, it is probably worth looking into the concept of unit testing as well. It will probably seem a bit complex at first and maybe a bit of an overkill for small projects, but once you have a good grasp of Java and you are starting to work on bigger projects, its benefits can be enormous.
Test Infected provides a good introduction.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Right on, Joanne. I avoided mentioning JUnit because as much as I love it, I spent a whole day getting my head around making it work the first time. Take a look at that link, Victor. If you can digest it quickly, go for it. JUnit is good stuff fer sure.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bank Account Program Help
 
Similar Threads
Banking with Threads
doubt on Threads
monolithic form to object oriented abstraction
Thread question
Replicating a simple bank account