制作一个简易的ATM取款机

这是一个Java实现的ATM系统,包括用户登录、注册账户功能以及取款、存款、转账和修改密码等操作。系统使用ArrayList存储Account对象,并通过Scanner获取用户输入。用户可以进行开户,输入卡号和密码进行登录,登录后可执行转账、取款、存款等操作。
摘要由CSDN通过智能技术生成

 ATM取款机主函数

package ATM;

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

public class ATMSystem {
    public static void main(String[] args) {
        ArrayList<Account> accounts = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        showMain(accounts, sc);

    }

    public static void showMain(ArrayList<Account> accounts, Scanner sc) {
        while (true) {
            System.out.println("请您输入您想要做的操作:");
            System.out.println("1、登录账户");
            System.out.println("2、注册账户");
            System.out.print("您可以输入命令了:");
            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 register(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("===============用户开户功能==============");

        System.out.println("请您输入开户名称:");
        String password = "";
        String name = sc.next();
        while (true) {
            System.out.println("请您输入用户密码:");
            password=sc.next();
            System.out.println("请您输入确认密码:");
            String okpassword = sc.next();

            if (okpassword.equals(password)) {
                break;
            } else {
                System.out.println("两次密码必须一致---");
            }
        }
        System.out.println("请您输入每次限额:");
        double quotaMoney = sc.nextDouble();
        String cardId = createCarId(accounts);
        Account account = new Account(cardId, name, password, quotaMoney);
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCarId() + "请您妥善保管");
    }

    public static String createCarId(ArrayList<Account> accounts) {
        while (true) {
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++) {
                cardId += r.nextInt(10);
            }
            Account acc = getAccountBuCardId(cardId, accounts);
            if (acc == null) {
                return cardId;
            }
        }
    }

    public static Account getAccountBuCardId(String cardId, ArrayList<Account> accounts) {
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCarId().equals(cardId)) {
                return acc;
            }
        }
        return null;
    }

    public 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 = getAccountBuCardId(cardId, accounts);
            if (acc != null) {
                System.out.println("请您输入登录的密码:");
                String password = sc.next();
                if (acc.getPassWord().equals(password)) {
                    System.out.println("恭喜您," + acc.getUserName() + "先生成功进入系统,您的卡号是:" + acc.getCarId());
                    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.printf("请您输入操作命令:");
            int comand = sc.nextInt();
            switch (comand) {
                case 1:
                    showAccount(acc);
                    break;
                case 2:
                    deposit(acc, sc);
                    break;
                case 3:
                    withdrawal(acc, sc);
                    break;
                case 4:
                    transfer(acc, sc, accounts);
                    break;
                case 5:
                    changePassword(acc, sc, accounts);
                    return;
                case 6:
                    System.out.println("欢迎下次光临!!");
                    return;
                case 7:
                    accounts.remove(acc);
                    System.out.println("销户成功!");
                    return;
                default:
                    System.out.println("您输入的命令有误");
            }
        }
    }

    private static void changePassword(Account acc, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("========欢迎您进入修改密码界面========");
        while (true) {
            System.out.println("请您输入当前账户的密码:");
            String password = sc.next();
            if (acc.getPassWord().equals(password)) {
                while (true) {
                    System.out.println("请你输入新的密码:");
                    String password1 = sc.next();
                    System.out.println("请您确认密码:");
                    String password2 = sc.next();
                    if (password1.equals(password2)) {
                        acc.setPassWord(password1);
                        System.out.println("恭喜您,密码修改成功,请重新登录");
                        return;
                    } else {
                        System.out.println("两次密码输入不一致,请重新输入:");
                    }
                }

            } else {
                System.out.println("您输入的密码错误,请重新输入:");
            }
        }
    }


    private static void transfer(Account acc, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("======欢迎您进入黑马银行用户转账界面======");
        if (accounts.size() < 2) {
            System.out.println("当前系统,账户不足2个,不能转账!");
            return;
        }
        if (acc.getMoney() <= 0) {
            System.out.println("您自己都没钱,就别转了!");
            return;
        } else {
            while (true) {
                System.out.println("请您输入转账的账户卡号:");
                String cardId = sc.next();
                Account acct = getAccountBuCardId(cardId, accounts);
                if (acc.getCarId().equals(acct.getCarId())) {
                    System.out.println("您不可以为自己转账!");
                }
                if (acct == null) {
                    System.out.println("您输入的卡号有误,请重新输入");
                    continue;
                } else {
                    System.out.println("您当前要为【*" + acct.getUserName().substring(1) + "】转账!");
                    while (true) {
                        System.out.println("请您输入姓氏确认:");
                        String name = sc.next();
                        if (acct.getUserName().charAt(0) == name.charAt(0)) {
                            System.out.println("请您确认是否转账:");
                            String s = sc.next();
                            if (s.equals("是"))
                                transferMoney(acc, acct, sc);

                            else
                                System.out.println("您已退出转账系统:");

                            return;

                        } else {
                            System.out.println("输入姓氏错误,请您重新输入:");
                        }
                    }
                }
            }
        }
    }

    private static void transferMoney(Account acc, Account acct, Scanner sc) {
        while (true) {
            System.out.println("请您输入转账的金额");
            double money = sc.nextDouble();
            if (money > acc.getQuotaMoney())
                System.out.println("您当前转账超过了当前限额!最多可转:" + acc.getQuotaMoney());
            else {
                if (money < acc.getMoney()) {
                    acc.setMoney(acc.getMoney() - money);
                    System.out.println("恭喜您,转账" + money + "成功!当前账户还剩余:" + acc.getMoney());
                    return;
                } else {
                    System.out.println("您的余额不足,最多可转账:" + acc.getMoney());
                }
            }
        }
    }


    private static void withdrawal(Account acc, Scanner sc) {
        System.out.println("======欢迎您进入黑马银行用户取款界面======");

        if (acc.getMoney() < 100) {
            System.out.println("账户余额不足100元,就别取啦吧!");
            return;
        } else {
            while (true) {
                System.out.println("请您输入取款的金额");
                double money = sc.nextDouble();
                if (money > acc.getQuotaMoney())
                    System.out.println("您当前取款超过了当前限额!最多可取:" + acc.getQuotaMoney());
                else {
                    if (money < acc.getMoney()) {
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您,取款" + money + "成功!当前账户还剩余:" + acc.getMoney());
                        return;
                    } else {
                        System.out.println("您的余额不足,最多可取:" + acc.getMoney());
                    }
                }
            }
        }
    }

    private static void deposit(Account acc, Scanner sc) {
        System.out.println("======欢迎您进入黑马银行用户存款界面======");
        System.out.println("请您输入存款的金额");
        double money = sc.nextDouble();
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!!");
        showAccount(acc);
    }

    private static void showAccount(Account acc) {
        System.out.println("================当前账户详情==================");
        System.out.println("卡号:" + acc.getCarId());
        System.out.println("姓名:" + acc.getUserName());
        System.out.println("余额:" + acc.getMoney());
        System.out.println("当次限额:" + acc.getQuotaMoney());
    }
}

ATM取款机对象类

package ATM;

public class Account {
    private String carId;
    private String userName;
    private String passWord;
    private double money;
    private double quotaMoney;

    public Account() {
    }

    public Account(String carId, String userName, String passWord, double quotaMoney) {
        this.carId = carId;
        this.userName = userName;
        this.passWord = passWord;
        this.quotaMoney = quotaMoney;
    }

    public String getCarId() {
        return carId;
    }

    public void setCarId(String carId) {
        this.carId = carId;
    }

    public String getUserName() {
        return userName;
    }

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

    public String getPassWord() {
        return passWord;
    }

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

    public double getMoney() {
        return money;
    }

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

    public double getQuotaMoney() {
        return quotaMoney;
    }

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱发博客的嗯哼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值