Overclock.net banner

1 - 16 of 16 Posts

·
Registered
Joined
·
4 Posts
Discussion Starter · #1 ·
I am trying to make a bank account program using Java but I am new and stuck. It has to use two classes. So far I have this. It will allow me to input my starting balance and my name. Then it will let me choose either deposit or withdraw but then it skips right to the end rather than go to the if statement. What can I do so that it will go to the loop and define x and preform the correct equation? I'm also new so if my formating is crap, sorry.

Code:

Code:
public class BankAccount 
{

    public double deposit (double balance)
    {
       balance = balance + x;
       return balance;
    }
    public double withdraw (double balance) 
    {
       balance = balance - x;
       return balance;
    }
    public String name;
    public double balance;
    public double x;
}
Code:

Code:
import java.io.*;
import java.util.*;

public class Tester
{
public static void main(String [] args)
{

 Scanner kbReader = new Scanner(System.in);
 System.out.println("Enter amount of monet in account");
 double balance = kbReader.nextDouble();

 System.out.println("Enter name:");
 String name = kbReader.next();

 System.out.println("Withdraw or Deposit");
 String choice = kbReader.next();
if (choice == "Withdraw" )
{

    System.out.println("How much would you like to withdraw?");
     double x = kbReader.nextDouble();
}
 if (choice == "Deposit")
 {

     System.out.println("How much would you like to deposit?");
     double x = kbReader.nextDouble();
}

 System.out.println("The " + name + " account balance is " + balance);
}
}
 

·
Registered
Joined
·
4 Posts
Discussion Starter · #6 ·
OK that helped a lot, thank you. However, now now whenever I run it, It prints the original balance that was input rather than the deposited or withdrawn amount. I think it's a failure of communicate between the two classes.
 

·
Premium Member
Joined
·
1,012 Posts
All you are doing is printing the variable name and balance as you entered them above.

You never use the class you created. Also you might want to put a constructor in that class

and then a way to create an instance of the class and associate it with the name of the account

examples

added constructor and changed methods to store values instead of returning them
added method to get balance

Code:

Code:
public class BankAccount 
{
    private double balance;

    public BankAccount(double balance) {
        this.balance = balance;
    }

    public void deposit(double balance) {
       this.balance += balance;
    }

    public void withdraw(double balance) {
       this.balance -= balance;
    }

    public double getBalance() {
        return this.balance;
    }
}
Code:

Code:
import java.util.HashMap;
import java.util.Scanner;

public class Main
{
    public static void main(String [] args){

        HashMap<String,BankAccount> accounts = new HashMap<String,BankAccount>();
        Scanner reader = new Scanner(System.in);
        String name;
        double balance;

        while(true) {

            System.out.println("[Create] account");
            System.out.println("[Withdraw] money from account");
            System.out.println("[Deposit] money in accout");
            System.out.println("[Get] account balance");
            System.out.println("[q] to quit");

            String minput = reader.next();

            if ("q".equalsIgnoreCase(minput)){
                break;
            }
            if ("create".equalsIgnoreCase(minput)){
                while(true) {
                    System.out.print("Enter name [c] to cancel: ");
                    name = reader.next();

                    if ("c".equalsIgnoreCase(name)){
                        break;
                    }

                    if (accounts.containsKey(name)) {
                        System.out.println("Account name is already in use");
                    }
                    else {
                        System.out.println("Enter amount of money in account");
                        balance = reader.nextDouble();
                        accounts.put(name, new BankAccount(balance));
                        break;
                    }
                }
            }

            if ("withdraw".equalsIgnoreCase(minput)){
                while(true) {
                    System.out.println("Enter name of account");
                    System.out.print("to withdraw from or [c] to cancel: ");
                    name = reader.next();

                    if ("c".equalsIgnoreCase(name)){
                        break;
                    }
                    if (accounts.containsKey(name)) {
                        System.out.print("How much would you like to withdraw? ");
                        balance = reader.nextDouble();
                        accounts.get(name).withdraw(balance);
                        break;
                    }
                    else {
                        System.out.println("account name not found");
                    }
                }
            }

            if ("deposit".equalsIgnoreCase(minput)){
                while(true) {
                    System.out.println("Enter name of account");
                    System.out.print("to deposit into or [c] to cancel: ");
                    name = reader.next();

                    if ("c".equalsIgnoreCase(name)){
                        break;
                    }
                    if (accounts.containsKey(name)) {
                        System.out.print("How much would you like to deposit? ");
                        balance = reader.nextDouble();
                        accounts.get(name).deposit(balance);
                        break;
                    }
                    else {
                        System.out.println("Account name not found");
                    }
                }
            }

            if ("get".equalsIgnoreCase(minput)){
                while(true) {
                    System.out.print("Enter name [c] to cancel: ");
                    name = reader.next();

                    if ("c".equalsIgnoreCase(name)){
                        break;
                    }

                    if (accounts.containsKey(name)) {
                        System.out.println("Account balance for " + name + " is $" +
                                accounts.get(name).getBalance());
                        break;
                    }
                    else {
                        System.out.println("No accout with that name");
                    }
                }
            }
        }
    }
}
create an empty HashMap that will use a string as a key and an instance of BankAccout as the value

Code:

Code:
HashMap<String,BankAccount> accounts = new HashMap<String,BankAccount>();
add the key,value pairs like so

Code:

Code:
accounts.put(name, new BankAccount(balance));
deposit, withdraw, or get the balance by calling the methods on the instance of the class

Code:

Code:
accounts.get(name).getBalance());
accounts.get(name).deposit(balance);
accounts.get(name).withdraw(balance);
 

·
Registered
Joined
·
276 Posts
tom.slick is correct about how you use the class. Before you were simply using it like a variable. The point of a class to be able to create an object, as he showed. However you don't need to use a hashmap, that is more complicated for you to understand. Simply declare one or more bankAccounts as follows:

BankAccount account1 = new BankAccount();
BankAccount account2 = new BankAccount();

and reference like
account1.getBalance();

To tom.slick - if he didn't know to correctly use a class, you shouldn't be describing a more complicated way of declaring an object in list or a hashmap. keep it simply for him. While it might be a better, cleaner way to do it, that is not what he is learning at the moment. People have to learn to walk before they can run. And if I was the instructor and I was teaching someone how to declare a class, and they came back with using a hashmap I would be suspicious of how they came up with that unless they excelled the entire time in the class.
 

·
Premium Member
Joined
·
1,012 Posts
Quote:
Originally Posted by Ike View Post

To tom.slick - if he didn't know to correctly use a class, you shouldn't be describing a more complicated way of declaring an object in list or a hashmap. keep it simply for him. While it might be a better, cleaner way to do it, that is not what he is learning at the moment. People have to learn to walk before they can run. And if I was the instructor and I was teaching someone how to declare a class, and they came back with using a hashmap I would be suspicious of how they came up with that unless they excelled the entire time in the class.
Your right I should have kept it simple. I sometimes forget the steps in between and just solve the problem.
 

·
Registered
Joined
·
278 Posts
Quote:
Originally Posted by Ike View Post

tom.slick is correct about how you use the class. Before you were simply using it like a variable. The point of a class to be able to create an object, as he showed. However you don't need to use a hashmap, that is more complicated for you to understand. Simply declare one or more bankAccounts as follows:

BankAccount account1 = new BankAccount();
BankAccount account2 = new BankAccount();

and reference like
account1.getBalance();

To tom.slick - if he didn't know to correctly use a class, you shouldn't be describing a more complicated way of declaring an object in list or a hashmap. keep it simply for him. While it might be a better, cleaner way to do it, that is not what he is learning at the moment. People have to learn to walk before they can run. And if I was the instructor and I was teaching someone how to declare a class, and they came back with using a hashmap I would be suspicious of how they came up with that unless they excelled the entire time in the class.
If I would of turned in an assignment on classes with a hashmap in my Java class , my teacher would have given me a 0 unless I could run through the code line by line and explain the logic of it all haha.
Quote:
Originally Posted by yui129 View Post

Thank you for simplifying it for me. Should I replace my methods with those?
A "trick" my teacher used to help the people in our class struggling with learning how classes worked, was making you remember that "String" variables aren't a primitive data type. Strings are a class, so any class you make must be used similarly to a String. So for this program, .getBalance() is comparable .equals() in the sense that they're both methods of the class. Hope this helped!
 

·
Registered
Joined
·
2,312 Posts
Quote:
Originally Posted by Failuyr View Post

If I would of turned in an assignment on classes with a hashmap in my Java class , my teacher would have given me a 0 unless I could run through the code line by line and explain the logic of it all haha.
A "trick" my teacher used to help the people in our class struggling with learning how classes worked, was making you remember that "String" variables aren't a primitive data type. Strings are a class, so any class you make must be used similarly to a String. So for this program, .getBalance() is comparable .equals() in the sense that they're both methods of the class. Hope this helped!
=D in my "Web pages for buisnesses" class they were teaching it using dreamweaver... The final basically had a page with a form that when you click submit it would validate the fields and send an email... but it really doesnt send an email because its linked to nothing. So, i decided to write mine with ASXP and C# in the backend where i linked it to my gmail account had my password converted into binary so even though i showed the code, my password wasnt clear text. My page actually emailed on the form submit. (And yes, i know i could have secured it a lotmore, but at least it was quick and easy.)

... It was sad to see that half the class come in wondering why their pages wouldnt work on this computer, but it worked at home. (Static Paths) ... sigh... class was a waste of time for me, but at least it counted for an elective.
 

·
Premium Member
Joined
·
65,162 Posts
Dealing with Bank Account? Need to discuss locking and cohesion!
thumb.gif
 

·
Premium Member
Joined
·
29,532 Posts
Quote:
Originally Posted by DuckieHo View Post

Dealing with Bank Account? Need to discuss locking and cohesion!
thumb.gif
And use of Decimal instead of Float and so on. Don't use Float for money related operations. Alternatively use int or long and save the currency in cents (e.g. $1.00 = 100 in int).
 

·
Premium Member
Joined
·
65,162 Posts
Quote:
Originally Posted by gonX View Post

And use of Decimal instead of Float and so on. Don't use Float for money related operations. Either use int or long and save the currency in cents (e.g. $1.00 = 100 in int) as an int.
Excellent point! This got me once a decade ago when I was an intern writing a spreadmart to calculate a money market allocations.

He also probably should range checking to protect from negative withdrawals/deposits and overflows as well.

[Secure systems are hard!]
 

·
Premium Member
Joined
·
29,532 Posts
Quote:
Originally Posted by DuckieHo View Post

Excellent point! This got me once a decade ago when I was an intern writing a spreadmart to calculate a money market allocations.

He also probably should range checking to protect from negative withdrawals/deposits and overflows as well.

[Secure systems are hard!]
Depending on what language you're programming in, you should probably consider checking if your language supports something similar to the checked keyword in C#.
 
1 - 16 of 16 Posts
Top