- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
每次创建银行账户时,账户 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/
我正在创建一个在线学习平台,我们在其中使用 Moodle 输入和存储问题。我需要创建一个包装器,使用它我可以从我的应用程序访问 Moodle 的问题库。最好的方法是什么:插件还是 Web 服务?已经有
我有一个银行线程和 4 个 ATM 机 .txt 文件。(atm_0_input_file.txt - atm_4_input_file.txt)。 每个文件都是一个线程,一个bank也是一个线程。当
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
当我使用“exchange_to (: CLP) .to_i”方法时,它在 OrdersController#create 中给我错误 Money::Bank::GoogleCurrencyFetch
您好,我对数据库的“用户”和从数据库访问其详细信息的企业“客户”感到困惑。 我正在构建一个网络,客户可以在其中登录并访问他们的银行余额、对账单、直接借记等(银行应用程序)。我的 SQL 数据库将有一个
我正在使用 Monzo 银行 API 开发一个金融应用程序,身份验证过程最终会从 Monzo api 接收 access_token:https://docs.monzo.com/#acquire-a
我正在设计银行 ATM 消息处理/路由框架,需要一些帮助来完成技术和架构。交易来自多个合作银行的 ATM,比如目前我们为 5 到 6 家银行提供服务,每家银行不超过 10 台 ATMS。 消息通过 t
我是第一次使用 Stripe,对它们提供的不同 API 有点困惑。有一个 Payment Method API,它是推荐用于处理客户付款方式的 API,但目前它只支持信用卡,如果我理解正确的话....
我是一名优秀的程序员,十分优秀!