我在用Java建立一个基本的银行帐户时遇到了一些麻烦。我正在尝试创建一个转移方法,我可以调用它将资金从一个帐户转移到另一个帐户。问题是,我创建的方法有一个不必要的步骤,我必须两次声明对象的名称。要执行transfer方法,如果要将资金从michael转到max,则当前的代码要求我编写michael.acc.transfer(michael, max);。我能把这个简化一点吗?
例如:michael.acc.transfer(max);
下面是我用可以在帐户上执行的方法创建的类:
public class Account {
static double balance;
String accountId;
static int nextId = 0;
static final int ROUTING_NUMBER = 12345;
String bankName;
{
if (ROUTING_NUMBER == 12345) {
bankName = "USA Bank";
}
else {
bankName = "Other bank";
}
}
public void deposit(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
balance = balance - amount;
}
public void transfer (Customer c1, Customer c2) {
double transferAmount;
int routingNumber;
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter transfer amount: ");
transferAmount = input.nextDouble();
System.out.println("Please enter recipient's routing number: ");
routingNumber = input.nextInt();
if (routingNumber == ROUTING_NUMBER) {
System.out.println("Your funds will transfer instantly, you and your recipient share the same bank!");
System.out.println("Bank name: " + bankName);
} else {
System.out.println("Your funds will transfer in 2-3 business days.");
}
}
c1.customerBalance -= transferAmount;
c2.customerBalance += transferAmount;
}
public static String getNextId() {
return "ACCT #" + nextId++;
}下一个类是我用创建客户的方法创建的类:
public class Customer {
public String firstName;
public String lastName;
public Account acc;
public double customerBalance = acc.balance;
int defaultBalance = 100;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Customer() {
firstName = "John";
lastName = "Doe";
}
public void addAccount(double initialBalance) {
acc = new Account();
acc.accountId = "ACCT ID: " + Account.getNextId();
customerBalance = initialBalance;
}
public void addAccount() {
addAccount(100);
}
}以下是我为将资金从一个转到另一个账户而设立的两个账户:
public class Bank {
public static void main(String[] args) {
Customer max = new Customer("Max", "Doe");
max.addAccount(1500);
Customer michael = new Customer("Michael", "Smith");
michael.addAccount(3000);
}
}发布于 2017-12-27 11:47:52
我看不出为什么您必须将这两个客户传递给您transfer方法。但首先,有一些主要的意见:
Customer还是Account转过来?通常,我从不同的帐户转帐,而不是客户。因此,让我们假设您想要的是一种方法,将给定金额的钱从一个帐户转移到另一个帐户。因此,您将有一个类似于Account.transfer(double amount, Account recipient)的方法
@Test
public void transfer_must_move_amount() {
Account michael = new Account();
Account max = new Account();
michael.transfer(15, max);
assertThat(max.getBalance()).isEqualTo(15);
}您的代码只需稍微重构一下:
public void transfer(double amount, Account recipient) {
double transferAmount = amount;
int routingNumber = recipient.ROUTING_NUMBER;
if (routingNumber == ROUTING_NUMBER) {
System.out.println("Your funds will transfer instantly, you and your recipient share the same bank!");
System.out.println("Bank name: " + bankName);
} else {
System.out.println("Your funds will transfer in 2-3 business days.");
}
this.balance -= transferAmount;
recipient.balance += transferAmount;
}此测试将失败,预期为: 15.0,实际: 0.0。这是因为您的balance是静态的,static成员属于这个类,而不是一个Account。把它拿开一切都会好起来的。
但是在您的代码上还有很多可以改进的地方。
从外部保护Account#balance属性;使其为private,并将其初始化为0以确保。
通过这样做,您的Customer将是红色的,因为acc.balance不再是avlid了。这是一件好事,因为通过复制值,如果您在您的balance中更改了Account,那么客户就不会再改变了。删除它并用getBalance()方法替换它,该方法将从Account返回余额。
由于您有一个addAccount方法,所以我认为客户可能有许多帐户,所以将Account acc字段替换为Account的集合。getBalance将返回所有帐户的和。
返回到Account,其中您的两个deposit和withdraw方法未使用。您可以通过您的转移方法使用它们。如果一些Account对这些操作有一些费用,这可能会保证您以后的安全。您只需重写这些方法,传输方法就不会被触及。
我们唯一没有接触到的东西是ROUTING_NUMBER。通常这些信息是您帐号的一部分。因此,现在可能是添加AccountNumber类的好时机。这是一个值对象,它强制执行某些规则,可以用来验证两个帐户是否在同一家银行。
--我忽略了这次审查中的nextId和bankName,因为它们超出了你们的“转移”范围。
https://github.com/gervaisb/stackexchange-codereview/tree/q183675
https://codereview.stackexchange.com/questions/183675
复制相似问题