Account类:
Customer类:
Bank类:
Bank与Customer为一对多
Customer与Account是一对多
CustomerReport类:
用于打印输出银行存取信息,按照顾客姓名排序输出(排序在customer类中实现)
/*
Account类
*/
package exercise3.banking.domain;
import com.sun.org.apache.xpath.internal.operations.Bool;
import exercise3.banking.domain.OverdraftException;
public class Account {
protected double balance;
public Account(double init_balance) {
balance=init_balance;
}
public double getBalance(){
return balance;
}
public boolean deposit(double amount)
{
balance=balance+amount;
return true;
}
public void withdraw(double amt) throws OverdraftException
{
if(balance>=amt)
{
balance=balance-amt;
}
else
{
throw new OverdraftException("Insufficient funds",amt-balance);
}
}
}
/*
Customer类
*/
package exercise3.banking.domain;
import java.util.ArrayList;
import java.util.List;
import exercise3.banking.domain.Account;
import exercise3.banking.domain.Customer;
public class Customer implements Comparable{
private String firstName,lastName;
private List<Account> accounts;
int numberOfaccounts=0;
public Customer(String f,String l) {
setFirstname(f);
setLastname(l);
accounts=new ArrayList<Account>();
}
public void setFirstname(String firstname) {
this.firstName = firstname;
}
public void setLastname(String lastname) {
this.lastName = lastname;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public void addAccount(Account acc)
{
accounts.add(acc);
}
public void setAccount(List account1) {
accounts=account1;
}
public Account getAccount(int index)
{
return accounts.get(index);
}
public int getnumOfaccounts()
{
return this.numberOfaccounts;
}
public int compareTo(Object o) {
Customer another=(Customer) o;
int i=0;
i=lastName.compareTo(another.lastName);
if(i == 0) {
int b=0;
b= firstName.compareTo(another.firstName);
return b;
}
else {
return i;
}
}
}
/*
SavingsAccount类
*/
package exercise3.banking.domain;
public class SavingsAccount extends Account{
private double interestRate;
public SavingsAccount(double balance,double in)
{
super(balance);
this.interestRate=in;
}
}
/*
CheckingAccount类
*/
package exercise3.banking.domain;
import exercise3.banking.domain.OverdraftException;
public class CheckingAccount extends Account{
private double overdraftProtection;//信用卡透支额度
public CheckingAccount(double balance)
{
super(balance);
this.overdraftProtection=0.0;
}
public CheckingAccount(double balance,double protect)
{
super(balance);
this.overdraftProtection=protect;
}
public void withdraw(double amt) throws OverdraftException
{
if(balance>=amt)
{
balance=balance-amt;
}
else
{
if(this.overdraftProtection==0)
{
throw new OverdraftException("No overdraft protection",amt-balance);
}
else
{
if ((overdraftProtection-(amt-balance))<0) {
throw new OverdraftException("Insufficient funds for overdraft protection",amt-balance-overdraftProtection);
}
else {
overdraftProtection=overdraftProtection-(amt-balance);
balance=0.0;
}
}
}
}
}
/*
用于抛出异常,账户赤字
*/
package exercise3.banking.domain;
public class OverdraftException extends Exception {
private double deficit;
public OverdraftException(String message,double de)
{
super(message);
this.deficit=de;
}
public double getDeficit()
{
return this.deficit;
}
}
/*
Bank类
*/
package exercise3.banking.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Bank {
List<Customer> customers;
int numberOfCustomers=0;
private static Bank bankInstance=new Bank();//一个银行实体
public Bank()
{
customers=new ArrayList<Customer>();
}
public static Bank getBank()
{
return bankInstance;
}
public void addCustomer(String f,String l)
{
customers.add(new Customer(f,l));
}
public int getNumOfCustomers()
{
return customers.size();
}
public Customer getCustomer(int index)
{
return customers.get(index);
}
public void sortCustomers() {
Collections.sort(this.customers);
}
}
/*
打印输出
*/
package exercise3.banking.reports;
import exercise3.banking.domain.*;
import java.text.NumberFormat;
public class CustomerReport {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = Bank.getBank();
Customer customer;
public void generateReport() {
// TODO Auto-generated method stub
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");
for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ ) {
customer = bank.getCustomer(cust_idx);
System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());
for ( int acct_idx = 0; acct_idx < customer.getnumOfaccounts(); acct_idx++ ) {
Account account = customer.getAccount(acct_idx);
String account_type = "";
boolean re1=account instanceof SavingsAccount;
if(re1==true) {
account_type="SavingsAccount";
System.out.println(account_type+":current balance is"+currency_format.format(account.getBalance()));
}
boolean re2=account instanceof CheckingAccount;
if(re2==true) {
account_type="CheckingAccount";
System.out.println(account_type+":current balance is "+currency_format.format(account.getBalance()));
}
}
}
}
}
主函数:
package exercise3.banking;
/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/
import exercise3.banking.domain.*;
public class TestBanking {
public static void main(String[] args) throws OverdraftException {
Bank bank = Bank.getBank();
Customer customer;
Account account;
// Create two customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 500.00));
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomer(1);
customer.addAccount(new CheckingAccount(200.00));
// Test the checking account of Jane Simms (with overdraft protection)
customer = bank.getCustomer(0);
account = customer.getAccount(1);
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance()
+ " with a 500.00 overdraft protection.");
try {
System.out.println("Checking Acct [Jane Simms] : withdraw 150.00");
account.withdraw(150.00);
System.out.println("Checking Acct [Jane Simms] : deposit 22.50");
account.deposit(22.50);
System.out.println("Checking Acct [Jane Simms] : withdraw 147.62");
account.withdraw(147.62);
System.out.println("Checking Acct [Jane Simms] : withdraw 470.00");
account.withdraw(470.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
}finally {
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
}
System.out.println();
// Test the checking account of Owen Bryant (without overdraft protection)
customer = bank.getCustomer(1);
account = customer.getAccount(0);
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
try {
System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00");
account.withdraw(100.00);
System.out.println("Checking Acct [Owen Bryant] : deposit 25.00");
account.deposit(25.00);
System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00");
account.withdraw(175.00);
} catch (OverdraftException e1) {
System.out.println("Exception: " + e1.getMessage()
+ " Deficit: " + e1.getDeficit());
} finally {
System.out.println("Customer [" + customer.getLastName()
+ ", " + customer.getFirstName() + "]"
+ " has a checking balance of "
+ account.getBalance());
}
}
}