java银行程序账户id不涨? [英] java bank program account id not going up?

查看:25
本文介绍了java银行程序账户id不涨?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次创建银行帐户时,帐户 ID 都应加 1,但每次我尝试提取 ID 时,我只会得到帐户 ID 为 0,任何建议,因为我完全按照书中的说明进行操作我正在学习,它仍然没有更新.

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.

帐户构造函数:

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

 }
}

推荐答案

您以一种杂乱无章的方式构建您的 BankAccount 对象,其中是否获得分配的 ID 取决于您使用的构造函数.如果您重写构造函数,使它们链接在一起,主构造函数负责所有核心职责,辅助构造函数负责分配默认值并委托给主构造函数,那么初始化将具有一致的结果.

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.

(该术语是 Scala 的,在该语言中构造函数链是强制性的.)

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

无论你调用哪个,你都会得到一个生成的 id.

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

(这类似于 Lorenzo 的回答,我为清楚地描述了问题而赞成.不同之处在于他的链接正在走向另一个方向,因此默认值被分配然后被覆盖.)

(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.)

这篇关于java银行程序账户id不涨?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆