[英]Get specific element from ArrayList
我主要是這樣做的:首先,我用名字,姓氏和號碼創建一個新客戶。 然后,我創建兩個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;
}
}
為此,您必須從結構上更改您的設置。
如果帳戶僅管理資金,則這些帳戶將添加到客戶中。 客戶是在銀行與其本人之間進行溝通的人。 最后,銀行與客戶溝通。
希望這對您有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.