Account类
package banking5;
public 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 boolean withdraw(double amt) {//取钱
if(balance>=amt) {
balance-=amt;
return true;
}else {
System.out.println("余额不足!");
return false;
}
}
}
Customer类
package banking5;
public class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String f,String l) {
firstName=f;
lastName=l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account acct) {
account = acct;
}
}
Bank类
package banking5;
public class Bank {
private Customer[] customers;//用于存放客户
private int numberOfCustomers;//记录Customer的个数
public Bank() {
customers = new Customer[5];
}
public void addCustomer(String f,String l) {
Customer cust = new Customer(f,l);
customers[numberOfCustomers] = cust;
numberOfCustomers++;
}
public int getNumberOfCustomers() {
return numberOfCustomers;
}
public Customer getCustomer(int index) {
return customers[index];
}
}
SavingAccount类
package banking5;
public class SavingAccount extends Account{
private double interestRate;
public SavingAccount(double balance,double init_rate) {
super(balance);
this.interestRate=init_rate;
}
}
CheckingAccount类
package banking5;
public class CheckingAccount extends Account{
private double overdraftProtection;
public CheckingAccount(double balance) {
super(balance);
}
public CheckingAccount(double balance,double protect) {
super(balance);
this.overdraftProtection=protect;
}
public double getOverdraftProtection() {
return overdraftProtection;
}
public void setOverdraftProtection(double overdraftProtection) {
this.overdraftProtection = overdraftProtection;
}
public boolean withdraw(double amt) {
if(balance>=amt) {
balance-=amt;
return true;
}else if(overdraftProtection>=amt-balance) {
overdraftProtection-=(amt-balance);
balance=0;
return true;
}else {
return false;
}
}
}
测试代码
package testbanking;
import banking5.*;
public class testBanking5 {
public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account;
System.out.println("creating the customer Jane Smith.");
bank.addCustomer("Jane", "Smith");
account = new SavingAccount(500.00,0.03);
customer = bank.getCustomer(0);
customer.setAccount(account);
System.out.println("Creating her Saving Account with a 500.00 balance and 3% interest.");
System.out.println("Creating the customer Owen Bryant.");
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
account = new CheckingAccount(500.00);
customer.setAccount(account);
System.out.println("Creating his checking Account with a 500.00 balance and no overdraft protection.");
System.out.println("Creating the customer Tim Soley.");
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomer(2);
account = new CheckingAccount(500.00,500.00);
System.out.println("Creating his checking Account with a 500.00 balance and 500.00 overdraft protection.");
System.out.println("Creating the customer Maria Soley.");
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomer(3);
System.out.println("Maria shares her Checking Account with her huaband Tim.");
customer.setAccount(bank.getCustomer(2).getAccount());
System.out.println();
System.out.println("Retrieving the customer Jane Smith with her saving account");
customer=bank.getCustomer(0);
account = customer.getAccount();
System.out.println("Withdraw 150.00:"+account.withdraw(150.00));
System.out.println("Deposit 22.50:"+account.deposit(22.50));
System.out.println("Withdraw 47.62:"+account.withdraw(47.62));
System.out.println("Withdraw 400.00:"+account.withdraw(400.00));
System.out.println("Customer["+customer.getLastName()+","+customer.getFirstName()+"] has a balance of "+account.getBalance());
System.out.println();
System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
customer=bank.getCustomer(1);
account = customer.getAccount();
System.out.println("Withdraw 150.00:"+account.withdraw(150.00));
System.out.println("Deposit 22.50:"+account.deposit(22.50));
System.out.println("Withdraw 47.62:"+account.withdraw(47.62));
System.out.println("Withdraw 400.00:"+account.withdraw(400.00));
System.out.println("Customer["+customer.getLastName()+","+customer.getFirstName()+"] has a balance of "+account.getBalance());
System.out.println();
System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
customer=bank.getCustomer(2);
account = customer.getAccount();
System.out.println("Withdraw 150.00:"+account.withdraw(150.00));
System.out.println("Deposit 22.50:"+account.deposit(22.50));
System.out.println("Withdraw 47.62:"+account.withdraw(47.62));
System.out.println("Withdraw 400.00:"+account.withdraw(400.00));
System.out.println("Customer["+customer.getLastName()+","+customer.getFirstName()+"] has a balance of "+account.getBalance());
System.out.println();
System.out.println("Retrieving the customer Maria Soley with her joint checking accout with husband Tim.");
customer=bank.getCustomer(3);
account = customer.getAccount();
System.out.println("Deposit 150.00:"+account.deposit(150.00));
System.out.println("Withdraw 750.00:"+account.withdraw(750.00));
System.out.println("Customer["+customer.getLastName()+","+customer.getFirstName()+"] has a balance of "+account.getBalance());
System.out.println();
}
}