image.png
    image.png
    image.png
    image.png

    1. package com.atguigu.exercise3;
    2. public class Account {
    3. //属性
    4. private double balance;
    5. //构造器
    6. public Account(double init_balance){
    7. balance = init_balance;
    8. }
    9. //方法
    10. public double getBalance() {
    11. return balance;
    12. }
    13. //存钱
    14. public void deposit(double amt){
    15. if(amt > 0){
    16. balance += amt;
    17. System.out.println("存钱成功!");
    18. }
    19. }
    20. //取钱
    21. public void withdraw(double amt){
    22. if(balance >= amt){
    23. balance -= amt;
    24. System.out.println("取钱成功!");
    25. }else{
    26. System.out.println("余额不足!");
    27. }
    28. }
    29. }

    1. package com.atguigu.exercise3;
    2. public class Customer {
    3. //属性
    4. private String firstName;
    5. private String lastName;
    6. private Account account;
    7. //构造器
    8. public Customer(String f,String l){
    9. this.firstName = f;
    10. this.lastName = l;
    11. }
    12. //方法
    13. public Account getAccount() {
    14. return account;
    15. }
    16. public void setAccount(Account account) {
    17. this.account = account;
    18. }
    19. public String getFirstName() {
    20. return firstName;
    21. }
    22. public String getLastName() {
    23. return lastName;
    24. }
    25. }

    1. package com.atguigu.exercise3;
    2. public class Bank {
    3. //属性
    4. private Customer[] customers= new Customer[10];
    5. ;//存放多个客户
    6. private int numberOfCustomer;//记录客户的个数
    7. //构造器
    8. public Bank(){
    9. // customers = new Customer[10];
    10. }
    11. //方法
    12. //添加客户
    13. public void addCustomer(String f,String l){
    14. Customer cust = new Customer(f,l);
    15. customers[numberOfCustomer] = cust;
    16. numberOfCustomer++;
    17. }
    18. //获取客户的个数
    19. public int getNumOfCustomers(){
    20. return numberOfCustomer;
    21. }
    22. //获取指定位置上的客户
    23. public Customer getCustomer(int index){
    24. if(index >= 0 && index < numberOfCustomer){
    25. return customers[index];
    26. }
    27. return null;
    28. }
    29. }

    1. package com.atguigu.exercise3;
    2. public class BankTest {
    3. public static void main(String[] args) {
    4. Bank bank = new Bank();
    5. bank.addCustomer("Jane", "Smith");
    6. bank.getCustomer(0).setAccount(new Account(2000));
    7. bank.getCustomer(0).getAccount().withdraw(500);//取钱
    8. double banlance = bank.getCustomer(0).getAccount().getBalance();
    9. System.out.println("客户:"+bank.getCustomer(0).getFirstName()+"的账户余额为:"+banlance);
    10. }
    11. }