第六次 java实验 紫金学院 泛型02

  1. 类结构如图所示。

​​

  • 将类结构以代码形式表示,并对方法和构造器提供合理的实现;
  • 使用TestBanking类对代码进行测试。

1.创建类 Account        按要求创建四个方法

package test06;

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

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

    public boolean withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            return true;
        } 
        else {
        	return false;
        }
    }

    public double getBalance() {
        return balance;
    }
}

2.创建类 SavingAccount        按要求创两个方法

package test06;

public class SavingAccount extends Account {
    private double rate;

    public SavingAccount(double balance, double rate) {
        super();
        this.rate = rate;
    }
}

 3.创建类 CheckingAccount    按要求创两个方法

package test06;

public class CheckingAccount extends Account {
    double overdraft;
    public CheckingAccount(double balance, double overdraft) {
        super();
        this.overdraft = overdraft;
    }

    public CheckingAccount(double balance) {
        this(balance, 0.0);
    }

    public boolean withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            return true;
        } else if (amount <= balance + overdraft) {
            overdraft -= (amount - balance);
            balance = 0;
            return true;
        } else return false;
    }

    public double getOverdraft() {
        return overdraft;
    }
}

4.创建类 Bank    

package test06;

import java.util.HashMap;

public class Bank {
    private HashMap customers;
    private int custNum;

    public Bank() {
        customers = new HashMap();
        custNum = 0;
    }

    public void addCustomer(Customer customer) {
        customers.put(new Integer(custNum + 1), customer);
        custNum++;
    }

    public Customer getCustomer(int index) {
        if (index >= 1 && index <= custNum) {
        	return (Customer) (customers.get(new Integer(index)));
        }
        else {
        	return null;
        }
    }

    public HashMap getAllCustomer() {
        return customers;
    }

    public int getCustomerNum() {
        return custNum;
    }
}

5.创建类 Customer

package test06;

import java.util.ArrayList;

public class Customer {
    private String name;
    private ArrayList accounts = new ArrayList();

    public Customer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void addAccount(Account account) {
        accounts.add(account);
    }

    public Account getAccount(int index) {
        return (Account) (accounts.get(index - 1));
    }

    public ArrayList getAllAccount() {
        return accounts;
    }
}

6.创建test

package test06;

public class TestBanking {
    public static void main(String[] args) {
        Bank bank = new Bank();
        bank.addCustomer(new Customer("ZhaoLong"));
        bank.addCustomer(new Customer("ZJJ"));
        Customer cust1 = bank.getCustomer(1);
        Customer cust2 = bank.getCustomer(2);
        cust1.addAccount(new SavingAccount(999999.0, 2.1));
        cust1.addAccount(new CheckingAccount(999999.0, 1.0));
        cust2.addAccount(new CheckingAccount(999999.0, 20.0));
        Account acc1 = cust1.getAccount(1);
        System.out.println(cust1.getName() + "的余额为:" + acc1.getBalance());
        acc1.deposit(88888.0);
        System.out.println(cust1.getName() + "的余额为: " + acc1.getBalance());
        acc1.withdraw(99999.0);
        System.out.println(cust1.getName() + "的余额为:" + acc1.getBalance());
        acc1.withdraw(99999.0);
        System.out.println(cust1.getName() + "'的余额为:" + acc1.getBalance());
        CheckingAccount acc2 = (CheckingAccount) cust1.getAccount(2);
        System.out.println(cust1.getName() + "的余额为: " + acc2.getBalance() + " 透支是: " + acc2.getOverdraft());
        acc2.deposit(88888.0);
        System.out.println(cust1.getName() + "的余额为:" + acc2.getBalance() + " 透支是: " + acc2.getOverdraft());
        acc2.withdraw(99999.0);
        System.out.println(cust1.getName() + "的余额为:" + acc2.getBalance() + " 透支是:" + acc2.getOverdraft());
        acc2.withdraw(99909.0);
        System.out.println(cust1.getName() + "的余额为:" + acc2.getBalance() + " 透支是: " + acc2.getOverdraft());
        acc2.withdraw(90000.0);
        System.out.println(cust2.getName() + "的余额为:" + acc2.getBalance() + " 透支是: " + acc2.getOverdraft());
        CheckingAccount acc3 = (CheckingAccount) cust2.getAccount(1);
        System.out.println(cust2.getName() + "的余额为: " + acc3.getBalance() + " 透支是:" + acc3.getOverdraft());
        acc3.deposit(99000.0);
        System.out.println(cust2.getName() + "的余额为: " + acc3.getBalance() + " 透支是: " + acc3.getOverdraft());
        acc3.withdraw(20000.0);
        System.out.println(cust2.getName() + "的余额为:" + acc3.getBalance() + " 透支是: " + acc3.getOverdraft());
        acc3.withdraw(3000.0);
        System.out.println(cust2.getName() + "的余额为:" + acc3.getBalance() + " 透支是: " + acc3.getOverdraft());
        acc3.withdraw(10000.0);
        System.out.println(cust2.getName() + "的余额为:" + acc3.getBalance() + " 透支是: " + acc3.getOverdraft());
    }
}

 运行截图如下:

 

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵相机~

谢谢你,调试、讲解私聊我~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值