面向对象(上)综合练习1:Account和Customer

账户 Account类

 1 package day1_11;
 2 //银行账户
 3 public class Account {
 4     private int id; //账号
 5     private double balance;//余款
 6     private double annualInterestRate;//年利率
 7 
 8     public int getId() {
 9         return id;
10     }
11 
12     public void setId(int id) {
13         this.id = id;
14     }
15 
16     public double getBalance() {
17         return balance;
18     }
19 
20     public void setBalance(double balance) {
21         this.balance = balance;
22     }
23 
24     public double getAnnualInterestRate() {
25         return annualInterestRate;
26     }
27 
28     public void setAnnualInterestRate(double annualInterestRate) {
29         this.annualInterestRate = annualInterestRate;
30     }
31 
32     public Account(int id, double balance, double annualInterestRate) {
33         this.id = id;
34         this.balance = balance;
35         this.annualInterestRate = annualInterestRate;
36     }
37 
38     //取钱
39     public void withdraw(double amount) {
40         if (balance < amount) {
41             System.out.println("余额不足,取款失败");
42             return;
43         }
44         balance -= amount;
45         System.out.println("成功取出," + amount);
46     }
47 
48     //存钱
49     public void deposit(double amount) {
50         if (amount > 0) {
51             balance += amount;
52             System.out.println("成功存入," + amount);
53         }
54     }
55 }

客户  Customer类

 1 package day1_11;
 2 
 3 //客户
 4 public class Customer {
 5     private String firstName;//
 6     private String lastName;//
 7     private Account account;//账户
 8 
 9     public Customer() {
10 
11     }
12 
13     public Customer(String firstName, String lastName) {
14         this.firstName = firstName;
15         this.lastName = lastName;
16     }
17 
18     public String getFirstName() {
19         return firstName;
20     }
21 
22     public String getLastName() {
23         return lastName;
24     }
25 
26     public Account getAccount() {
27         return account;
28     }
29 
30     public void setAccount(Account account) {
31         this.account = account;
32     }
33 }

 

测试类 CustomerTest类

 1 package day1_11;
 2 
 3 public class CustomerTest {
 4     public static void main(String[] args) {
 5         Customer customer = new Customer("Jane", "Smith");
 6         Account account = new Account(1001, 2000, 0.0123);
 7         customer.setAccount(account);
 8         //存钱100
 9         customer.getAccount().deposit(100);
10         //取钱960
11         customer.getAccount().withdraw(960);
12         //取钱2000
13         customer.getAccount().withdraw(2000);
14         //打印客户的账户信息
15         System.out.println("Customer [" + customer.getLastName() + "," + customer.getFirstName()
16                             +"] has a account: id is " + account.getId() + ",annualInterestRate is "
17                             + account.getAnnualInterestRate() + ",balance is " + account.getBalance());
18     }
19 }

运行结果

 

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