gpt4 book ai didi

Java 银行程序帐户 ID 没有上升?

转载 作者:行者123 更新时间:2023-11-29 05:24:26 26 4
gpt4 key购买 nike

每次创建银行账户时,账户 ID 都应该加一,但每次我尝试提取 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();
}
}

}
}

最佳答案

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

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

这里的主要构造函数是:

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

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

public BankAccount() {
this(0);
}

无论您调用哪个,您都将获得一个生成的 ID。

(这类似于 Lorenzo's answer ,我赞成它清楚地描述了问题。不同之处在于他的链接是在另一个方向上进行的,因此默认值被分配然后被覆盖。)

关于Java 银行程序帐户 ID 没有上升?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23252646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com