• 实验01——java模拟银行ATM系统


       用java写的一个模拟银行系统,比较初级。

    ATM.java

      1 package cn.tedu.yinhang;
      2 
      3 import java.util.Scanner;
      4 
      5 /**
      6  * @author 赵瑞鑫 E-mail:1922250303@qq.com
      7  * @version 1.0
      8  * @创建时间:2020年7月27日 下午7:04:27
      9  * @类说明:
     10  */
     11 public class ATM implements PayTelInter {
     12     private UnionCard card;
     13     Scanner sc = new Scanner(System.in);
     14 
     15     // 插卡,不同卡片显示不同的menu
     16     public void insertCard(UnionCard card) {
     17         this.card = card;
     18         // 测试卡片是否为BOC,
     19         // BOC为Card的实例对象
     20         if (card instanceof BOC) {
     21             checkPWA();
     22         }
     23     }
     24 
     25     public void checkPWA() {
     26         Scanner sc = new Scanner(System.in);
     27         System.out.println("请输入密码:");
     28         int pw = sc.nextInt();
     29         if (card.checkPw(pw)) {
     30             while (true)
     31                 showBocMenu();
     32         } else {
     33             System.out.println("密码错误");
     34         }
     35     }
     36 
     37     // BOC菜单
     38     public void showBocMenu() {
     39         /*Scanner sc = new Scanner(System.in);
     40         System.out.println("请输入密码:");
     41         int pw = sc.nextInt();
     42         if (card.checkPw(pw)) {
     43             while (true) {*/
     44                 System.out.println("*******************************");
     45                 System.out.println("        云尚天成银行一体化试行系统            ");
     46                 System.out.println("           操作1 余额查询");
     47                 System.out.println("           操作2 存    款");
     48                 System.out.println("           操作3 取    款 ");
     49                 System.out.println("           操作4 生活缴费");
     50                 System.out.println("           操作5 退    出");
     51                 System.out.println("请您选择操作数:");
     52                 int key = sc.nextInt();
     53                 switch (key) {
     54                 case 1:
     55                     showBalance();
     56                     System.out.println("输入0返回:");
     57                     int n = sc.nextInt();
     58                     if(n==0)showBocMenu();
     59                     break;
     60                 case 2:
     61                     saveBalance();
     62                     System.out.println("输入0返回:");
     63                     int n1 = sc.nextInt();
     64                     if(n1==0)showBocMenu();
     65                     break;
     66                 case 3:
     67                     takeBalance();
     68                     System.out.println("输入0返回:");
     69                     int n2 = sc.nextInt();
     70                     if(n2==0)showBocMenu();
     71                     break;
     72                 case 4:
     73                     payMethod();
     74                     System.out.println("输入0返回:");
     75                     int n4 = sc.nextInt();
     76                     if(n4==0)showBocMenu();
     77                     break;
     78                 case 5:
     79                     System.exit(0);
     80                     break;
     81                 default:
     82                     System.out.println("请选择合法操作!");
     83                     System.out.println("输入0返回:");
     84                     int n5 = sc.nextInt();
     85                     if(n5==0)showBocMenu();
     86                     break;
     87                 }
     88             }
     89 
     90         /*} else {
     91             System.out.println("密码错误");
     92         }*/
     93 
     94     
     95     // 显示余额
     96     private void showBalance() {
     97         System.out.println(card.getBalance());
     98     }
     99 
    100     // 存钱
    101     private void saveBalance() {
    102         Scanner sc = new Scanner(System.in);
    103         System.out.println("请存款:(提示:存款数值应不小于100且为100的倍数)");
    104         int money = sc.nextInt();
    105         card.saveMoney(money);
    106         System.out.println("成功!");
    107         System.out.println("您当前余额为:");
    108         System.out.println(card.getBalance());
    109     }
    110 
    111     // 取钱
    112     private void takeBalance() {
    113         Scanner sc = new Scanner(System.in);
    114         System.out.println("请取款:(提示:取款数值应不小于100且为100的倍数)");
    115         int money = sc.nextInt();
    116         card.takeMoney(money);
    117         System.out.println("您当前余额为:");
    118         System.out.println(card.getBalance());
    119     }
    120 
    121     private void payMethod() {
    122         System.out.println("*******************************");
    123         System.out.println("        云尚天成银行ATM系统            ");
    124         System.out.println("           操作1 手机话费");
    125         System.out.println("           操作2  水电费");
    126         System.out.println("           操作3  医疗费 ");
    127         System.out.println("           操作4  煤气费");
    128         System.out.println("           操作5  返    回");
    129         System.out.println("请您选择操作数:");
    130         int key = sc.nextInt();
    131         switch (key) {
    132         case 1:
    133             Scanner sc = new Scanner(System.in);
    134             System.out.println("请输入充值数额:");
    135             int money = sc.nextInt();
    136             payTel(money);
    137             System.out.println("成功!");
    138             System.out.println("您当前余额为:");
    139             System.out.println(card.getBalance());
    140             break;
    141         case 2:
    142 
    143             break;
    144         case 3:
    145 
    146             break;
    147         case 4:
    148 
    149             break;
    150         case 5:
    151 
    152             break;
    153         default:
    154             break;
    155         }
    156     }
    157 
    158     @Override
    159     public boolean payTel(int money) {
    160         // TODO Auto-generated method stub
    161         if (money<0&&money%100!=0) {
    162             System.err.println("数值不合法!");
    163         }else{
    164             double balance = card.getBalance();
    165             balance -= money;
    166             card.setBalance(balance);
    167         }
    168         return false;
    169     }
    170 
    171 }
    ATM.java

    BOC.java

     1 package cn.tedu.yinhang;
     2 
     3 /**
     4  * @author 赵瑞鑫 E-mail:1922250303@qq.com
     5  * @version 1.0
     6  * @创建时间:2020年7月27日 下午6:58:32
     7  * @类说明:中国银行卡类
     8  */
     9 public class BOC extends UnionCard {
    10     //构造方法传值
    11     public BOC(String id, double balance, int pw) {
    12         // TODO Auto-generated constructor stub
    13         super(id, balance, pw);
    14     }
    15 }
    BOC.java

    MainClass.java

     1 package cn.tedu.yinhang;
     2 
     3 import cn.tedu.yinhang.ATM;
     4 import cn.tedu.yinhang.BOC;
     5 
     6 /**
     7  * @author 赵瑞鑫 E-mail:1922250303@qq.com
     8  * @version 1.0
     9  * @创建时间:2020年7月27日 下午7:53:08
    10  * @类说明: 测试类
    11  */
    12 public class MainClass {
    13 
    14     public static void main(String[] args) {
    15         // TODO Auto-generated method stub
    16         new ATM().insertCard(new BOC("0001", 200, 123));
    17 
    18     }
    19 
    20 }
    MainClass.java

    PayTelInter.java

     1 package cn.tedu.yinhang;
     2 
     3 /**
     4  * @author 赵瑞鑫 E-mail:1922250303@qq.com
     5  * @version 1.0
     6  * @创建时间:2020年7月27日 下午7:02:50
     7  * @类说明: 交电话费接口
     8  */
     9 public interface PayTelInter {
    10     public abstract boolean payTel(int money);
    11 }
    PayTelInter

    UnionCard.java

     1 package cn.tedu.yinhang;
     2 
     3 /**
     4  * @author 赵瑞鑫 E-mail:1922250303@qq.com
     5  * @version 1.0
     6  * @创建时间:2020年7月27日 下午6:45:08
     7  * @类说明: UnionCard类,银联卡
     8  */
     9 public class UnionCard {
    10     private String id;// 卡号
    11     private double balance;// 余额
    12     private int pw;// 密码
    13     
    14     
    15     // 初始化成员变量
    16     public UnionCard(String id, double balance, int pw) {
    17         super();
    18         this.id = id;
    19         this.balance = balance;
    20         this.pw = pw;
    21     }
    22 
    23     // set,get方法
    24     public String getId() {
    25         return id;
    26     }
    27 
    28     public void setId(String id) {
    29         this.id = id;
    30     }
    31 
    32     public double getBalance() {
    33         return balance;
    34     }
    35 
    36     public void setBalance(double balance) {
    37         this.balance = balance;
    38     }
    39 
    40     public int getPw() {
    41         return pw;
    42     }
    43 
    44     public void setPw(int pw) {
    45         if (pw<0&&pw>6) {
    46             System.err.println("您输入的密码有误,请检查。");
    47         }else{
    48             this.pw = pw;
    49         }
    50     }
    51 
    52     // 存钱的方法
    53     public void saveMoney(int money) {
    54         if (money%100==0) {
    55             balance += money;
    56         }else{
    57             System.err.println("数值不合法!");
    58         }
    59     }
    60 
    61     // 取钱的方法
    62     public void takeMoney(int money) {
    63         if (money%100==0) {
    64             balance -= money;
    65         }else{
    66             System.out.println("数值不合法!");
    67         }
    68     }
    69 
    70     // 验证密码是否相同,相同返回true,否则返回false
    71     public boolean checkPw(int pw) {
    72         if (pw == this.pw) {
    73             return true;
    74         } else {
    75             return false;
    76         }
    77     }
    78 }
    UnionCard.java
    作者:赵瑞鑫。支持原创,从你我做起。
  • 相关阅读:
    Export excel的终级解决方案
    Power Designer 常见问题
    正反向数据库及生成设计报告
    寻求最佳开发模式,免得落得“精”尽人亡
    XmlNode与XmlElement的区别总结
    软件版本的定义:
    我发现博客园一个很严重的问题
    sql 行转列的终极写法
    js 自定义 $ 选择器
    可持续集成环境相关文章
  • 原文地址:https://www.cnblogs.com/Winer-Jiu/p/13387393.html
Copyright © 2020-2023  润新知