小编典典

Java银行程序帐户ID不涨?

java

每次开设银行帐户,帐户ID都应加1,但是每次我尝试拉出ID时,我只会得到帐户ID为0,这是任何建议,因为我完全按照从书中学习的方式进行学习而且仍然没有更新。

帐户构造函数:

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

银行账户主程序:

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

 }
}

阅读 262

收藏
2020-11-26

共1个答案

小编典典

您可以采用一种杂乱的方式构造BankAccount对象,其中是否分配ID取决于您使用的构造函数。如果您重写构造函数以便将它们链接在一起,而主要构造函数负责所有核心职责,而次要构造函数将默认值和委托分配给主要构造函数,那么初始化将具有一致的结果。

(术语是Scala的,在该语言中强制使用构造函数链接。)

这里的主要构造函数是:

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

并添加一个辅助构造函数:

public BankAccount() {
    this(0);
}

无论您呼叫哪个,都将生成一个ID。

(这与Lorenzo的答案类似,我为清楚地描述问题而提出投票。不同之处在于,他的链接朝着另一个方向发展,因此默认值被分配然后被覆盖。)

2020-11-26