package biz;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import entity.Account;
import entity.CreditAccount;
import entity.SavingAccount;
import exception.LoginException;
import exception.RegisterException;
public class MapBank implements IBank{
Map<Long,Account> map=new HashMap<Long,Account>();
public long reg(String password,String password2,String name,String personId,int type){
Account a=null;
if(!password.equals(password2)){
throw new RegisterException("两次密码不相同!");
}else if(type==1){
a=new SavingAccount(name,password, personId);
}else if(type==0){
a=new CreditAccount(name,password,personId);
}else{
throw new RegisterException("输入有误,请选择1或0!");
}
map.put(a.getId(),a);
return a.getId();
}
public long login(long id,String password){
Account a=getAccountById(id);
if(a==null){
throw new LoginException("账户为空!");
}else if(!a.getPassword().equals(password)){
throw new LoginException("密码不正确!");
}
return a.getId();
}
public void deposit(long id,double money){
Account a=getAccountById(id);
a.deposit(money);
}
public void withdraw(long id,double money){
Account a=getAccountById(id);
a.withdraw(money);
}
public double selectBalance(long id){
Account a=getAccountById(id);
return a.getBalance();
}
public Account getAccountById(long id) {
return map.get(id);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import entity.Account;
import entity.CreditAccount;
import entity.SavingAccount;
import exception.LoginException;
import exception.RegisterException;
public class MapBank implements IBank{
Map<Long,Account> map=new HashMap<Long,Account>();
public long reg(String password,String password2,String name,String personId,int type){
Account a=null;
if(!password.equals(password2)){
throw new RegisterException("两次密码不相同!");
}else if(type==1){
a=new SavingAccount(name,password, personId);
}else if(type==0){
a=new CreditAccount(name,password,personId);
}else{
throw new RegisterException("输入有误,请选择1或0!");
}
map.put(a.getId(),a);
return a.getId();
}
public long login(long id,String password){
Account a=getAccountById(id);
if(a==null){
throw new LoginException("账户为空!");
}else if(!a.getPassword().equals(password)){
throw new LoginException("密码不正确!");
}
return a.getId();
}
public void deposit(long id,double money){
Account a=getAccountById(id);
a.deposit(money);
}
public void withdraw(long id,double money){
Account a=getAccountById(id);
a.withdraw(money);
}
public double selectBalance(long id){
Account a=getAccountById(id);
return a.getBalance();
}
public Account getAccountById(long id) {
return map.get(id);
}
}