package com.atguigu.exercise3;
public class Account {
//属性
private double balance;
//构造器
public Account(double init_balance){
balance = init_balance;
}
//方法
public double getBalance() {
return balance;
}
//存钱
public void deposit(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("余额不足!");
}
}
}
package com.atguigu.exercise3;
public class Customer {
//属性
private String firstName;
private String lastName;
private Account account;
//构造器
public Customer(String f,String l){
this.firstName = f;
this.lastName = l;
}
//方法
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
package com.atguigu.exercise3;
public class Bank {
//属性
private Customer[] customers= new Customer[10];
;//存放多个客户
private int numberOfCustomer;//记录客户的个数
//构造器
public Bank(){
// customers = new Customer[10];
}
//方法
//添加客户
public void addCustomer(String f,String l){
Customer cust = new Customer(f,l);
customers[numberOfCustomer] = cust;
numberOfCustomer++;
}
//获取客户的个数
public int getNumOfCustomers(){
return numberOfCustomer;
}
//获取指定位置上的客户
public Customer getCustomer(int index){
if(index >= 0 && index < numberOfCustomer){
return customers[index];
}
return null;
}
}
package com.atguigu.exercise3;
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 banlance = bank.getCustomer(0).getAccount().getBalance();
System.out.println("客户:"+bank.getCustomer(0).getFirstName()+"的账户余额为:"+banlance);
}
}