JAVA ATM剧情文字对话操作系统

分别开两个类:

效果如图:

第一个类:

package ATMSystem;

public class Account {
    private String cardId;//账号ID
    private String userName;//用户名称
    private String passWord;//用户密码
    private double money;//剩余金额
    private double quotaMoney;//每次取现额度

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public void setQuotaMoney(double quotaMoney) {
        this.quotaMoney = quotaMoney;
    }

    public String getCardId() {
        return cardId;
    }

    public String getUserName() {
        return userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public double getMoney() {
        return money;
    }

    public double getQuotaMoney() {
        return quotaMoney;
    }
}

第二个类:

 

package ATMSystem;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 ATM系统的入口类
 */

public class ATMSystems {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Account> accounts = new ArrayList<>();
        while (true) {
            System.out.println("================ATM系统==============");
            System.out.println("1:账户登入");
            System.out.println("2:账户开户");

            int command = sc.nextInt();//输入命令
            switch (command){
            case 1:
                login(accounts, sc);
                //用户登入操作
                break;
            case 2:
                register(accounts, sc);
                //用户账户开户
                break;
            default:
                System.out.println("您输入的命令不存在,请重新输入");
            }
        }
    }

    private static void login(ArrayList<Account> accounts, Scanner sc) {//登入功能
        if (accounts.size() == 0)
        {
            System.out.println("对不起,当前系统中没有账户,请先开户~ ~");
            return ;
        }

        while (true) {
            System.out.println("请输入登入卡号");
            String cardId = sc.next();
            Account acc = GetaccountbycardId(accounts, cardId);
            if (acc != null)
            {
                while (true) {
                    System.out.println("请输入密码");
                    String password = sc.next();
                    if (acc.getPassWord().equals(password))
                    {
                        System.out.println("恭喜您进入ATM系统" + acc.getUserName() + "先生/女士, 您的卡号是:" + acc.getCardId() + ", 登入成功" );

                        showUserCommand(sc, acc, accounts);
                        return ;
                    }
                    else
                    {
                        System.out.println("您输入的密码不正确,请重新输入");
                    }
                }
            }
            else {
                System.out.println("您输入的账户不存在,请重新输入");
            }
        }

    }

    private static void showUserCommand(Scanner sc, Account acc, ArrayList<Account> accounts) {
        while (true) {
            System.out.println("=============用户操作页==============");
            System.out.println("1: 查询账户");
            System.out.println("2: 存款");
            System.out.println("3: 取款");
            System.out.println("4: 转账");
            System.out.println("5: 修改密码");
            System.out.println("6: 退出");
            System.out.println("7: 注销账户");
            System.out.println("请选择");

            int command = sc.nextInt();

            switch (command){
                case 1:
                    showAccount(acc);
                    break;
                case 2:
                    storageMoney(acc, sc);
                    break;
                case 3:
                    getMoney(acc, sc);
                    break;
                case 4:
                    switchMoney(acc, sc, accounts);
                    break;
                case 5:
                    modifyPassWord(acc, sc);
                    break;
                case 6:
                    System.out.println("退出成功");
                    return;
                case 7:
                    deleteAccount(acc, sc, accounts);
                    return;
                default:
                    System.out.println("输入的命令有误请重新输入");
            }
        }
    }


    private static void deleteAccount(Account acc, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("===============用户注销=============");
        System.out.println("您确定要销户?, Y/N");
        String option = sc.next();
        if (option.equals("Y"))
        {
            if (acc.getMoney() > 0)
            {
                System.out.println("您账户中还有钱没有去玩,不允许销户~~~~");
                showAccount(acc);
                return;
            }
            else
            {
                accounts.remove(acc);
                System.out.println("您的账户销户完成~~~");
                return;
            }
        }
        else
        {
            System.out.println("好的,您的当前账户继续保留");
            showAccount(acc);
            return;
        }
    }

    private static void modifyPassWord(Account acc, Scanner sc) {
        System.out.println("==============欢迎来到密码修改系统================");
        while (true) {
            System.out.println("请输入您当前的密码");
            String nowpassword = sc.next();
            if (acc.getPassWord().equals(nowpassword))
            {
                System.out.println("密码输入正确");
                System.out.println("请输入修改后的密码");
                String modifyps = sc.next();
                acc.setPassWord(modifyps);
                System.out.println("恭喜您, 密码修改成功");
                showAccount(acc);
                return;
            }
            else
            {
                System.out.println("您输入的密码有误,请重新输入");
            }
        }
    }

    private static void switchMoney(Account acc, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("==================欢迎来到转账系统===================");
        if (accounts.size() < 2)
        {
            System.out.println("对不起现在账户数量不足两个,请先开户");
            return;
        }
        if (acc.getMoney() == 0)
        {
            System.out.println("对不起,您的账户余额不足,请先存款");
            showAccount(acc);
            return ;
        }
        while (true) {
            System.out.println("请输入您要存入的账户号码:");
            String cardID = sc.next();
            boolean flag = false;
            int i;
            for (i = 0; i < accounts.size(); i ++ )
            {
                if (accounts.get(i).getCardId().equals(cardID))
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
            {
                System.out.println("请输入您要转账的金额");
                double monkey = sc.nextDouble();
                if (acc.getQuotaMoney() < monkey)
                {
                    System.out.println("您的限额过大, 请重新输入转账卡号");
                }
                else if (acc.getMoney() < monkey)
                {
                    System.out.println("您的金额不足,请重新输入");
                }
                else
                {
                    accounts.get(i).setMoney(accounts.get(i).getMoney() + monkey);
                    acc.setMoney(acc.getMoney() - monkey);
                    System.out.println("您成功将: " + monkey + "转入卡号为:" + accounts.get(i).getCardId() + "的账户中");
                    showAccount(acc);
                    return;
                }
            }
            else
            {
                System.out.println("您输入的卡号不存在,请重新输入");
            }
        }
    }

    private static void getMoney(Account acc, Scanner sc) {
        System.out.println("================欢迎来到取款系统==================");
        while (true) {
            System.out.println("请输入取款的金额");
            double getmonkey = sc.nextDouble();
            if (acc.getQuotaMoney() < getmonkey)
            {
                System.out.println("您取款的金额超过预定限额,您的预定限额为:" + acc.getQuotaMoney());
                break;
            }
            if (acc.getMoney() < getmonkey)
            {
                System.out.println("您的余额不足请重新输入,您当前的余额为: " + acc.getMoney());
                break;
            }
            else
            {
                acc.setMoney(acc.getMoney() - getmonkey);
                System.out.println("取款成功您当前的余额为:" + acc.getMoney());
                showAccount(acc);
                return ;
            }
        }
    }

    private static void storageMoney(Account acc, Scanner sc) {
        System.out.println("==============用户存钱操作===============");
        System.out.println("请输入存款金额");
        double monkeys = sc.nextDouble();
        acc.setMoney(acc.getMoney() + monkeys);
        System.out.println("恭喜: " + acc.getUserName() + "先生/女士存款成功,您现在的余额是:" + acc.getMoney());
        showAccount(acc);
        return;

    }

    private static void showAccount(Account acc) {
        System.out.println("==============当前账号信息如下============");
        System.out.println("卡号: " + acc.getCardId());
        System.out.println("户主: " + acc.getUserName());
        System.out.println("余额: " + acc.getMoney());
        System.out.println("限额: " + acc.getQuotaMoney());
    }

    /**
     * 用户开户功能的实现
     * @param accounts 接收账户集合
     */
    private static void register(ArrayList<Account> accounts, Scanner sc) {//注册功能
        Account account = new Account();

        System.out.println("请输入用户名");
        String username = sc.next();
        account.setUserName(username);

        while (true) {
            System.out.println("请输入密码");
            String password = sc.next();
            System.out.println("请确定密码");
            String okpassword = sc.next();
            if (password.equals(okpassword))
            {
                System.out.println("密码正确");
                account.setPassWord(password);
                break;
            }
            else {
                System.out.println("密码不一致,请重新输入");
            }
        }

        System.out.println("请输入每次取现额度");
        double quote = sc.nextDouble();
        account.setQuotaMoney(quote);

        String cardId = GetRandomCard(accounts);
        account.setCardId(cardId);

        //把账户对象添加到账户集合中去
        accounts.add(account);
        System.out.println("恭喜您, " + username + "先生/女生,您开户成功,您的卡号是:" + cardId + ", 请您妥善保管卡号");
    }

    public static String GetRandomCard(ArrayList<Account> accounts){//随机得到一个ID
        Random rd = new Random();
        while (true) {
            String cardID = "";
            for (int i = 0; i < 8; i ++ )
            {
                int rds = rd.nextInt(10);
                cardID += rds;
            }
            Account acc = GetaccountbycardId(accounts, cardID);
            if (acc == null) return cardID;
        }

    }

    public static Account GetaccountbycardId(ArrayList<Account> accounts, String cardId){//判断ID是否重复
            for (int i = 0; i < accounts.size(); i ++ )
            {
                Account acc = accounts.get(i);
                if (acc.getCardId().equals(cardId))
                {
                    return acc;
                }
            }

            return null;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啥也不会hh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值