从ArrayList获取特定元素

时间:2014-09-20 22:29:58

标签: java

主要是我这样做: 首先,我创建一个名字,姓氏和号码的新客户。 然后我创建两个SavingAccounts,其金额,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(&#34; 2&#34;,&#34; 1&#34;,500);

我无法弄清楚如何访问2号客户及其帐号1。 有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:2)

你可以做的是在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;
  }
}

为此,您必须从结构上改变您的设置。

让帐户只管理资金,这些帐户会添加到客户手中。客户是银行与他自己之间进行通信的人。最后银行与客户沟通。

我希望这会对你有所帮助