我主要是这样做的:
首先,我用名字,姓氏和号码创建一个新客户。
然后,我创建两个SavingsAccounts,分别是其金额,id和利率。
然后,我将两个SavingsAccounts添加到新客户。
最后,我将新客户添加到银行。
Customer newCustomer = new Customer(firstName, lastName, pnumber);
SavingsAccount savingsAccount1 = new SavingsAccount(400, "1", 4); //400$ into account no.1, with interest 4%
SavingsAccount savingsAccount2 = new SavingsAccount(300, "2", 3);
newCustomer.addAccount(savingsAccount1);
newCustomer.addAccount(savingsAccount2);
bank.addCustomer(newCustomer);
这是班级银行:
public class Bank {
String bankName;
private ArrayList<Customer> customers = new ArrayList<Customer>();
Bank(String bankName) {
this.bankName = bankName;
}
public void addCustomer(Customer newCustomer) {
customers.add(newCustomer);
}
}
这是类客户:
public class Customer {
private String firstName;
private String lastName;
private String number;
private ArrayList<Account> accounts;
Customer(String firstName, String lastName, String number) {
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
this.accounts = new ArrayList<Account>();
}
public void addAccount(SavingsAccount account) {
accounts.add(account);
}
public void addAccount(CreditAccount account) {
accounts.add(account);
}
public ArrayList<Account> getAccounts() {
return accounts;
}
}
这是SavingsAccount类(继承了Account类):
public class SavingsAccount extends Account {
public SavingsAccount() {
super();
}
public SavingsAccount(double bal, String id, double inte) {
super(bal, id, inte);
}
@Override
public void deposit(String number, String id, double amount) {
}
@Override
public void withdraw(String number, String id, double amount) {
}
@Override
public void transfer(String number, String id, double amount) {
}
@Override
public double getBalance() {
}
@Override
public String getAccountId() {
return accountId;
}
@Override
public double getInterest(){
return interest;
}
}
我的问题是:
如何在SavingsAccount类中编写代码以为特定客户和特定帐户存入,提取或转账?
假设我想向1号客户的2号客户存款500。
那应该是诸如SavingsAccount.deposit(“ 2”,“ 1”,500);之类的东西。
我只是想不通如何访问客户2和他的帐户1。
谁能帮我吗?
最佳答案
您可以做的是在Bank类中找到一种方法:
public class Bank {
// Your stuff
// new method:
public boolean transfer(Account accountFrom, double amount, String nameTo, int account) {
//check if the balance can be deposit from the account
if(amount <= accountFrom.getBalance()) {
//Check if the person exists in the bank
String name = nameTo.split(" "); // name[0] is the first name, name[1] last name
boolean success = false;
for(Customer c: customers) {
if(c.getFirstName().equalsIgnoreCase(name[0]) &&
c.getLastName().equalsIgnoreCase(name[1]) {
for(Account a : c.getAccounts()) {
if(a.getAccountId() == account) {
// Add it to the account
a.deposit(amount);
success = true;
break;
}
}
break;
}
}
// Deposit it from the account (That class should only keep track of money, so it
// only takes an argument to deposit or withdraw a value, the rest is done by the bank
// Only do this if money has been dsposited at the target account
if(success){
accountFrom.withdraw(amount);
return true;
}
}
return false;
}
}
为此,您必须从结构上更改您的设置。
如果帐户仅管理资金,则这些帐户将添加到客户中。客户是在银行与其本人之间进行沟通的人。最后,银行与客户沟通。
希望这对您有帮助