代碼挺多的貼不上。
請聯系我。
下面是部分代碼:(不完整)
public class Bank {
private ArrayList<Account> list=new ArrayList<Account>();
public static final long startNO=1000L;
private static long NO=startNO;
public void registery(){
Scanner scan=new Scanner(System.in);
String name,key1,key2,id,email;
double money=0;
int state=0;
do {
System.out.print("請選擇您要開戶的類型 1.借記卡 2.信用卡:");
try {
state=Integer.parseInt(scan.next().trim());
if(state!=1&&state!=2)
throw new Exception();
}
catch (Exception e) {
System.out.println("非法字符或指令輸入錯誤");
}
}while(state==0);
do {
System.out.print("輸入用戶名,長度為>=3:");
name=scan.next().trim();
if(name.length()<3)
System.out.println("用戶名長度不夠");
else break;
} while (true);
do {
System.out.print("輸入密碼,長度為>=6,必須含數字,字符和特殊符號:");
key1=scan.next().trim();
if(key1.length()<6){
System.out.println("密碼長度不夠");
continue;}
if(!key1.matches("\\d+\\p{Punct}+\\p{Alpha}+")&&!
key1.matches("\\p{Alpha}+\\p{Punct}+\\d+")){
System.out.println("密碼格式不對,必須含數字,字符和特殊符號:");
continue;}
System.out.print("確認密碼:");
key2=scan.next().trim();
if(!key1.equals(key2))
System.out.println("兩次密碼不匹配");
else break;
} while (true);
do {
System.out.print("輸入18位的身份證號:");
id=scan.next().trim();
if(id.length()!=18||!id.matches("\\d+"))
System.out.println("身份證號長度不符合或格式不對");
else break;
} while(true);
do {
System.out.print("請輸入郵箱email:");
email=scan.next().trim();
if(!email.matches("^\\w+@\\w+$"))
System.out.println("郵箱格式不對");
else break;
} while (true);
do {
System.out.print("請輸入存款金額:");
try {
money=Double.parseDouble(scan.next().trim());
if(money<=0)
throw new Exception();
}
catch (Exception e){
System.out.println("金額格式錯誤或不合理(<=0)");
}
} while (money<=0);
System.out.print("開戶成功 卡號為:"+NO+" 類型是"+(state==1?"借記卡":"信用卡")+" 姓名:"+name+
" 身份證號:"+id+" 郵箱:"+email+" 預存款金額為:"+money+"元");
if(state==2){
CreditAccount creditAccount=new CreditAccount(NO,name,key1,id,email,money);
System.out.println(" 可用額度為:"+(creditAccount.getCeiling()+money+"元"));
list.add(creditAccount);
}
else{
list.add(new DebtAccount(NO,name,key1,id,email,money));
System.out.println();
}
NO++;
}
public Account login(long id, String password){
Account a=searchAccount(id);
if(a!=null)
if(a.getPasswd().equals(password))
return a;
return null;
}
public Account deposit(long id, double money){
Account a=searchAccount(id);
if(a!=null){
a.deposit(money);
return a;
}
return null;
}
public Account withdraw(long id, double money){
Account a=searchAccount(id);
if(a!=null)
if(a.withdraw(money)>0)
return a;
return null;
}
public Account setCeiling(long id,double ceiling){
Account a=searchAccount(id);
if(a!=null)
if(a.getClass()==CreditAccount.class){//找id對應帳戶,並且為信用卡的帳戶
((CreditAccount)a).setCeiling(ceiling);
return a;
}
return null;
}
public String balanceStr(Account a){
String str="您的余額為:"+a.getBalance();
if(a.getClass()==CreditAccount.class)
str=str+" 信用卡可用額度為:"+
(((CreditAccount)a).getCeiling()+a.getBalance())+"元";
return str;
}
public double allBalance(boolean isComputeCredit){
double allRemain=0;
if(isComputeCredit){
for(Account a:list)
if(a.getClass()==CreditAccount.class)
allRemain=allRemain+a.getBalance();
}
else{
for(Account a:list)
allRemain=allRemain+a.getBalance();
}
return allRemain;
}
public Account searchAccount(long id){
for(Account a:list)
if(a.getId()==id)
return a;
return null;
}
public static void main(String[] args) {
Bank bank=new Bank();
Scanner scan=new Scanner(System.in);
int state=0;
do {
System.out.print("歡迎使用網上銀行系統 請輸入指令 1.開戶 2.登錄 3.退出:");
try {
state=Integer.parseInt(scan.next().trim());
if(state!=1&&state!=2&&state!=3)
throw new Exception();
} catch (Exception e) {
System.out.println("非法字符或指令輸入錯誤");
continue;
}
if(state==1)
bank.registery();
else if(state==2){
Account log=bank.logCheck();
if(log!=null){
bank.operate(log);
}
}
} while (state!=3);
System.out.println("謝謝使用,歡迎下次使用!!");
}
public Account logCheck(){
Scanner scan=new Scanner(System.in);
String password;
long id=0;
do {
try {
System.out.print("輸入卡號:");
id=Long.parseLong(scan.next().trim());
if(id<startNO)
throw new Exception();
}
catch (Exception e) {
System.out.println("卡號長度不夠或非法字符輸入");
continue;
}
do {
System.out.print("輸入密碼:");
password=scan.next().trim();
if(password.length()<6)
System.out.println("密碼長度不夠");
else break;
} while (true);
Account log=login(id, password);
if(log==null){
System.out.println("登錄失敗!卡號或密碼錯誤,請重新登錄!");
}
else{
System.out.println("登錄成功!您的卡片類型為"+(log.getClass()==
CreditAccount.class?"信用卡":"借記卡"));
return log;
}
} while(true);
}
public void operate(Account a){
Scanner scan=new Scanner(System.in);
int state=-1;
do {
System.out.print("請輸入指令 1.取款 2.存款 3.退出 0.余額查詢:");
try {
state=Integer.parseInt(scan.next().trim());
if(state!=1&&state!=2&&state!=3&&state!=0)
throw new Exception();
}
catch (Exception e) {
System.out.println("非法字符或指令輸入錯誤");
continue;
}
if(state==0){
System.out.println(balanceStr(a));
}
else if(state==1){
double money=0;
do {
System.out.print("請輸入取款金額:");
try {
money=Double.parseDouble(scan.next().trim());
if(money<=0)
throw new Exception();
}
catch (Exception e) {
System.out.println("金額格式錯誤或不合理(<=0)");
}
} while (money<=0);
if(a.withdraw(money)==0){
System.out.println("對不起,您的余額不足,請重新輸入");
}
else System.out.println("取款成功,"+balanceStr(a));
}
else if(state==2){
double money=0;
do {
System.out.print("請輸入存款金額:");
try {
money=Double.parseDouble(scan.next().trim());
if(money<=0)
throw new Exception();
}
catch (Exception e) {
System.out.println("金額格式錯誤或不合理(<=0)");
}
} while (money<=0);
a.deposit(money);
System.out.println("存款成功,"+balanceStr(a));
}
} while(state!=3);
System.out.println(a.getName()+"退出成功。");
}
}