Java 从ArrayList获取特定元素
我主要是这样做的: 首先,我创建了一个名为、姓为和编号为的新客户。 然后我创建了两个储蓄账户,包括金额、id和利率。 然后,我将两个savingsaccount添加到新客户。 最后,我将新客户添加到银行Java 从ArrayList获取特定元素,java,Java,我主要是这样做的: 首先,我创建了一个名为、姓为和编号为的新客户。 然后我创建了两个储蓄账户,包括金额、id和利率。 然后,我将两个savingsaccount添加到新客户。 最后,我将新客户添加到银行 Customer newCustomer = new Customer(firstName, lastName, pnumber); SavingsAccount savingsAccount1 = new SavingsAccount(400, "1", 4); //400$ int
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);
}
}
我的问题是:
如何在“储蓄帐户”类中编写代码,以便为某个客户、某个帐户存款、取款、转账?
假设我想在2号客户的1号账户上存入500英镑
这应该类似于savingsAccount.deposit(“2”、“1”、500)
我就是不知道如何访问客户2号和他的帐户1号。
有人能帮我吗?你能做的就是在银行类中找到一种方法:
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;
}
}
公共类银行{
//你的东西
//新方法:
公共布尔转账(帐户accountFrom、双倍金额、字符串nameTo、整数帐户){
//检查余额是否可以从账户中存入
如果(amount为什么这个方法会出现在这个类中?在Customer
中似乎更有逻辑,因为它是唯一一个知道所有与特定Customer
@Dici相关的帐户的类,所以如果我把它放在类帐户中,我如何才能访问正确的客户和帐户?代码看起来是什么样子?而且,它是存储所有客户的e bank类。我将这样做-迭代银行客户以查找是否有id==2的客户。如果找到,则迭代该客户的所有帐户以查找编号==1的帐户。如果找到,则对该帐户执行诸如添加或删除钱之类的操作。最好使用映射
在你的银行(除非ID是连续整数)。这样,查找效率会更高。谢谢。我认为转账方法一开始就太复杂了。你能给我一个更简单的方法吗,比如存款或取款?我不明白你上面的解释,你说的是什么意思“这些帐户被添加到客户中”?。
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;
}
}
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;
}
}