class BankTest
package cn.sxt.arrays.ever;
public class BankTest {
public static void main(String[] args) {
Bank bank=new Bank();
bank.addCustomer("Jane","Smith");
bank.getCustomer(0).setAccount(new Account(2000));
bank.getCustomer(0).getAccount().withdraw(500);
double balance=bank.getCustomer(0).getAccount().getBalance();
System.out.println("客户"+bank.getCustomer(0).getFirstName()+"的账户余额为:" +balance);
System.out.println(",,,,,,,,,,,,,,,,,,,");
bank.addCustomer("万里", "杨");
System.out.println("银行客户的个数为:"+bank.numberOfCustomers());
}
}
class Bank
package cn.sxt.arrays.ever;
public class Bank {
private Custemer[] customers;
private int numberOfCustomers;
public Bank() {
customers=new Custemer[10];
}
public void addCustomer(String f,String l) {
Custemer cust=new Custemer(f,l);
customers[numberOfCustomers]=cust;
numberOfCustomers++;
}
public int numberOfCustomers() {
return numberOfCustomers;
}
public Custemer getCustomer(int index) {
if(index>=0&&index<=numberOfCustomers) {
return customers[index];
}
return null;
}
}
class Account
package cn.sxt.arrays.ever;
public class Account {
private double balance;
public Account(double init_balance) {
this.balance=init_balance;
}
public double getBalance() {
return balance;
}
public void depsoit(double amt) {
if(amt>0) {
balance +=amt;
System.out.println("存钱成功");
}
}
public void withdraw(double amt) {
if(balance>=amt) {
balance-=amt;
System.out.println("取钱成功");
}
else {
System.out.println("余额不足");
}
}
}
class Custemer
package cn.sxt.arrays.ever;
public class Custemer {
private String firstName;
private String lastName;
private Account account;
public Custemer(String f, String l) {
this.firstName = f;
this.lastName = l;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account2) {
this.account = account2;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
结果: