gistfile1.txt package experiment3;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class TestBanking {public static void main(String[] args) {Bank bank = new Bank();bank.addCustomer("Simms", "Jane");bank.add
package experiment3; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class TestBanking { public static void main(String[] args) { Bank bank = new Bank(); bank.addCustomer("Simms", "Jane"); bank.addCustomer("Bryant", "Owen"); bank.addCustomer("Soley", "Tim"); bank.addCustomer("Soley", "Maria"); SavingsAccount saving = new SavingsAccount(500.00, 0.05); CheckingAccount checking = new CheckingAccount(200.00, true, 400.00); bank.getCustomer(1).addAccount(saving); bank.getCustomer(1).addAccount(checking); checking = new CheckingAccount(200.00); bank.getCustomer(2).addAccount(checking); saving = new SavingsAccount(1500.00, 0.05); checking = new CheckingAccount(200.00, true, 400.00); bank.getCustomer(3).addAccount(saving); bank.getCustomer(3).addAccount(checking); saving = new SavingsAccount(150.00, 0.05); bank.getCustomer(4).addAccount(saving); bank.getCustomer(4).addAccount(checking); CustomerReport cr = new CustomerReport(); cr.generateReport(bank); } } /* * Account类是抽象类,有一个balance属性,代表余额。deposit方法表示存款,amount参数是存款额。withdraw方法表示取款, * 取款额amount * 如果超出了余额就会抛出透支异常,我们需要自己定义一个OverdraftException类来表示这个异常,当抛出透支异常时,不进行取款交易, * 并报告用户此项错误。 */ abstract class Account { protected double balance;// 代表余额 Account(double intBalance) { this.balance = intBalance; } protected double getBalance() { return balance; } protected void setBalance(double newBalance) { this.balance = newBalance; } protected void deposit(double amount) {// 存款 balance += amount; } protected void withdraw(double amount) {// 取款 try { if (amount <= balance) { balance -= amount; } else { balance = amount - balance; throw new OverdraftException(balance); } } catch (OverdraftException e) { System.out.println("余额不足,与目标金额相差" + e.getamount()); } } } /* * Customer类有一个accounts集合,用来存储某个储户的所有Account(帐号)对象,addAccount方法用于向该集合中加帐号, * getAccount方法根据下标值取该储户的某个帐号,getNumOfAccounts方法取该储户的帐号总数, * getAccounts方法返回该储户的帐号的Iterator, 以便获得每个帐号对象。 */ class Customer { protected String firstName; protected String lastName; List account = new ArrayList ();; Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } protected void addAccount(Account Acct) { account.add(Acct); } protected Account getAccount(int Account_index) { // return account.get(Account_index); List list = new ArrayList (); Iterator it = account.iterator(); while (it.hasNext()) { Account temp = it.next(); list.add(temp); } return list.get(Account_index - 1); } protected int getNumOfAccounts() { // return account.size(); List list = new ArrayList (); Iterator it = account.iterator(); while (it.hasNext()) { Account temp = it.next(); list.add(temp); } return list.size(); } protected Iterator getAccounts() { // return account.iterator(); Iterator it = account.iterator(); return it; } } /* * Bank类中有一个customers集合,用来存储所有的Customer(储户)对象,addCustomer方法用于向该集合中加储户, * getCustomer方法根据下标值取某个储户,getNumOfCustomers方法取储户总数,getCustomers方法返回储户的Iterator, * 以便获得每个储户对象。 */ class Bank { ListB8~IB9A5BJ_ZBQFM%(FDPBW.pngcustomers = new ArrayList (); Bank() { } protected void addCustomer(String firstName, String lastName) { // addCustomer方法用于向该集合中加储户 Customer cust = new Customer(firstName, lastName); customers.add(cust); } protected Customer getCustomer(int customer_index) { // return customers.get(customer_index); List list = new ArrayList (); Iterator it = customers.iterator(); while (it.hasNext()) { Customer temp = it.next(); list.add(temp); } return list.get(customer_index - 1); } protected int getNumOfCustomers() { // return customers.size(); List list = new ArrayList (); Iterator it = customers.iterator(); while (it.hasNext()) { Customer temp = it.next(); list.add(temp); } return list.size(); } protected Iterator getCustomers() { // return customers.iterator(); Iterator it = customers.iterator(); return it; } } /* SavingsAccount类表示存款帐号,继承Account,新增一个属性interestRate, 代表利率。 */ class SavingsAccount extends Account { double interestRate;// 代表利率 SavingsAccount(double intBalance, double interestRate) { super(intBalance); this.interestRate = interestRate; } } /* * CheckingAccount类表示大额存款帐号,也继承Account,它有一个叫canOverdraft的属性,是一个boolean值, * 代表该帐号能否透支(true-能,false-否);它还有一个叫maxOverdraft的属性,表示该帐号允许的最大透支额。 * 这个类的withdraw(取款)方法需要考虑的因素比较多:在发生透支时,如果帐号不允许透支(canOverdraft=false), * 则抛出OverdraftException异常并退出交易;如果允许透支(canOverdraft=true), * 但透支额(amount-balance)超过最大透支额的话,也抛出OverdraftException异常并退出交易; * 只有在不发生透支或透支额小于最大透支额时交易才能正常进行。另外,在每次进行透支交易时,最大透支额(maxOverdraft)应做调整, * 以便使该帐号的最大透支额随透支次数的增加而不断减少,从而可以避免透支的滥用,阻止信用膨胀。CheckingAccount类有两个构造方法, * 只带一个参数的构造方法用来初始化balance,同时设定canOverdraft=false,maxOverdraft=0.00。 */ class CheckingAccount extends Account { protected boolean canOverdraft;// 代表该帐号能否透支 protected double maxOverdraft;// 表示该帐号允许的最大透支额 CheckingAccount(double intBalance) { super(intBalance); canOverdraft = false; maxOverdraft = 0.00; } CheckingAccount(double intBalance, boolean canOverdraft, double maxOverdraft) { super(intBalance); this.canOverdraft = canOverdraft; this.maxOverdraft = maxOverdraft; } protected void withdraw(double intBalance) { if (intBalance > super.balance) { try { if (canOverdraft = false) { intBalance = intBalance - super.balance; throw new OverdraftException(intBalance); } } catch (OverdraftException e) { System.out.println("余额不足,与目标金额相差" + e.getamount()); } if (canOverdraft = true) { intBalance = intBalance - super.balance; try { if (intBalance > maxOverdraft) { throw new OverdraftException(intBalance); } else { maxOverdraft = maxOverdraft - intBalance; } } catch (OverdraftException e) { System.out.println(e.getamount() + "超过最大透支金额"); } } } } } /* CustomerReport类用来显示每个储户的姓名以及他所持有的帐号的类别和余额,以报表的形式输出。 */ class CustomerReport { void generateReport(Bank bank) { System.out.println("CUSTOMERS REPORT"); System.out.println("=================="); for (Customer customer : bank.customers) { System.out.println("储户姓名:" + customer.lastName + " " + customer.firstName); for (Account accut : customer.account) { System.out.println(accut.getClass().getSimpleName() + ":当前余额是¥" + accut.getBalance()); } System.out.println(); } } } class OverdraftException extends Exception { double amount; public OverdraftException(double amount) { this.amount = amount; } public double getamount() { return amount; } }