java---简单的ATM存取系统,
新手练手必备~
密码账户为:
先创建账户类:
package cn.Atm; /** * @author 偶my耶 */ import java.io.*; import com.project.project; public class Account { private String number=null; private String name=null; private String password=null; private double money=0.0; public Account(String number,String name,String password,double money){ this.number=number; this.name=name; this.password=password; this.money=money; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } protected void sub_Balance(double mon) { money-=mon; } protected void add_Balancen(double mon) { money+=mon; } }
然后建立操作类:
里面包含各种操作方法:
1 package cn.Atm; 2 /** 3 * @author 偶my耶 4 */ 5 import java.io.*; 6 7 import com.project.project; 8 public class ATM { 9 Account act; 10 11 public ATM(){ 12 act=new Account("0000", "test", "0000", 2000); 13 } 14 /*************欢迎界面*********************/ 15 protected void Welcome(){ 16 String str="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; 17 System.out.print(str+"\n"); 18 System.out.print( 19 "1.取款"+"\n"+ 20 "2.查询"+"\n"+ 21 "3.存款"+"\n"+ 22 "4.退出"+"\n" 23 ); 24 System.out.print(str+"\n"); 25 } 26 /************登陆系统********************/ 27 protected void Load_Sys() throws Exception{ 28 String card,pwd; 29 int counter=0; 30 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 31 do{ 32 System.out.println("请输入你的卡号"); 33 card=br.readLine(); 34 System.out.println("请输入你的密码"); 35 pwd=br.readLine(); 36 if (!isRight(card,pwd)) { 37 System.out.println("你的卡号或密码有误"); 38 counter++; 39 } 40 else { 41 Welcome(); 42 SysOpter(); 43 } 44 }while(counter<5); 45 System.exit(1); 46 } 47 /**********系统操作提示******************/ 48 protected void SysOpter() throws Exception { 49 int num; 50 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 51 System.out.println("请选择你要操作的项目(1--4)"); 52 num=br.read(); 53 switch (num) { 54 case 49: 55 GetBalance(); 56 break; 57 case 50: 58 Inqu_Iofo(); 59 break; 60 case 51: 61 AddBalance(); 62 break; 63 case 52: 64 Exit_Sys(); 65 break; 66 } 67 } 68 /**********信息查询*******************/ 69 protected void Inqu_Iofo() throws Exception{ 70 // TODO Auto-generated method stub 71 String str="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"; 72 System.out.print(str+"\n"); 73 System.out.print( 74 "账号"+act.getNumber()+"\n"+ 75 "姓名"+act.getName()+"\n"+ 76 "余额"+act.getMoney()+"\n"+ 77 str+"\n" 78 ); 79 SysOpter(); 80 } 81 82 /**********存款******************/ 83 public void AddBalance() throws Exception{ 84 // TODO Auto-generated method stub 85 String str=null,str1; 86 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 87 do { 88 System.out.println("请输入存款数目"); 89 str=br.readLine(); 90 double qu=Double.valueOf(str).doubleValue(); 91 act.add_Balancen(qu); 92 System.out.println("存款成功,你的账户余额为"+act.getMoney()); 93 Welcome(); 94 SysOpter(); 95 } while (true); 96 } 97 /**********取款********************/ 98 public void GetBalance() throws Exception{ 99 // TODO Auto-generated method stub 100 String str=null,str1; 101 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 102 do { 103 System.out.println("请输入取款数目"); 104 str=br.readLine(); 105 double qu=Double.valueOf(str).doubleValue(); 106 if(qu>act.getMoney()){ 107 System.out.println("余额不足请重新输入"); 108 } 109 else { 110 act.sub_Balance(qu); 111 System.out.println("取款成功你的账户余额未"+act.getMoney()); 112 Welcome(); 113 SysOpter(); 114 } 115 } while (true); 116 } 117 /*********退出******************/ 118 protected void Exit_Sys () { 119 // TODO Auto-generated method stub 120 System.out.println("安全退出!"); 121 System.exit(1); 122 } 123 124 /**********卡号和密码是否正确********************/ 125 protected boolean isRight (String card, String pwd) { 126 // TODO Auto-generated method stub 127 if (act.getNumber().equals(card)&&act.getPassword().equals(pwd)) { 128 return true; 129 } else { 130 return false; 131 } 132 133 } 134 public static void main(String[] args) throws Exception { 135 ATM atm=new ATM(); 136 // atm.Welcome(); 137 atm.Load_Sys(); 138 } 139 }
代码仅供参考
运行结果如下
博客地址: | http://www.cnblogs.com/oumyye/ |
博客版权: | 本文以学习、研究和分享为主,欢迎转载,转载请务必注明出处,谢谢合作。 如果文中有不妥或者错误的地方请指出。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! |