?? bank.java
字號:
package business;
import java.util.*;
import model.*;
import Exception.*;
import DAO.*;
/*
* 把銀行類Bank作成單例模式
* 因為銀行類Bank要維護很多方法和屬性,每創建一個實例就耗用很大的內存,
* 所以最好作成單例模式
*/
public class Bank {
private static Bank bank = null;
private HashMap map1;
private AccountDAO dao;
/*
* 向外提供一個公開的獲得銀行bank的方法
*/
public static Bank newBank() {
if (bank == null)
bank = new Bank();
return bank;
}
/*
* 從這里更能看出使用DAO模式的優勢:
* 這里你調用DAO工廠就能獲得一個相應的DAO實例,
* 就可以使用它的loadAccounts()方法或是saveAccounts()方法來
* 載入數據或是保存數據;而根本不用用考慮地層是怎么來實現的;
* 注意:在創建銀行對象時,就已經將文件中的所以客戶的數據都保存在了HashMap中(內存中);
*/
private Bank() {
AccountDaoFactory factory= AccountDaoFactory.newFactory();
dao = factory.getAccountDao();
map1 = dao.loadAccounts();
}
/*
* Bank類的任何一個修改客戶數據的操作都要進行一次保存,
* 把修改后的結果重新保存到文件中;
* 是為了保證內存中的客戶的數據和文件中客戶的數據保持一致;
*/
// 開戶
public Account register(String pass1, String pass2, String name,
String personId, String email, int type)
throws BusinessException.RegisterException {
if (!(pass1.equals(pass2)))
throw new BusinessException.RegisterException(
name + " RegisterException--->register: PasswordError ! 兩次密碼輸入不一致!");
/*
* 系統規定: 0---代表儲蓄用戶 1---代表信用賬戶 2---代表可貸款儲蓄用戶 3---代表可貸款信用賬戶
*/
Account c = null;
if (type == 0)
c = new SavingAccount(pass1, name, personId, email);
else if (type == 1)
c = new CreditAccount(pass1, name, personId, email);
else if (type == 2)
c = new LoanSavingAccount(pass1, name, personId, email);
else if (type == 3)
c = new LoanCreditAccount(pass1, name, personId, email);
else
return null;
map1.put(c.getId(), c);
dao.saveAccounts(map1);
return c;
}
// 登錄
public Account login(long id, String password)
throws BusinessException.LoginException {
Account c = (Account) (map1.get(new Long(id)));
if (c == null)
throw new BusinessException.LoginException(
"LoginException--->register:NotFoundId ! id 不存在!");
if (!(password.equals(c.getPassword())))
throw new BusinessException.LoginException(
"LoginException--->register:PasswordError ! 密碼錯誤!");
System.out.println("Welcom " + "\t" + c.getName() + " !");
return c;
}
// 存款
public Account deposit(long id, double money) throws NullPointerException {
Account c = (Account) (map1.get(new Long(id)));
c.deposit(money);
dao.saveAccounts(map1);
return c;
}
// 取款
public Account withdraw(long id, double money)
throws BusinessException.BalanceNotEnoughException,
NullPointerException {
Account c = (Account) (map1.get(new Long(id)));
c.withdraw(money);
dao.saveAccounts(map1);
return c;
}
// 設置透支額度
public Account setCeiling(long id, double ceiling) {
Account c = (Account) (map1.get(new Long(id)));
if (c instanceof CreditAccount) {
CreditAccount ca = (CreditAccount) c;
ca.setCeiling(ceiling);
dao.saveAccounts(map1);
}
return c;
}
// 申請貸款
public Account requestLoan(long id, double money) {
Account c = (Account) (map1.get(new Long(id)));
if (c instanceof Loannable) {
Loannable an = (Loannable) c;
an.requestLoan(money);
dao.saveAccounts(map1);
}
return c;
}
// 還貸款
public Account payLoan(long id, double money)
throws BusinessException.LoanException,
BusinessException.BalanceNotEnoughException {
Account c = (Account) (map1.get(new Long(id)));
if (c instanceof Loannable) {
Loannable an = (Loannable) c;
an.payLoan(money);
dao.saveAccounts(map1);
}
return c;
}
// 統計所有帳戶余額
public double getAllBalance() {
double d = 0;
// 得到所有值對象 Account對象
Collection c = map1.values();
Iterator it = c.iterator();
while (it.hasNext()) {
Account ac = (Account) it.next();
d += ac.getBalance();
}
return d;
}
// 統計所有信用帳戶透支額度的總和
public double getAllCeiling() {
double d = 0;
Collection c = map1.values();
Iterator it = c.iterator();
while (it.hasNext()) {
Account ac = (Account) it.next();
if (ac instanceof CreditAccount) {
CreditAccount ca = (CreditAccount) ac;
d += ca.getCeiling();
}
}
return d;
}
// 統計所有貸款帳戶的貸款額總和
public double getAllLoan() {
double d = 0;
Collection c = map1.values();
Iterator it = c.iterator();
while (it.hasNext()) {
Account ac = (Account) it.next();
if (ac instanceof Loannable) {
Loannable l = (Loannable) ac;
d += l.getLoan();
}
}
return d;
}
//
public void print() { // key:身份證號,value:總資產
Map map2 = new HashMap();
// 遍歷所有的帳戶,添加到map2中,并且同時也要遍歷map2求出總資產.
Collection c = map1.values();
Iterator it = c.iterator();
while (it.hasNext()) {
Account ac = (Account) it.next();
String pid = ac.getPersonId();
// 在key鍵對中找是否有key對象(返回boolean值).
// 如果有這個pid的話會從新覆蓋.
if (map2.containsKey(pid)) { // 把總資產強轉成Double類型的對象.
Double d = (Double) map2.get(pid);
// d.doubleValue()返回此 Double 對象的 double 值。
map2.put(pid, d.doubleValue() + ac.getBalance());
} else
map2.put(pid, ac.getBalance());
}
// 實現一個輔助的內部類
class Temp implements Comparable {
String pid;
double money;
public Temp(String pid, double maoney) {
super();
this.pid = pid;
this.money = maoney;
}
public String toString() {
return pid + " " + money;
}
public int compareTo(Object o) {
Temp t = (Temp) o;
if (this.money > t.money)
return -1;
else if (this.money == t.money)
return 0;
else
return 1;
}
}
// 遍歷m2,構造temp對象,放在List里,為了下一步排序.
List list = new ArrayList();
Set set = map2.keySet();
Iterator ite = set.iterator();
while (ite.hasNext()) {
String pid = (String) ite.next();
// Double d=(Double)map2.get(pid);
// Temp temp=new Temp(pid,d.doubleValue());
Temp temp = new Temp(pid, ((Double) map2.get(pid)));
list.add(temp);
}
// 對List排序并且遍歷
Collections.sort(list);
Iterator iter = list.iterator();
while (iter.hasNext()) {
Object o = iter.next();
System.out.println(o);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -