2014-09-20 166 views
0

主要我這樣做: 首先,我創建一個名字,姓氏和編號的新客戶。 然後我創建兩個savingsAccounts,其金額,ID和利率。 然後我將這兩個savingsAccounts添加到新客戶。 最後,我將新客戶添加到銀行。從ArrayList獲取特定元素

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(繼承類賬戶):

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. 任何人都可以幫助我嗎?

+0

這個方法爲什麼會在這個類?在'Customer'中看起來更合乎邏輯,因爲它是唯一知道與特定'Customer'相關的所有'Account'的類。 – Dici 2014-09-20 22:34:57

+0

@Dici ok,所以如果我將它放在班級Account中,我如何訪問正確的客戶和帳戶?代碼如何看起來像? – user2939293 2014-09-20 22:39:39

+0

另外,它是存儲所有客戶的銀行類。 – user2939293 2014-09-20 22:41:14

回答

2

你可以做的是在銀行類,一個方法:

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; 
    } 
} 

爲了做到這一點,你必須structurely更改設置。

有帳戶只管理資金,這些帳戶被添加到客戶。顧客是銀行和他自己之間溝通的人。最後,銀行與客戶進行溝通。

我希望這會幫助你在方向

+0

謝謝。我認爲傳輸方式太複雜了。你能否給我一個簡單的方法,比如存款還是取款? 我不明白你上面的解釋,你是什麼意思「這些帳戶被添加到客戶」? – user2939293 2014-09-20 22:57:21

+0

謝謝!感謝您的幫助,我終於找到了工作。 – user2939293 2014-09-21 10:44:25