java - 为什么我的工作没有显示我的转移工作
问题描述
文件 Account.java 包含一个简单银行帐户类的定义,该类具有提款、存款、获取余额和帐号以及打印摘要的方法。编写以下附加代码:
添加方法
public void transfer(Account acct, double amount)
到 Account 类,该类允许用户将资金从一个银行账户转移到另一个银行账户。如果 acct1 和 acct2 是 Account 对象,那么方法调用 acct1.transfer(acct2,957.80) 应该将 $957.80 从 acct1 转移到 acct2。一定要清楚地记录转移的方式!
- 编写一个带有 main 方法的类 TransferTest,该方法创建两个银行帐户对象并进入执行以下操作的循环:
询问用户是想从 account1 转移到 account2,从 account2 转移到 account1,还是退出。如果选择转账,询问转账金额,执行操作,并打印每个账户的新余额。重复直到用户要求退出,然后打印每个帐户的摘要。
向 Account 类添加一个静态方法,该方法允许用户在两个帐户之间转移资金,而无需通过任何一个帐户。您可以(并且应该)像调用另一个方法一样调用方法转移 - 您正在重载此方法。您的新方法应采用两个 Account 对象和一个金额,并将金额从第一个帐户转移到第二个帐户。签名将如下所示:
public static void transfer(Account acct1, Account acct2, double amount)
修改您的 TransferTest 类以使用静态传输而不是实例版本。
public class Account {
//instance variables
private long accountNum; //bank account number
private String name; //name
private double balance; //balance
//Constructor
public Account () {
accountNum = 0;
name = "Nobody";
balance = 0.0;
}
public Account (long accountNum, String name, double balance) {
this.accountNum = accountNum;
this.name = name;
this.balance = balance;
}
//Accessor (get) methods
public long getAccountNum() {
return accountNum;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
//Mutator (set) methods
public void setAccountNum (long newAccountNum) {
this.accountNum = newAccountNum;
}
public void setName (String newName)
{
this.name = newName;
}
public void deposit (double amount) {
if (amount >= 0) //enough money to withdraw
this.balance += amount;
}
public void withdraw (double amount) {
if (amount >= 0 && balance >= amount)
this.balance -= amount;
else {
System.out.println("Insufficient payment");
}
}
public void transfer(Account acct, double amount) {
//amount= 957.80;
if(this.balance >= amount) {
acct.balance += amount;
this.balance-=amount;
System.out.println(amount + "successfully transfered from account1 to account 2" );
}
}
public String toString() {
return "Account Number: " + this.accountNum + "\nAccount Name: " + this.name +
"\nBalance: " + this.balance;
}
public boolean equals(Account otherAccount) {
return this.accountNum == otherAccount.accountNum &&
this.name.equalsIgnoreCase(otherAccount.name) &&
(Math.abs(this.balance - otherAccount.balance) <= 0.001);
}
//Transfer from acct1 to acct2
public static void transfer(Account acct1, Account acct2, double amount) {
acct1.transfer(acct2, amount);
}
}
解决方案
String confirm;
boolean done = false;
double amount1 = 0;
int choice;
while (true) {
System.out.println("Would you like to: " + "\n (1) Transfer money from account # "+acct1.getAccountNum()+
" to account # " +acct2.getAccountNum()+ "\n (2) Transfer money from account # " +acct2.getAccountNum()+
" to account # "+ acct1.getAccountNum()+ "\n (3) Quit");
if(scan.hasNextInt()) {
choice= scan.nextInt();
scan.nextLine();
if (choice==1|| choice ==2) {
System.out.println("How much amount would you like to transfer? " );
amount1=scan.nextDouble();
scan.nextLine();
}
if(choice==1) {
Account.transfer(acct1, acct2, amount1);
}
else if (choice==2) {
acct2.transfer(acct1, amount1);
}
else {
System.out.println("Quit \n Bye have a great day ");
}
}
}
}
}
推荐阅读
- assembly - YASM [symbol+$$] 有效地址在平面二进制中太复杂了
- python - 如何编写正确排序的查询
- c - 如何管道不同的进程?(C)
- javascript - 页面更改但看到旧页面
- php - Laravel phpunit 测试嵌套预加载
- python - 与 cross_validate 和迭代 Kfolds 不同的 RMSE
- android - 当 DATABASE_VERSION 确实增加时,没有调用 SQLite onUpgrade
- laravel - Laravel 捕捉到任何带前缀的路由?
- scala - 更改 M-tree 中的最大节点容量会影响结果
- macos - 如何在 vscode Mac 中禁用这个烦人的弹出窗口