image.png
    image.png

    1. package com.atguigu.exercise2;
    2. public class CustomerTest {
    3. public static void main(String[] args) {
    4. Customer cust = new Customer("Jane","Smith");
    5. Account acct = new Account(1000,2000,0.0123);
    6. cust.setAccount(acct);
    7. //存取操作
    8. //cust.getAccount();
    9. acct.deposit(100);//存钱
    10. acct.withdraw(960);//取钱
    11. acct.withdraw(2000);//取钱
    12. }
    13. }

    1. package com.atguigu.exercise2;
    2. public class Account {
    3. //属性
    4. private int id;//账号
    5. private double balance;//余额
    6. private double annuallnterestRate;//年利率
    7. //构造器
    8. public Account(int id, double balance, double annuallnterestRate){
    9. this.id = id;
    10. this.balance = balance;
    11. this.annuallnterestRate = annuallnterestRate;
    12. }
    13. //方法
    14. public int getId() {
    15. return id;
    16. }
    17. public double getBalance() {
    18. return balance;
    19. }
    20. public double getAnnuallnterestRate() {
    21. return annuallnterestRate;
    22. }
    23. public void setId(int id) {
    24. this.id = id;
    25. }
    26. public void setBalance(double balance) {
    27. this.balance = balance;
    28. }
    29. public void setAnnuallnterestRate(double annuallnterestRate) {
    30. this.annuallnterestRate = annuallnterestRate;
    31. }
    32. public void withdraw(double amount){ //取钱
    33. if(balance < amount){
    34. System.out.println("余额不足,取款失败!");
    35. return;
    36. }
    37. balance -= amount;
    38. System.out.println("成功取出:"+amount);
    39. }
    40. public void deposit(double amount){ //存钱
    41. if(amount > 0){
    42. balance += amount;
    43. System.out.println("成功存入:" + amount);
    44. }else{
    45. System.out.println("存款失败!");
    46. }
    47. }
    48. }

    1. package com.atguigu.exercise2;
    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 String getfirstName(){
    14. return firstName;
    15. }
    16. public String getlastName(){
    17. return lastName;
    18. }
    19. public Account getAccount(){
    20. return account;
    21. }
    22. public void setAccount(Account account){
    23. }
    24. }