Account类

在提款方法 an中,需要判断用户余额是否能够满足提款数额的要求,如果不能, 应给出提示。
deposit()方法表示存款。

Customer类

Bank类

  • addCustomer 方法必须依照参数(姓 , 名)构造一个新的 Customer 对象,然后把 它放到 customer 数组中。还必须把 numberOfCustomer 属性的值加 1。
  • getNumOfCustomers 方法返回 numberofCustomers 属性值。
  • getCustomer 方法返回与给出的 index 参数相关的客户。

创建 BankTest 类进行测试

实现代码参考

1. Account

  1. package pers.zyx.accountcustomerbank;
  2. public class Account {
  3. private double balance;
  4. public Account(double initBalance){
  5. this.balance = initBalance;
  6. }
  7. public double getBalance(){
  8. return balance;
  9. }
  10. // 存钱
  11. public void deposit(double amt){
  12. if(amt > 0){
  13. balance += amt;
  14. System.out.println("存钱成功");
  15. }
  16. }
  17. // 取钱
  18. public void withdraw(double amt){
  19. if(balance >= amt){
  20. balance -= amt;
  21. System.out.println("取钱成功");
  22. }else{
  23. System.out.println("余额不足");
  24. }
  25. }
  26. }

2. Customer

  1. package pers.zyx.accountcustomerbank;
  2. public class Customer {
  3. private String firstName;
  4. private String lastName;
  5. private Account account;
  6. public Customer(String f , String l){
  7. this.firstName = f;
  8. this.lastName = l;
  9. }
  10. public String getFirstName() {
  11. return firstName;
  12. }
  13. public String getLastName() {
  14. return lastName;
  15. }
  16. public Account getAccount() {
  17. return account;
  18. }
  19. public void setAccount(Account account) {
  20. this.account = account;
  21. }
  22. }

3. Bank

  1. package pers.zyx.accountcustomerbank;
  2. public class Bank {
  3. private Customer[] customer;
  4. private int numberOfCustomers;
  5. public Bank(){
  6. customer = new Customer[100];
  7. }
  8. // 添加客户
  9. public void addCustomer(String f , String l){
  10. Customer cust = new Customer(f , l);
  11. customer[numberOfCustomers++] = cust;
  12. }
  13. // 获取客户个数
  14. public int getNumOfCustomers(){
  15. return numberOfCustomers;
  16. }
  17. // 获取指定位置上的客户
  18. public Customer getCustomer(int index){
  19. if(index >= 0 && index < numberOfCustomers){
  20. return customer[index];
  21. }
  22. else{
  23. return null;
  24. }
  25. }
  26. }

4. BankTest

  1. package pers.zyx.accountcustomerbank;
  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 balance = bank.getCustomer(0).getAccount().getBalance();
  9. System.out.println("Balance = " + balance);
  10. }
  11. }

备注