//纯面向对象 //银行普通客户类 //取款不可透支,不可转账 public class Cust { String name; int ID; int password; int money; Cust(String name,int ID,int password,int money) { this.name=name; this.ID=ID; this.password=password; this.money=money; } boolean getMoney(int m) { if(m<0) { if(saveMoney(-m)) return true; else return false; } else { if(m>money) { System.out.println("Warning:该账户不可透支,本次操作中断!"); return false; } this.money-=m; return true; } } boolean saveMoney(int m) { if(m<0) { if(getMoney(-m)) return true; else return false; } else { this.money+=m; return true; } } void search() { System.out.println(name+"(" + ID +") 余额:"+money); } boolean checkpw() { int i; for(i=0;i<3;i++) { if(i>0) System.out.println("Warning:密码错误,请输入密码:"); int pw=KB.scanInt(); if(pw==password) return true; } if(i==3) System.out.println("Warning:密码输入出错3次,操作中断!"); return false; } void run(Cust st[]) { while(true) { printMenu(); int cmd=KB.scanInt(); switch(cmd) { case 1: { System.out.println("请输入存款数:"); int m=KB.scanInt(); saveMoney(m); break; } case 2: { System.out.println("请输入取款数:"); int m=KB.scanInt(); getMoney(m); break; } case 3: search();break; case 0:return; } } } static void printMenu() { //System.out.println("诚信银行"); System.out.println("——————"); System.out.println("存款——1"); System.out.println("取款——2"); System.out.println("查余——3"); System.out.println("退出——0"); System.out.println("——————"); } } //银行VIP客户类 //取款可透资,可转账 public class CustVip extends Cust { CustVip(String name,int ID,int password,int money) { super(name,ID,password,money); } boolean getMoney(int m) { if(m<0) { if(saveMoney(-m)) return true; else return false; } else { if(m-5000>money) { System.out.println("Warning:该账户透支不可超过5000元,本次操作中断!"); return false; } this.money-=m; return true; } } void moveMoney(Cust st[]) { System.out.println("请输入目标帐户ID:"); int id=KB.scanInt(); int i; for(i=0;i<st.length;i++) { if(st[i].ID==id) { System.out.println("请输入目标帐户密码:"); if(st[i].checkpw()) { System.out.println("请输入转账金额:"); int m=KB.scanInt(); if(this.getMoney(m)) { st[i].saveMoney(m); System.out.println("转账成功!"); } else { System.out.println("Warning:转账失败!"); } return ; } } } if(i==st.length) System.out.println("Warning:用户不存在!"); } void run(Cust st[]) { while(true) { printMenu(); int cmd=KB.scanInt(); switch(cmd) { case 1: { System.out.println("请输入存款数:"); int m=KB.scanInt(); saveMoney(m); break; } case 2: { System.out.println("请输入取款数:"); int m=KB.scanInt(); getMoney(m); break; } case 3: search();break; case 4: moveMoney(st);break; case 0:return; } } } static void printMenu() { //System.out.println("诚信银行"); System.out.println("——————"); System.out.println("存款——1"); System.out.println("取款——2"); System.out.println("查余——3"); System.out.println("转账——4"); System.out.println("退出——0"); System.out.println("——————"); } } //测试类,主函数 public class Test { /** * @WrittenBy:http://hi.baidu.com/duxing */ public static void main(String[] args) { Cust st[]=new Cust[4]; st[0]=new Cust("admin",0,0,0); st[1]=new Cust("笃行",111,111,1000); st[2]=new CustVip("冬梅",222,222,2000); st[3]=new CustVip("乐乐",333,333,3000); while(true) { System.out.println("请输入帐户ID:"); int id=KB.scanInt(); if(id==0)return; int i; for(i=0;i<st.length;i++) { if(st[i].ID==id) { System.out.println("请输入帐户密码:"); if(st[i].checkpw()) { st[i].run(st); break; } } } if(i==st.length) { System.out.println("Warning:该用户不存在!"); } } } } //获取键盘输入类 import java.io.*; public class KB { public static String scan() { String str = ""; try { BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); str = buf.readLine(); } catch (Exception e) { } return str; } public static int scanInt() { int temp=0; while(true) { try { temp=Integer.parseInt(scan()); break; } catch (Exception e) { System.out.println("Warning:输入不合法,请输入整数:"); } } return temp; } }