Java超简易命令行银行存取款模拟

引言

之前做的一个小程序,超级简单。初始账号密码都在main函数里。

代码

import java.util.*;

public class Main {
    private static HashMap<String, Card> map = new HashMap<String, Card>();// 所有的银行卡
    private static Card current = null;// 当前的银行卡
    private static Scanner in = new Scanner(System.in);

    public static void MainMenu() {
        while (true) {
            System.out.println("  **************************************************************************");
            System.out.println("  *                                                                        *");
            System.out.println("  *                          1            登录                             *");
            System.out.println("  *                          0            退出                             *");
            System.out.println("  *                                                                        *");
            System.out.println("  **************************************************************************");
            int i = -1;
            while (i < 0 || i > 1) {
                i = in.nextInt();
            }
            switch (i) {
            case 1:
                String Id, Password;
                System.out.print("请输入卡号:");
                Id = in.next();
                System.out.print("请输入密码:");
                Password = in.next();
                if (map.containsKey(Id)) {
                    if (map.get(Id).login(Id, Password)) {
                        System.out.println("登录成功");
                        current = map.get(Id);
                        Menu();
                    } else {
                        System.out.println("密码输入错误!");
                    }
                } else {
                    System.out.println("不存在此卡号!");
                }
                break;
            case 0:
                System.exit(0);
                break;
            }
        }
    }

    public static void Menu() {
        int Money;
        while (true) {
            System.out.println("  **************************************************************************");
            System.out.println("  *                                                                        *");
            System.out.println("  *                     欢迎您!    您的卡号是:" + current.getId() + "                     *");
            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("  *                                                                        *");
            System.out.println("  **************************************************************************");
            int i = -1;
            while (i < 0 || i > 4) {
                i = in.nextInt();
            }
            switch (i) {
            case 1:
                System.out.print("请输入取款数:");
                Money = in.nextInt();
                if (current.getBalance() >= Money) {
                    current.withdraw(Money);
                    System.out.println("成功取出" + Money + "元");
                } else {
                    System.out.println("账户余额不足!");
                }
                break;
            case 2:
                System.out.print("请输入存款数:");
                Money = in.nextInt();
                if (Money < 0) {
                    System.out.println("存款数不能为负数!");
                } else {
                    current.save(Money);
                    System.out.println("成功存入" + Money + "元");
                }
                break;
            case 3:
                System.out.println("您的余额为:" + current.getBalance() + "元");
                break;
            case 4:
                String p1, p2;
                System.out.print("请输入新密码:");
                p1 = in.next();
                System.out.print("请再输入一次新密码:");
                p2 = in.next();
                if (!p1.equals(p2)) {
                    System.out.println("两次输入不一致!");
                    break;
                }
                current.changePassword(p2);
                System.out.println("成功修改密码!");
                break;
            case 0:
                MainMenu();
                break;
            }
        }
    }

    public static void main(String[] args) {
        map.put("621001", new Card("621001", "123456", 1000));
        map.put("621002", new Card("621002", "123456", 800));
        map.put("621003", new Card("621003", "123456", 5000));
        map.put("621004", new Card("621004", "123456", 100000));
        map.put("621005", new Card("621005", "123456", 300));
        MainMenu();
    }

}

class Card {
    private String Id; // 卡号
    private String PassWord; // 密码
    private int Balance; // 余额

    public Card(String id, String passWord, int balance) {// 构造方法
        super();
        Id = id;
        PassWord = passWord;
        Balance = balance;
    }

    public int getBalance() { // 返回余额

        return Balance;
    }

    public void changePassword(String p) { // 修改密码
        PassWord = p;
    }

    public String getId() {// 返回卡号
        return Id;
    }

    public boolean login(String Id, String PassWord) { // 登录,返回成功true或失败false
        if (this.Id.equals(Id) && this.PassWord.equals(PassWord)) {
            return true;
        }
        return false;
    }

    public void withdraw(int money) { // 取钱扣款
        Balance -= money;
    }

    public void save(int money) { // 存钱
        Balance += money;
    }

};

发表评论

电子邮件地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部