- 类结构如图所示。
- 将类结构以代码形式表示,并对方法和构造器提供合理的实现;
- 使用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());
}
}
运行截图如下: