面向对象(上)综合练习1:银行存取

账户  Account类

 1 package day1_11.exer2;
 2 
 3 //账户
 4 public class Account {
 5     private double balance;//余额
 6 
 7     public Account(double balance) {
 8         this.balance = balance;
 9     }
10     //获取余额
11     public double getBalance() {
12         return balance;
13     }
14 
15     //存钱
16     public void deposit(double amt) {
17         if (amt > 0) {
18             balance += amt;
19             System.out.println("存入" + amt + "成功");
20         }
21     }
22 
23     //取钱
24     public void withdraw(double amt) {
25         if (balance > amt) {
26             balance -= amt;
27             System.out.println("取出" + amt + "成功");
28         }else{
29             System.out.println("取出" + amt + "失败");
30         }
31     }
32 }

 

客户 Customer类

 1 package day1_11.exer2;
 2 //客户
 3 public class Customer {
 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 Customer() {
14     }
15 
16     public String getFirstName() {
17         return firstName;
18     }
19 
20     public String getLastName() {
21         return lastName;
22     }
23 
24     public Account getAccount() {
25         return account;
26     }
27 
28     public void setAccount(Account account) {
29         this.account = account;
30     }
31 }

 

银行 Bank类

 1 package day1_11.exer2;
 2 
 3 public class Bank {
 4     private Customer[] customers;//存放多个客户的数组
 5     private int numberOfCustomer;//记录客户的个数
 6 
 7     public Bank() {
 8         customers = new Customer[10];
 9     }
10 
11     //添加客户
12     public void addCustomer(String f,String l) {
13         Customer customer = new Customer(f, l);
14         //customers[numberOfCustomer] = customer;
15         //numberOfCustomer++;
16         //简单写法
17         customers[numberOfCustomer++] = customer;
18     }
19 
20     //获取客户个数
21     public int getNumberOfCustomer() {
22         return numberOfCustomer;
23     }
24 
25     //获取指定位置上的客户
26     public Customer getCustomer(int index) {
27         //return customers[index]   可能报异常
28         if(index >= 0 && index <numberOfCustomer){
29             return customers[index];
30         }
31 
32         return null;
33     }
34 }

 

 

测试 BankTest

 1 package day1_11.exer2;
 2 
 3 public class BankTest {
 4     public static void main(String[] args) {
 5         Bank bank = new Bank();
 6         bank.addCustomer("Davi", "Smith");
 7         //获取客户的个数
 8         int numberOfCustomer = bank.getNumberOfCustomer();
 9         System.out.println("客户个数:" + numberOfCustomer);
10         //获取指定位置上的客户
11         Customer customer = bank.getCustomer(numberOfCustomer-1);
12         Account account = new Account(2000);
13         customer.setAccount(account);
14         //存钱100
15         customer.getAccount().deposit(100);
16         //查看余额
17         System.out.println("客户:" + customer.getFirstName() + "的账户余额为:" + customer.getAccount().getBalance());
18         //取钱2000
19         customer.getAccount().withdraw(2000);
20         //查看余额
21         System.out.println("客户:" + customer.getFirstName() + "的账户余额为:" + customer.getAccount().getBalance());
22 
23         System.out.println("**********************");
24 
25         bank.addCustomer("慕容","上官");
26         System.out.println("客户个数:" + bank.getNumberOfCustomer());
27         Customer customer2 = bank.getCustomer(bank.getNumberOfCustomer() - 1);
28         customer2.setAccount(new Account(100));
29         //存100
30         customer2.getAccount().deposit(100);
31         //查看余额
32         System.out.println("客户:" + customer2.getFirstName() + "的账户余额为:" + customer2.getAccount().getBalance());
33         //取300
34         customer2.getAccount().withdraw(3000);
35         //查看余额
36         System.out.println("客户:" + customer2.getFirstName() + "的账户余额为:" + customer2.getAccount().getBalance());
37     }
38 }

 

运行结果

 

posted @ 2021-01-11 23:28  dog_IT  阅读(105)  评论(0编辑  收藏  举报