zoukankan      html  css  js  c++  java
  • ATM机

    需求:模拟ATM机的存取钱功能
    用户类
    /**
    * 用户类
    * @author hp
    *
    */
    public class Person {
    String name=”zs”;//姓名
    int pwd=123456;//密码
    }
    import java.util.Scanner;
    ATM类
    /**
    * ATM类
    *
    * @author hp
    *
    */
    public class ATM {
    double lastMoney = 1000;// 账户余额

    /**
     * 插卡开始
     */
    public void start() {
        boolean flag = login();
        if (flag) {
            Scanner sc = new Scanner(System.in);
            int num = 0;
            do {
                System.out.println("1.查询	" + "2.存款	" + "3.取款	" + "0.退出");
                System.out.print("请选择你需要办理的业务:");
                num = sc.nextInt();
                switch (num) {
                case 0:
                    exit();
                    break;
                case 1:
                    showMoney();
                    break;
                case 2:
                    addMoney();
                    break;
                case 3:
                    takeMoney();
                    break;
                default:
                    System.out.println("您输入数字有误!");
                    break;
                }
            } while (num != 0);
        }
    
    }
    
    /**
     * 登录
     */
    public boolean login() {
        Person p = new Person();
        boolean flag = false;
        Scanner sc = new Scanner(System.in);
        for (int i = 0; (i < 3) && (!flag); i++) {
            System.out.println("请输入用户名:");
            String nowName = sc.next();
            if (p.name.equals(nowName)) {
                for (int j = 0; j < 3; j++) {
                    System.out.println("请输入密码:");
                    int nowPWD = sc.nextInt();
                    if (nowPWD == p.pwd) {
                        System.out.println("登录成功");
                        flag = true;
                        break;
                    } else {
                        System.out.println("密码错误,您还有" + (2 - i) + "次机会!");
                        i++;
                    }
                }
            } else {
                System.out.println("用户名错误,您还有" + (2 - i) + "次机会!");
            }
        }
    
        return flag;
    }
    
    /**
     * 查询方法
     */
    public void showMoney() {
        System.out.println("****
    当前余额为:" + lastMoney + "****
    ");
    }
    
    /**
     * 存钱方法
     */
    public void addMoney() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入存款金额:");
        double money = sc.nextDouble();
        if (money % 100 == 0) {
            lastMoney += money;
            System.out.println("存款成功");
        } else {
            System.out.println("存款失败");
        }
        System.out.println("
    ****当前余额为:" + lastMoney + "****");
    }
    
    /**
     * 取钱方法
     */
    public void takeMoney() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入取款金额:");
        double money = sc.nextDouble();
        if (money > lastMoney || money % 100 != 0) {
            System.out.println("取款失败");
        } else {
            lastMoney -= money;
            System.out.println("取款成功");
        }
        System.out.println("
    ****当前余额为:" + lastMoney + "****
    ");
    }
    
    /**
     * 退出方法
     */
    public void exit() {
        System.out.println("取卡,谢谢使用!");
    }
    

    }
    ATM测试类
    /**
    * ATM测试类
    *
    * @author hp
    *
    */
    public class ATMTest {
    public static void main(String[] args) {
    ATM atm = new ATM();
    atm.start();
    }
    }
    运行图
    这里写图片描述

  • 相关阅读:
    Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments环境代码详解
    zc.buildout构建项目时报错‘AttributeError: '_NamespacePath' object has no attribute 'sort'’
    利用Jenkins打包ISO和QCOW2镜像文件
    解决python pip安装提示"not a supported wheel on this platform"
    Kali 2017.3开启VNC远程桌面登录
    Jenkins邮件扩展插件Email Extension Plugin配置使用
    Jenkins执行sudo权限的设置
    如何解决源码安装软件中make时一直重复打印configure信息
    CentOS 7下安装配置proftpd搭建ftp服务器
    如何使用capedit分割数据包文件
  • 原文地址:https://www.cnblogs.com/wangqilong/p/8279808.html
Copyright © 2011-2022 走看看