银行业务管理软件(8)

实验旨在通过使用ArrayList改进Bank类和Customer类,以实现银行客户及账户关系的多样性管理。涉及修改Bank类的构造器、addCustomer、getCustomer和getNumOfCustomer方法,以及Customer类的账户管理。TestBanking程序的运行验证了改动的正确性,输出显示了客户及其账户余额。可选优化包括在Bank和Customer类中添加iterator方法,以便更灵活地遍历客户和账户。
摘要由CSDN通过智能技术生成

实验题目 8:
将替换这样的数组代码:这些数组代码用于实现银行和客户间,以及客户与他们
的帐户间的关系的多样性。
实验目的:
使用集合
实验说明:
修改 Bank 类
修改 Bank 类,利用 ArrayList 实现多重的客户关系,不要忘记倒入必须的
java.util类
1. 将 Customer 属性的声明修改为List 类型,不再使用
numberOfCustomers 属性。
2. 修改 Bank 构造器,将 customers 属性的声明修改为List 类型,不再使用
numberOfcustomers 属性
3. 修改 addCustomer 方法,使用 add 方法
4. 修改 getCustomer 方法,使用 get 方法
5. 修改 getNumofCustomer 方法,使用 size 方法
修改 Customer 类 6. 修改 Customer 类,使用 ArrayList 实现多重的账户关系。修改方法同
上。
编译运行 TestBanking 程序
这里,不必修改 CustomerReport 代码,因为并没有改变 Bank 和
Customer 类的接口。编译运行TestBanking
应看到下列输出结果:
" CUSTOMERS REPORT "
“=================”
Customer:Simms,Jane
Savings Account:current balance is
尚硅谷 Java 基础实战—Bank 项目
$500.00 Checking Account:current
balance is $200.00
Customer:Bryant,Owen
Checking Accout:current balance is $200
Customer:Soley,Tim U7
Savings Account:current balance is $1,500.00
Checking Account:current balance is $200.00
Customer:Soley,Tim
Checking Account:current balance is $200.00
Savings Account :current balance is $150.00

Account.java

package banking;

public class Account {
   
    protected double balance ;

    public Account(){
   

    }

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

    public void setBalance(double balance) {
   
        this.balance = balance;
    }

    public double getBalance() {
   
        return balance;
    }

    public boolean deposit(double amt){
   
        if (amt > 0){
   
            balance += amt ;
            return true ;
        }else {
   
            System.out.println("请输入正确的存款数");
            return false ;
        }

    }

    public void withdraw(double amt) throws OverdraftException{
   
        if (balance >= amt){
   
            balance -= amt ;
        } else {
   
            throw new OverdraftException("资金不足", amt - balance) ;
        }
    }
}

Bank.java

package banking;

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

public class Bank {
   
    private List<Customer> customers ;
    //private int numberOfCustomer ;

    private Bank(){
   
        customers = new ArrayList<Customer>() ;
    }
    private static Bank bank = new Bank() ;

    public static Bank getBank(){
   
        return bank ;
    }

    public void addCustomer(String f, String l){
   
        Customer cust= new Customer(f,l) ;
        customers.add(cust) ;
    }

    public int getNumOfCustomers(){
   
        return customers.size() ;
    }

    public Customer getCustomer(int index){
   
        return customers.get(index) ;
    }

    public Iterator<Customer> getCustomers(){
   
        return customers.iterator() ;
    }
}

CheckingAccount.java

package banking;

public class CheckingAccount extends Account{
   
    private Double overdraftProtection ;

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

    public CheckingAccount(double balance, double protect){
   
        super(balance);
        overdraftProtection = protect ;
    }

    public double getOverdraftProtection() {
   
        return overdraftProtection;
    }

    public void setOverdraftProtection(double overdraftProtection) {
   
        this.overdraftProtection = overdraftProtection;
    }

    public void withdraw(double amt) throws OverdraftException{
   
        if (balance >= amt){
   
            balance -= amt ;
        } else{
   
            if(overdraftProtection == null){
   
                throw new OverdraftException("no overdraft exception", amt - balance) ;
            }else if (overdraftProtection <= amt - balance){
   
                throw new OverdraftException("Insuffient funds for overdraft exception", amt - balance - overdraftProtection) ;
            }else {
   
                overdraftProtection 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值