📄 bank.java
字号:
package biz;
import entity.Account;
import entity.CreditAccount;
import entity.SavingAccount;
public class Bank implements IBank{
private Account[] accounts=new Account[10];
private int index=0;
private void expand(){
Account[] as=new Account[accounts.length*2];
System.arraycopy(accounts,0,as,0,accounts.length);
accounts=as;
}
public long regist(String name,String password,String againPassword,String personId,int type){
if(password.equals(againPassword)){
Account a=null;
if(type==0){
a=new SavingAccount(name,password,personId);
}
if(type==1){
a=new CreditAccount(name,password,personId);
}
if(index>=accounts.length){
expand();
}
accounts[index]=a;
index++;
return a.getId();
}else{
//System.out.println("Sorry,password not same!");
return -1;
}
}
public long login(long id,String password){
// 根据id查找帐户,获取该帐户的password
Account a=queryById(id);
if(a==null){
System.out.println("Sorry,Account not exist");
return -1;
}
String password1=a.getPassword();
// 判断两个password是否相等
if(password1.equals(password)){
return id;
}
//System.out.println("Sorry , Password not correct");
return -1;
}
public void save(long id,double money){
// 根据id查找帐户
Account a=queryById(id);
if(a==null){
System.out.println("Sorry,Account not exist");
} else{
a.deposit(money);
}
}
public void withdraw(long id,double money){
// 根据id查找帐户
Account a=queryById(id);
if(a==null){
System.out.println("Sorry,Account not exist");
} else{
a.withdraw(money);
}
}
public double queryBalance(long id){
// 根据id查找帐户
Account a=queryById(id);
if(a==null){
System.out.println("Sorry,Account not exist");
return -1;
}else{
return a.getBalance();
}
}
private Account queryById(long id){
for(int i=0;i<index;i++){
long lid=accounts[i].getId();
if(id==lid) return accounts[i];
}
return null;
}
public static void main(String[] args){
Bank bank=new Bank();
bank.regist("huxz","12345","12345","1111111",0);
bank.login(10001,"1234");
bank.save(10001,5000);
System.out.println(bank.queryBalance(10001));
bank.withdraw(10002,3000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -