gpt4 book ai didi

java - 从ArrayList中获取特定元素

转载 作者:行者123 更新时间:2023-12-02 05:26:35 26 4
gpt4 key购买 nike

我主要这样做:首先,我使用名字、姓氏和号码创建一个新客户。然后我创建两个 savingAccounts,及其金额、id 和利率。然后,我将两个 savingAccounts 添加到新客户。最后,我将新客户添加到银行。

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 类中编写代码来为特定客户的特定帐户存款、取款、转账?假设我想在 2 号客户的 1 号账户上存入 500 美元。

应该类似于 savingAccount.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;
}
}

要实现这一点,您必须从结构上改变您的设置。

让帐户仅管理资金,这些帐户将被添加到客户。客户是银行和他自己之间沟通的人。最后是银行与客户沟通。

希望这对您有所帮助

关于java - 从ArrayList中获取特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953902/

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