1

Each time a bank account is made the account id should be raised by one, but each time i try to pull the Id i just get the account ID being 0, any suggestions, as i followed exactly how it is in the book im learning from and it still is not updating.

Account constructor :

public class BankAccount {

    public static int bankID = 0;

    //constructor called by BankAccount michaelsBank = new BankAccount();
    public BankAccount(){
        balance = 0;
        lastAssignedNumber++;

        accountNumber = lastAssignedNumber;
    }

    //Constructs a bank account with an initial deposit, will be used if given a number for a parameter
    public BankAccount(double initialBalance){
        balance = initialBalance;
    }


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

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

        public double getBalance(){
            return balance;
        }

        public int getID(){
            return accountNumber;
        }

        private double balance;
        private int accountNumber;
        private static int lastAssignedNumber;
}

Bank account main program:

import java.text.*;

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

            NumberFormat formatter = NumberFormat.getNumberInstance();
            formatter.setMaximumFractionDigits(2);  // Helps formatter format for final output
            formatter.setMinimumFractionDigits(2);
            ConsoleReader console = new ConsoleReader(System.in);

     System.out.println("Hello, would you like to make a new bank account?");
     String newA = console.readLine();

     if(newA.equalsIgnoreCase("yes")){
         System.out.println("How much would you like to deposit initially?");
         double init = console.readDouble();

         BankAccount account = new BankAccount(init);

         System.out.println("Your account is created, what would you like to do? \n 1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
         int option = console.readInt();

         while(option == 1){
             System.out.println(account.getBalance() + " Is your balance. \nWhat would you like to do next?");
             System.out.println("1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
             option = console.readInt();
         }
         while(option == 2){
             System.out.println(account.getID() + " Is your account id.\nWhat would you like to do next?");
             System.out.println("1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
             option = console.readInt();
         }
         while(option == 3){
             System.out.println("How much would you like to withdraw?");
             double withdraw = console.readDouble();

             account.withdraw(withdraw);
             System.out.println("Your new balance is " + account.getBalance() + "\nWhat would you like to do next?");
             System.out.println("1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
             option = console.readInt();
         }
         while(option == 4){
             System.out.println("How much would you like to deposit?");
             double deposit = console.readDouble();

             account.deposit(deposit);

             System.out.println("Your new balance is " + account.getBalance() + "\n what would you like to do next?");
             System.out.println("1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
             option = console.readInt();
         }
     }

 }
}
sirnomnomz
  • 623
  • 1
  • 7
  • 20

3 Answers3

1

Code is calling:

BankAccount account = new BankAccount(init);

which goes to this constructor:

public BankAccount(double initialBalance){
    balance = initialBalance;
}

if you also want to get the other constructor being called (the one that assign the id), you need to change it as follow:

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

Java doesn't call the default constructor if you don't tell it.

Lorenzo Boccaccia
  • 6,041
  • 2
  • 19
  • 29
1

You can simply try this one

private int accountNumber = ++lastAssignedNumber;
private static int lastAssignedNumber;

public BankAccount(){
    balance = 0;

    //No need to increment it anywhere.
    //lastAssignedNumber++;
    //accountNumber = lastAssignedNumber;
}

OR

you can try initialization block

private int accountNumber;
private static int lastAssignedNumber;

//initialization block that will be called for any overloaded constructor also
{
    accountNumber = ++lastAssignedNumber;
}

public BankAccount(){
    balance = 0;

    //No need to increment it anywhere.
    //lastAssignedNumber++;
    //accountNumber = lastAssignedNumber;
}
Braj
  • 46,415
  • 5
  • 60
  • 76
1

You have a disorganized way of constructing your BankAccount objects where whether you get an ID assigned or not depends on which constructor you use. If you rewrite your constructors so that they are chained together, with a primary constructor that takes care of all core responsibilities and a secondary constructor that assigns default values and delegates to the primary one, then initialization will have consistent results.

(The terminology is Scala's, constructor-chaining is mandatory in that language.)

Here the primary constructor would be:

public BankAccount(double initialBalance){
    balance = initialBalance;
    lastAssignedNumber++;
    accountNumber = lastAssignedNumber;
}

and add a secondary constructor:

public BankAccount() {
    this(0);
}

and you will get an id generated regardless of which you call.

(This is similar to Lorenzo's answer, which I upvoted for clearly describing the problem. The difference is that his chaining is going in the other direction, so that the default value gets assigned and then overwritten.)

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276