Account.java:

public abstract class Account { protected double balance; //账户余额

public Account(double init_balance) {
    balance = init_balance;
}

public double getBalance() {
    return balance;
}

public boolean deposit(double amt) {
    balance += amt;
    return true;
}

public abstract boolean withdraw(double amt); //取款

}

SavingsAccount.java:

public class SavingsAccount extends Account { private double interestRate; //利息

public SavingsAccount(double init_balance, double interest_rate) {
    super(init_balance);
    interestRate = interest_rate;
}

public void setInterestRate(double rate) { //设置利息
    interestRate = rate;
}

public double getInterestRate() { //获取利息
    return interestRate;
}

public void calculateInterest() { //计算利息
    double interest = balance * interestRate;
    balance += interest;
    System.out.println("计算利息后的余额是->" + balance);
}

public boolean withdraw(double amt) { //取款
    if (amt > balance) {
        return false;
    }
    balance -= amt;
    return true;
}

}

CheckingAccount.java:

public class CheckingAccount extends Account { private double overdraftProtection; //透支额度

public CheckingAccount(double init_balance) {
    super(init_balance);
}

public CheckingAccount(double init_balance, double overdraft) {
    super(init_balance);
    overdraftProtection = overdraft;
}

public void setOverdraftProtection(double overdraft) { //设置透支额度
    overdraftProtection = overdraft;
}

public double getOverdraftProtection() { //获取透支额度
    return overdraftProtection;
}

public boolean withdraw(double amt) { //取款
    if (amt <= balance) {
        balance -= amt;
    } else if (amt <= balance + overdraftProtection) {
        overdraftProtection -= (amt - balance);
        balance = 0;
    } else {
        return false;
    }
    System.out.println("取款后的余额是->" + balance);
    System.out.println("下一次允许透支的额度是->" + overdraftProtection);
    return true;
}

}

Customer.java:

import java.util.List; import java.util.ArrayList;

public class Customer { private String firstName; //名字 private String lastName; //姓氏 private List accounts; //账户列表

public Customer(String f, String l) {
    firstName = f;
    lastName = l;
    accounts = new ArrayList<Account>(10); //初始化账户列表
}

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

public void addAccount(Account acct) { //添加账户
    accounts.add(acct);
}

public int getNumberOfAccounts() { //获取账户数量
    return accounts.size();
}

public Account getAccount(int index) { //获取指定索引的账户
    return accounts.get(index);
}

}

Bank.java:

import java.util.List; import java.util.ArrayList;

public class Bank { private static List customers; //客户列表

static { //静态初始化块,初始化客户列表
    customers = new ArrayList<Customer>(10);
}

public static void addCustomer(String f, String l) { //添加客户
    Customer cust = new Customer(f, l);
    customers.add(cust);
}

public static int getNumberOfCustomers() { //获取客户数量
    return customers.size();
}

public static Customer getCustomer(int index) { //获取指定索引的客户
    return customers.get(index);
}

}

TestBanking.java:

public class TestBanking { public static void main(String[] args) { Bank bank = new Bank();

    // Add Customer Jane, Simms
    bank.addCustomer("Jane", "Simms");
    // Add Customer Owen, Bryant
    bank.addCustomer("Owen", "Bryant");
    // Add Customer Tim, Soley
    bank.addCustomer("Tim", "Soley");
    // Add Customer Maria, Soley
    bank.addCustomer("Maria", "Soley");

    for (int i = 0; i < bank.getNumberOfCustomers(); i++) { //遍历所有客户
        Customer customer = bank.getCustomer(i);
        System.out.println(customer.getLastName() + ", " + customer.getFirstName()); //输出客户姓名

        // Add accounts for each customer
        customer.addAccount(new SavingsAccount(5000, 0.05)); //添加储蓄账户
        customer.addAccount(new CheckingAccount(2000, 5000)); //添加支票账户
        customer.addAccount(new CheckingAccount(3000)); //添加支票账户

        for (int j = 0; j < customer.getNumberOfAccounts(); j++) { //遍历客户的所有账户
            Account account = customer.getAccount(j);
            System.out.print("    " + (j + 1) + ":");
            if (account instanceof SavingsAccount) { //判断账户类型
                System.out.print(" 储蓄账户");
            } else if (account instanceof CheckingAccount) {
                System.out.print(" 支票账户");
            }
            System.out.println(" 初始余额是->" + account.getBalance()); //输出账户余额
            account.withdraw(500); //取款
            System.out.println("    " + (j + 1) + ":取款后的余额是->" + account.getBalance()); //输出取款后的余额
            if (account instanceof SavingsAccount) { //判断账户类型
                SavingsAccount savingsAccount = (SavingsAccount) account;
                savingsAccount.calculateInterest(); //计算利息
            } else if (account instanceof CheckingAccount) {
                CheckingAccount checkingAccount = (CheckingAccount) account;
                System.out.println("    " + (j + 1) + ":允许透支的额度是->" + checkingAccount.getOverdraftProtection()); //输出透支额度
                System.out.println("    " + (j + 1) + ":下一次允许透支的额度是->" + checkingAccount.getOverdraftProtection()); //输出透支额度
            }
        }
    }
}
编程要求根据注释在右侧所有出现的Begin至End处补充代码:修改Customer类:将accounts实例变量的声明修改为ListAccount类型使用泛型不再使用numberOfAccounts实例变量;修改构造方法将accounts实例变量初始化为新的ArrayList对象初始容量为10;修改addAccount方法使用List接口的add方法;修改getAccount方法使用List接口的

原文地址: https://www.cveoy.top/t/topic/dprG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录