zoukankan      html  css  js  c++  java
  • Java模拟ATM运行过程(第一版)

      该版本只有一个银行且没有并发,没有数据库,也没有GUI……主要是能力不够,而且飞哥说这个版本还是面向过程的,牛人给指点下。

      1 /*想了想这个数组还是放在ATM里会比较好,直接放在主类里不行
      2  * 选择创建ATM的时候创建一个ArrayList
      3  * 直接输入新用户的话是String,没法转为UserCard,只能在ATM里创建新用户了
      4  */
      5 import java.io.BufferedReader;
      6 import java.io.IOException;
      7 import java.io.InputStreamReader;
      8 import java.util.ArrayList;
      9 
     10 public class TestMyATM {
     11 
     12     public static void main(String[] args) throws Exception {
     13         
     14         ArrayList<UserCard> array =new ArrayList<UserCard>();
     15         ATM atm = new ATM("我是一号ATM",1000000,array);//ATM机里有一千万
     16         atm.addUser("001", "Mike", "123456", 1000.0);
     17         atm.MyWelcome();
     18         atm.LoadSys();
     19     }
     20 
     21 }
     22 
     23 class ATM {
     24     
     25     UserCard user;
     26     private String name;
     27     /*
     28      * 也可以使用String,然后用compareTo比较大小,但是做差时就不方便啦,
     29      * 所以还是用基础类型较好,可以方便地转为String
     30      */
     31     public long remain = 0;//ATM机里的钱,以判断是否够取,不能设置为private,因为UserCard类里还要判断钱是否够取
     32     ArrayList<UserCard> array = new ArrayList<UserCard>();
     33     
     34     ATM(String name, long remain, ArrayList<UserCard> array) {
     35         this.name = name;
     36         this.remain = remain;    
     37         this.array = array;
     38     }    
     39     /*
     40      * java里如何在一个类里调用不相干类里的构造方法 ?
     41      * 下面的方法想了好久…………………………操
     42      */
     43     public void addUser(String card, String name,String pwd, double money) {
     44         user = new UserCard(card,name,pwd,money);
     45         array.add(user);
     46     }
     47     
     48     protected long getRemain() {
     49         return remain;
     50     }
     51 
     52     protected void setRemain(long remain) {
     53         this.remain = remain;
     54     }
     55 
     56 
     57     //欢迎界面 
     58     protected void MyWelcome() {
     59         String str = "---------------------------------";
     60         System.out.print(str + "\n" + "欢迎使用飞蓬工作室出品的模拟ATM程序.\n" + str + "\n");
     61         System.out.print(" 1.>取款." + "\n" + " 2.>存款." + "\n" + " 3.>查询信息." + "\n" + " 4.>密码设置."
     62                 + "\n" + " 5.>退出系统." + "\n");
     63     }
     64     
     65     //登陆系统 ,3次密码错误就锁定系统
     66     protected void LoadSys() throws Exception {
     67         String card, pwd;
     68         //UserCard name;
     69         int counter = 0;
     70 
     71         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     72         do {
     73             System.out.println("请输入您的卡号:");
     74             card = br.readLine();
     75             System.out.println("请输入您的密码:");
     76             pwd = br.readLine();
     77             
     78             //卡号密码是否匹配
     79             if (!isRight(card,pwd)) {
     80                 System.out.println("您的卡号或密码输入有误.");
     81                 counter++;
     82             } else {
     83                 SysRun();
     84             }
     85                 
     86 
     87         } while (counter < 3);
     88     }
     89     
     90     /*
     91      * 卡号密码是否正确
     92      * 先判断卡号是否被注销或者挂失
     93      */
     94     protected boolean isRight(String card, String pwd) {
     95         //getCode明明是protected的竟然在这可以调用,难道是因为在该类里声明了user
     96         if (user.getCode().equals(card) && user.getPassword().equals(pwd))
     97             return true;
     98         else
     99             return false;
    100     }
    101     
    102     protected void SysRun() throws Exception {
    103         int num;
    104         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    105         System.out.println("请选择您要操作的项目(1-5):");
    106         num = br.read(); // 0的ASICC码为48,读取单个字符。
    107         switch (num) {//switch的选择因子为byte,short,int,char
    108             case 49:
    109                 user.withDraw(this);//指定当前ATM,我就纳闷this.ATM就不行了
    110                 break;
    111             case 50:
    112                 user.deposit(this);
    113                 break;
    114             
    115             case 51:
    116                 user.queryInfo(this);
    117                 break;
    118             case 52:
    119                 user.resetPwd(this);
    120                 break;
    121             case 53:
    122                 user.exitSys(this);//这里面的参数在只有一个ATM机时完全无用
    123                 break;
    124         }
    125         System.exit(1);
    126     }
    127     
    128 }
    129 
    130 
    131 class UserCard {//static也可修饰类,不过要加上其它控制符
    132     
    133     private String code = null; // 信用卡号
    134     private String name = null; // 客户姓名
    135     private String password = null; // 客户密码
    136     private double money = 0.0; // 卡里金额
    137     
    138     UserCard(String code, String name, String password, double money) {
    139                 this.code = code;
    140                 this.name = name;
    141                 this.password = password;
    142                 this.money = money;
    143     }
    144 
    145     protected String getCode() {
    146         return code;
    147     }
    148     /*//一个卡只有一个号
    149     protected void setCode(String code) {
    150         this.code = code;
    151     }
    152     */
    153 
    154     protected String getName() {
    155         return name;
    156     }
    157 
    158     protected void setName(String name) {
    159         this.name = name;
    160     }
    161 
    162     protected String getPassword() {
    163         return password;
    164     }
    165 
    166     protected void setPassword(String password) {
    167         this.password = password;
    168     }
    169 
    170     protected double getMoney() {
    171         return money;
    172     }
    173 
    174     protected void setMoney(double money) {
    175         this.money = money;
    176     }
    177     
    178     public void withDraw(ATM atm) throws Exception{//由于不知道如何指定ATM,所以默认
    179         
    180         boolean tag = false;
    181         /*
    182          * 先判断储户的钱是否够再判断ATM里的钱是否够
    183          */
    184         String str = null, str1;
    185         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    186         do {
    187             System.out.println("请输入您要取的数目:");
    188             str = br.readLine();
    189             str1 = String.valueOf(atm.getRemain());
    190             tag = (str.compareTo(str1) > 0) && (str.compareTo(String.valueOf(money))>0);
    191             if (!tag) {
    192                 System.out.println("超过已有的钱数,请重新输入您要取的数目:");
    193             } else {
    194                 atm.remain -= Long.decode(str);//既然直接包装类访问,就是静态方法,将 String 解码成 Long。
    195                 money -= Long.decode(str);
    196                 System.out.println("取款成功,请收好您的钱.");
    197                 atm.MyWelcome();
    198                 atm.SysRun();
    199             }
    200         } while (true);
    201         
    202     }
    203     
    204     protected void queryInfo(ATM atm) throws Exception{
    205         System.out.print("---------------------\n" + getCode() + "\n"
    206                 + getName() + "\n" + getCode() + "\n" + getCode() + "\n"
    207                 + "-----------------------");
    208     }
    209     
    210     //密码修改
    211     protected void  resetPwd(ATM atm) throws Exception {
    212         String pwd = null;
    213         int counter = 0;
    214         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    215         
    216         do {
    217             System.out.println("请输入旧密码:");
    218             pwd = br.readLine();
    219             if (this.getPassword().equals(pwd)) {
    220                 do {
    221                     System.out.println("请输入新密码:");
    222                     String pwd1 = br.readLine();
    223                     System.out.println("请再次输入新密码:");
    224                     String pwd2 = br.readLine();
    225                     if (!pwd1.equals(pwd2)) {//String里已经重写了equals方法
    226                         System.out.println("两次输入不一致,请再次输入.");
    227                     } else {
    228                         System.out.println("密码修改成功.");
    229                         atm.MyWelcome();
    230                         atm.SysRun();
    231                     }
    232                 } while (true);
    233             }else {
    234                 System.out.println("密码输入错误.");
    235             }
    236         } while (counter > 3);
    237         
    238     }
    239     
    240     public void deposit(ATM atm) throws IOException {
    241         String str = null;
    242         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    243         str = br.readLine();
    244         double increment = Double.valueOf(str);
    245         this.money += increment;
    246         System.out.println("你刚刚存入了" + increment + "元");;
    247         
    248     }
    249     
    250     protected void exitSys(ATM atm) throws Exception {
    251         System.out.println("感谢您使用本系统,欢迎下次在来,再见!");
    252         System.exit(0);
    253     }
    254     
    255 }

      总结:整个开发过程很是痛苦……不懂UML,完全凭想象。

  • 相关阅读:
    DataSet & DataTable &DataRow 深入浅出(转)
    使用三种方式搭建IIS web 网站
    淘宝系App图片为什么在北京电信网络加载这么慢?
    面向新手的Web服务器搭建(一)——IIS的搭建
    Ajax请求中的async:false/true的作用
    .net SerialPort
    .net常用属性
    Active Directory 常用属性
    .net加密
    MongoDb
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2940283.html
Copyright © 2011-2022 走看看