Accessing account balance in Bank System application

Luka Elkington :

I am currently trying to create a simple bank program in which users can create an account, deposit and withdraw money. The problem is that I am having trouble with creating accounts.

Here is my main:

Account account = new Account();
account.createAccount(1000, "Bob");

Here is my Account.java class:

public void createAccount(int bal, String name){

    int balance = bal;

    String username = name;

    System.out.println("An account has been set up for "+name+", with a balance of $"+bal+".");

The problem I'm having is accessing the accounts which are created. The account is created and the string runs, but I am not sure how I would be able to access the account.

I want to add a method in which you can call the account's balance but am not sure how to do this. Any help would be greatly appreciated.

dovetalk :

You aren't showing much of your Account class (which is fine -- having a Minimal Reproducible Example is good, but a fully compilable code snippet would be preferable), so my answer will go with some assumptions:

  1. You need to be able to retrieve and set the balance.
  2. You don't currently have a solution for this problem.

I'll assume your current class looks like this (eliminating whitespace for brevity):

public class Account {
    public void createAccount(int bal, String name) {
        int balance = bal;
        String username = name;
        System.out.println("An account has been set up for "+name+", with a balance of $"+bal+".");
    }
}

As @ScaryWombat pointed out in his comment, you need to move the balance and username variables out of the createAccount method and into the class as fields. You then set these fields in the createAccount method:

public class Account {
    private int balance = bal;
    private String username = name;
    public void createAccount(int bal, String name) {
        this.balance = bal;
        this.username = name;
        System.out.println("An account has been set up for "+name+", with a balance of $"+bal+".");
    }
}

Now, an Account instance holds a balance and a name, which is overwritten when you call createAccount.

However, we still have a problem. Every time you call createAccount, the data is overwritten. For example in this code, Bob's account will be forgotten and the data overwritten by Alice's account:

Account account = new Account();
account.createAccount(1000, "Bob");
account.createAccount(2000, "Alice");

This is where constructors come in. Instead of a createAccount method, we'll move the logic into the Account constructor:

public class Account {
    private int balance = bal;
    private String username = name;
    public Account(int bal, String name) {
        this.balance = bal;
        this.username = name;
        System.out.println("An account has been set up for "+name+", with a balance of $"+bal+".");
    }
}

With this, the only way to create an account is to specify the starting details in the call to new Account():

Account bobsAccount = new Account(1000, "Bob");
Account alicesAccount = new Account(2000, "Alice");

However, we still haven't addressed the core of your question: How to access the balance? The following will result in an error:

bobsAccount.balance = 2000;

This is because the balance field is private (as it should be). What we need is to add accessor methods to the Account class:

public class Account{
...
    public void setBalance(int newBalance) {
        balance = newBalance;
        System.out.println(name+"'s account balance is now $"+bal+".");
    }
    public int getBalance() {
        return balance;
    }
...
}

You might ask "why go to this trouble?", well, we can add checks (e.g. not allow a balance to go below zero), add logging to audit access to balances, or even not have a setBalance or getBalance, but only transferFrom(Account otherAccount) and transferTo(Account otherAccount) (ensuring money is never added, but only transferred around in the system).

PS. and as pointed out in other comments, int is never good for monetery amounts. What you need is a Currency type that uses something else, e.g. BigDecimal, to store the actual values.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27162&siteId=1