📄 bankimpl.java
字号:
package biz;
import java.io.DataOutputStream;
import java.io.File;
import dao.AccountDao;
import dao.DaoImpl;
import entity.*;
import exception.AccountNotExistException;
import exception.BalanceNotEnoughException;
import exception.RegisterException;
public class BankImpl implements Bank {
private AccountDao dao = new DaoImpl();
public void delete(long id) {
dao.deleteAccount(id);
}
public void deposit(long id,double money) {
Account a=null;
try {
a=this.findAccountById(id);
} catch (AccountNotExistException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double d=a.getBalance();
d+=money;
a.setBalance(d);
try {
dao.updateAcccount(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public long login(long id, String password) throws AccountNotExistException{
Account a=this.findAccountById(id);
if(a!=null){
if(a.getPassword().equals(password)){
return a.getCardId();
}
}
return -1;
}
public long register(String personId, String password, String password2,
String name, int type) throws RegisterException{
Account a = null;
long id=0;
if (password.equals(password2)) {
try {
if (type == 1) {
id = dao.findLastId();
a = new SavingAccount(personId, password, name);
a.setCardId(id);
dao.insertAccount(a);
}
if (type == 2) {
id = dao.findLastId();
a = new CreditAccount(personId, password, name, 10000.0);
a.setCardId(id);
dao.insertAccount(a);
}
} catch (Exception e) {
e.printStackTrace();
}
}else throw new RegisterException("2次密码输入错误");
return id;
}
public double select(long id) {
Account a=null;
try {
a = this.findAccountById(id);
} catch (AccountNotExistException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
double balance=a.getBalance();
return balance;
}
public void withDraw(long id,double money) throws BalanceNotEnoughException{
Account a=null;
try {
a=this.findAccountById(id);
} catch (AccountNotExistException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(a instanceof CreditAccount){
CreditAccount c=(CreditAccount)a;
double d=a.getBalance();
if(money<=(d+c.getCredit())){
d-=money;
a.setBalance(d);
}else throw new BalanceNotEnoughException("余钱不足");
}else{
double d=a.getBalance();
if(money<=d){
d-=money;
a.setBalance(d);
}else throw new BalanceNotEnoughException("余钱不足");
}
try {
dao.updateAcccount(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Account findAccountById(long id) throws AccountNotExistException {
Account a = null;
try {
a = dao.getAccountById(id);
} catch (Exception e) {
e.printStackTrace();
}
if (a == null)
throw new AccountNotExistException("此帐号不存在");
else
return a;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -