📄 bank.java
字号:
package itso.bank.model;
import itso.bank.exception.AccountDoesNotExistException;
import itso.bank.exception.CustomerDoesNotExistException;
import itso.bank.exception.InsufficientFundsException;
import java.util.*;
import java.math.BigDecimal;
/**
* The bank functions as a coordinator class for the banking
* system. It is implemented as a singleton, and is initialized
* with hardcoded data in order to make the example function
* without a datasource backend.
*
* @author Fabio Ferraz, modified by Ueli Wahli
*/
public class Bank {
// The bank singleton instance
private static Bank instance = new Bank();
public static Bank getInstance() { return instance; }
// Object features
private Map accountsMap = new Hashtable();
private Map customersMap = new Hashtable();
private Map customerAccounts = new Hashtable();
private Map transactionMap = new Hashtable();
private Map getAccountsMap() { return accountsMap; }
private Map getCustomersMap() { return customersMap; }
private Map getCustomerAccounts() { return customerAccounts; }
private Map getTransactionMap() { return transactionMap; }
// store customer - accounts relationship: per customer
private void associate(String customerID, String accountID) {
Vector accounts = (Vector)getCustomerAccounts().get(customerID);
if (accounts == null) {
accounts = new Vector();
getCustomerAccounts().put(customerID, accounts);
}
accounts.addElement(accountID);
}
// store transaction records: per account
private void addTransaction(String accountID, TransRecord tr) {
TreeSet transSet = (TreeSet)getTransactionMap().get(accountID);
if (transSet == null) {
transSet = new TreeSet();
getTransactionMap().put(accountID, transSet);
}
transSet.add(tr);
}
// business methods
public Account getAccount(String accountID) throws AccountDoesNotExistException {
if (accountID == null)
throw new AccountDoesNotExistException();
Account account = (Account) getAccountsMap().get(accountID);
if (account == null)
throw new AccountDoesNotExistException();
return account;
}
public Customer getCustomer(String customerID) throws CustomerDoesNotExistException {
if (customerID == null)
throw new CustomerDoesNotExistException();
Customer customer = (Customer) getCustomersMap().get(customerID);
if (customer == null)
throw new CustomerDoesNotExistException();
return customer;
}
public Account[] getAccounts(String customerID) throws CustomerDoesNotExistException {
Vector accounts = (Vector)getCustomerAccounts().get(customerID);
Account[] result = new Account[accounts.size()];
for (int i=0; i < accounts.size(); i++) {
try {
result[i] = getAccount( (String)accounts.elementAt(i) );
} catch (AccountDoesNotExistException e) { result[i] = null; }
}
return result;
}
public TransRecord[] getTransactions(String accountID) throws AccountDoesNotExistException {
TreeSet transSet = (TreeSet)getTransactionMap().get(accountID);
return (TransRecord[])transSet.toArray( new TransRecord[transSet.size()] );
}
public BigDecimal deposit(String accountID, BigDecimal amount)
throws AccountDoesNotExistException {
Account account = getAccount(accountID);
account.deposit(amount);
TransRecord transaction = new TransRecord("C", amount);
addTransaction(accountID, transaction);
return account.getBalance();
}
public BigDecimal withdraw(String accountID, BigDecimal amount)
throws AccountDoesNotExistException, InsufficientFundsException {
Account account = getAccount(accountID);
account.withdraw(amount);
TransRecord transaction = new TransRecord("D", amount);
addTransaction(accountID, transaction);
return account.getBalance();
}
public BigDecimal transfer(String accountID1,String accountID2, BigDecimal amount)
throws AccountDoesNotExistException, InsufficientFundsException {
Account account1 = getAccount(accountID1);
Account account2 = getAccount(accountID2);
withdraw(accountID1,amount);
deposit(accountID2,amount);
return account1.getBalance();
}
//------------------------------------------------------
// build the in-memory Bank with customers and accounts
private Bank() {
// Create hardcoded customers
Customer customer;
customer = new Customer("101");
customer.setTitle("Mr");
customer.setFirstName("Maik");
customer.setLastName("Schumacher");
getCustomersMap().put(customer.getId(), customer);
customer = new Customer("102");
customer.setTitle("Mr");
customer.setFirstName("Fabio");
customer.setLastName("Ferraz");
getCustomersMap().put(customer.getId(), customer);
customer = new Customer("103");
customer.setTitle("Mr");
customer.setFirstName("Henrik");
customer.setLastName("Sjostrand");
getCustomersMap().put(customer.getId(), customer);
customer = new Customer("104");
customer.setTitle("Mr");
customer.setFirstName("Ian");
customer.setLastName("Brown");
getCustomersMap().put(customer.getId(), customer);
customer = new Customer("105");
customer.setTitle("Ms");
customer.setFirstName("Unknown");
customer.setLastName("Lady");
getCustomersMap().put(customer.getId(), customer);
customer = new Customer("106");
customer.setTitle("Mr");
customer.setFirstName("Ueli");
customer.setLastName("Wahli");
getCustomersMap().put(customer.getId(), customer);
// Create hardcoded accounts
Account account;
try {
account = new Account("101-1001");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("80.00") );
account = new Account("101-1002");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("375.26") );
account = new Account("102-2001");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("9375.26") );
account = new Account("102-2002");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("75.50") );
account = new Account("103-3001");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("100.00") );
account = new Account("103-3002");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("20000.00") );
account = new Account("104-4001");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("888.88") );
account = new Account("104-4002");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("88.88") );
account = new Account("105-5001");
getAccountsMap().put(account.getId(), account);
account = new Account("106-6001");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("1000.00") );
account = new Account("106-6002");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("2000.00") );
account = new Account("106-6003");
getAccountsMap().put(account.getId(), account);
deposit( account.getId(), new BigDecimal("3000.00") );
} catch (AccountDoesNotExistException e) {}
// Create Customer <-> Account associations
associate("101", "101-1001");
associate("101", "101-1002");
associate("102", "102-2001");
associate("102", "102-2002");
associate("103", "103-3001");
associate("103", "103-3002");
associate("104", "104-4001");
associate("104", "104-4002");
associate("105", "105-5001");
associate("106", "106-6001");
associate("106", "106-6002");
associate("106", "106-6003");
associate("106", "105-5001");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -