📄 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 + -