Account:
package banking4; public class Account { private double balance; public Account(double int_balance) { balance = int_balance; } public double getBlance() { return balance; } public boolean deposit(double amt) { balance += amt; return true; } public boolean withdraw(double amt) { if (balance >= amt) { balance -= amt; return true; } else { System.out.println("余额不足"); return false; } } }
Customer:
package banking4; public class Customer { private String firstName; private String lastName; private Account account; public Customer(String f, String l) { firstName = f; lastName = l; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public Account getAccount() { return account; } public void setAccount(Account acct) { account = acct; } }
Bank:
package banking4; public class Bank { private Customer[] customers;// 用于存放客户的 private int numberOfCustomers;// 记录Customer的个数 public Bank() { customers = new Customer[5]; } // 添加一个Customer到数组中 public void addCustomer(String f, String l) { Customer cust = new Customer(f, l); customers[numberOfCustomers] = cust; numberOfCustomers++; } // 获取Customer的个数 public int getNumberofCustomer() { return numberOfCustomers; } // f返回指定索引位置Customer public Customer getCustomer(int index) { return customers[index]; } }
TestBanking4:
package TestBanking; /* * This class creates the program to test the banking classes. * It creates a new Bank, sets the Customer (with an initial balance), * and performs a series of transactions with the Account object. */ import banking4.*; public class TestBanking4 { public static void main(String[] args) { Bank bank = new Bank(); // Add Customer Jane, Simms bank.addCustomer("Jane", "Simms"); //code //Add Customer Owen, Bryant bank.addCustomer("Owen", "Bryant"); //code // Add Customer Tim, Soley bank.addCustomer("Tim", "Soley"); //code // Add Customer Maria, Soley bank.addCustomer("Maria", "Soley"); //code for ( int i = 0; i < bank.getNumberofCustomer(); i++ ) { Customer customer = bank.getCustomer(i); System.out.println("Customer [" + (i+1) + "] is " + customer.getLastName() + ", " + customer.getFirstName()); } } }
输出结果:
Customer [1] is Simms, Jane
Customer [2] is Bryant, Owen
Customer [3] is Soley, Tim
Customer [4] is Soley, Maria