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();
}
}
}
}