ATM系统

  ATM系统:

 

系统准备:

首页设计:

 

开户功能:

 

 开户操作:

 

 

 

 登录功能:

补充一个步骤来判断当前系统如果没有任何账号该怎么办

补充第五条:

 

 用户操作页  查询以及操作功能

  public static void login(ArrayList<Account> accounts,Scanner scanner){
        //先判断系统中是否有账号
        if(accounts.size()==0){
            return;
        }
        System.out.println("请输入您的卡号:");
        String cardId=scanner.next();
        //2.根据卡号查询账户对象
        Account leo=getAccountByCardId(cardId,accounts);
        //3.判断是否有这个账号
        if(leo!=null){
            //4.让用户输入密码
            System.out.println("请您输入登录的密码:");
            String password=scanner.next();
            //5.判断密码是否与账号中的密码相同
            if(leo.getPassword().equals(password)){
                System.out.println("恭喜您"+leo.getUserName()+"先生或女士成功进入系统,您的卡号是:"+leo.getCard());
                showUsercommand(scanner,leo);
                return;
            } else {
                System.out.println("您的密码输入有误 请确认");
            }
        } else {
            System.out.println("对不起 该账号不存在");
        }
    }
    public static void showUsercommand(Scanner scanner,Account leo) {
        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.注销账号");
        while (true) {
            System.out.println("请您输入操做命令");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    //查询账户
                    showAccount(leo);
                    break;
                case 2:
                    //存款
                    break;
                case 3:
                    //取款
                    break;
                case 4:
                    //转账
                    break;
                case 5:
                    //修改密码
                    break;
                case 6:
                    //退出
                    System.out.println("欢迎下次光临");
                   return;//结束当前操作
                case 7:
                    //注销账号
                    break;
                default:
                    System.out.println("您输入错误 请重新输入");
            }
        }
    }
    public static void showAccount(Account leo){
        System.out.println("==============当前账号详情====================");
        System.out.println("卡号:"+leo.getCard());
        System.out.println("姓名:"+leo.getUserName());
        System.out.println("余额:"+leo.getMoney());
        System.out.println("当次限额:"+leo.getQuotaMoney());
    }
}

 用户存款: 

取款

 

转账操作:

大纲:

详细写出第三步:

 

 用户密码的修改以及销毁

 

销户:

 

标准注释:/**再加上一个回车即可 

总代码如下:

Accont.java:

package ATM;

public class Account {
    //定义属性
    private String card;
    private String userName;
    private String password;
    private double money;
    private double quotaMoney;//单次取款限额

    public Account() {
    }

    public Account(String card, String userName, String password,double quotaMoney) {
        this.card = card;
        this.userName = userName;
        this.password = password;
        this.quotaMoney = quotaMoney;
    }

    public String getCard() {
        return card;
    }

    public void setCard(String card) {
        this.card = card;
    }

    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;
    }
}

ATMsystem.java:

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<>();
        Showmain(accounts);
    }

    /**
     *
     * @param accounts
     */
                              //把这个集合容器给操作页面
    public static void Showmain(ArrayList<Account> accounts) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请您输入您想做的操作:");
        System.out.println("1.登录");
        System.out.println("2.开户");
        System.out.print("您可以输入命令了:");
        while (true) {
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    login(accounts,scanner);
                    break;
                case 2:
                    register(accounts, scanner);
                    break;
                default:
                    System.out.println("您输入错误 请重新输入");
            }
        }
    }

    /**
     * 开户操作
     * @param accounts
     * @param scanner
     */
    public static void register(ArrayList<Account> accounts, Scanner scanner) {
        System.out.println("================用户开户功能===============");
        //2.键盘输入用户名 密码 确认密码
        System.out.println("请输入开户的户主姓名");
        String name = scanner.next();
        String password = "";
        while (true) {
            System.out.println("请输入开户密码");
            password = scanner.next();
            System.out.println("请再输入一次密码来确认一下");
            String okpassword = scanner.next();
            if (password.equals(okpassword)) {
                break;
            } else {
                System.out.println("两次输入密码不一致");
            }
        }
        //3.生成的卡号 卡号是后8位 而且不能和其他卡号重复
        String cardId = createCard(accounts);
        System.out.println("请输入单次的取款限额");
        Double  quotaMoney=scanner.nextDouble();
        //4.创建一个账号对象封装账号的信息
      
        Account account = new Account(cardId,name,password,quotaMoney);
        //5.把这个用户对象存到集合中
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCard() + "请您妥善保管");
    }

    public static String createCard(ArrayList<Account> accounts) {
        while (true) {
            String cardId = "";
            Random random = new Random();
            for (int i = 0; i < 8; i++) {
                cardId += random.nextInt(10);//这里是10 那么范围就确定在0到9
            }
            //判断卡号是否重复
            Account a = new Account();
            a = getAccountByCardId(cardId, accounts);
            if (a == null) {
                return cardId;//没有重复
            }
            return null;
        }
    }
//通过卡号查找在集合是否有与之相同的
    public static Account getAccountByCardId(String cardId,ArrayList<Account> accounts) {
            //根据卡号查询对象
            //用循环遍历每一个账号对象
            for (int i = 0; i < accounts.size(); i++) {
                Account a = accounts.get(i);//拿出每一个账户对象给a
                //通过a调用公共接口调用属性
                if (a.getCard().equals(cardId)) {
                    return a;
                }
            }
            return null;
        }
    public static void login(ArrayList<Account> accounts,Scanner scanner){
        //先判断系统中是否有账号
        if(accounts.size()==0){
            return;
        }
        System.out.println("请输入您的卡号:");
        String cardId=scanner.next();
        //2.根据卡号查询账户对象
        Account leo=getAccountByCardId(cardId,accounts);
        //3.判断是否有这个账号
        if(leo!=null){
            //4.让用户输入密码
            System.out.println("请您输入登录的密码:");
            String password=scanner.next();
            //5.判断密码是否与账号中的密码相同
            if(leo.getPassword().equals(password)){
                System.out.println("恭喜您"+leo.getUserName()+"先生或女士成功进入系统,您的卡号是:"+leo.getCard());
                showUsercommand(accounts,scanner,leo);
                return;
            } else {
                System.out.println("您的密码输入有误 请确认");
            }
        } else {
            System.out.println("对不起 该账号不存在");
        }
    }
    public static void despoitMoney(Account leo,Scanner scanner){
        System.out.println("===========存款界面============");
        double money=scanner.nextDouble();
        leo.setMoney(leo.getMoney()+money);
        System.out.println("存款完成");
        showAccount(leo);
    }
    public static void showUsercommand(ArrayList<Account> accounts,Scanner scanner,Account leo) {
        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.注销账号");
        while (true) {
            System.out.println("请您输入操做命令");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    //查询账户
                    showAccount(leo);
                    break;
                case 2:
                    //存款
                    despoitMoney(leo,scanner);
                    break;
                case 3:
                    //取款
                    drawMoney(leo,scanner);
                    break;
                case 4:
                    //转账
                    transferMoney(accounts,leo,scanner);
                    break;
                case 5:
                    //修改密码
                    Updatapassword(leo,scanner);
                    return;//修改完密码之后 肯定是返回主页了 而只是单单的跳出switch语句
                case 6:
                    //退出
                    System.out.println("欢迎下次光临");
                   return;//结束当前操作
                case 7:
                    //注销账号
                    accounts.remove(leo);
                    System.out.println("销户成功");
                    return;
                default:
                    System.out.println("您输入错误 请重新输入");
            }
        }
    }

    /**
     * 用户密码的修改
     * @param leo
     * @param scanner
     */
    private static void Updatapassword(Account leo, Scanner scanner) {
        System.out.println("=============修改密码============");
        while (true) {
            System.out.println("请输入您的密码");
            String password = scanner.next();
            if (password.equals(leo.getPassword())) {
                while (true) {
//输入要修改的新密码
                    System.out.println("请您输入您要修改的新密码");
                    String password1 = scanner.next();
                    System.out.println("请您重新输入");
                    String okpassword = scanner.next();
                    if (password1.equals(okpassword)) {
leo.setPassword(password1);
                        System.out.println("恭喜您修改成功");
                        return;//直接结束
                    } else {
                        System.out.println("两次输入的新密码不一致 请重新输入");
                    }
                }
            }else {
                System.out.println("您输入的密码错误 请您重新输入");
            }
        }
    }

    /**
     * 转账
     * @param accounts
     * @param leo
     * @param scanner
     */
    private static void transferMoney(ArrayList<Account> accounts, Account leo, Scanner scanner) {
        //1.如果只有两个账号那么不可以转账
        if (accounts.size() < 2) {
            return;
        }
        //2.没有余额的时候 不可以转账
        if (leo.getMoney() == 0) {
            System.out.println("你没钱了就别转账了");
            return;
        }
        //3.可以转账
        while (true) {
            System.out.println("请您输入要转账给的人的卡号");
            String cardId = scanner.next();
            //messi即表示新输入卡号的引用
            Account messi = getAccountByCardId(cardId, accounts);
            if (messi != null) {//证明有重复的  账户存在
//不可以转账给自己    //新输入的卡号            //原来的卡号
                if(messi.getCard().equals(leo.getCard())){
                    System.out.println("不可以自己给自己转账");
                } else {
                    //确认对方的姓氏
                    //这里表示用*截断第一位
                    String name="*"+messi.getUserName().substring(1);
                    System.out.println("请您确认["+name+"]的姓氏");
                    String preName=scanner.next();
                    //startsWith是判断当前字符串是否是以另外一个给定字符串开头的
                    if(messi.getUserName().startsWith(preName)) {
                        System.out.println("可以开始转账 请您输入转账金额");
                        Double Money = scanner.nextDouble();
                        //判断是否这个转账金额大于余额
                        if (Money > leo.getMoney()) {
                            System.out.println("余额不足 不可以进行转账");
                        } else {
                            leo.setMoney(leo.getMoney()-Money);
                            messi.setMoney(messi.getMoney()+Money);
                            System.out.println("转账金额为:" + Money);
                            System.out.println("恭喜您已经为"+messi.getUserName()+"转账了"+Money);
                            showAccount(leo);
                            return;
                        }
                    }else {
                        System.out.println("输入错误  请您重新输入");
                    }
                }
            } else {
                System.out.println("账户不存在 请您重新输入");
            }
        }
    }

    /**
     * 取款
     * @param leo
     * @param scanner
     */
    private static void drawMoney(Account leo, Scanner scanner) {
        System.out.println("=====================取款操作==============");
    //1.假设少于100就不可以取款
        if(leo.getMoney()>100){
            System.out.println("请输入您要取款的金额");
            Double money=scanner.nextDouble();
            //2.判断是否超过单次限额
            if(money> leo.getQuotaMoney()){
                System.out.println("您取款金额超过单次限额");
            } else {
                //3.看剩下的钱够不够取款
                if(money> leo.getMoney()){
                    System.out.println("余额不足");
                } else {
                    System.out.println("取款成功 取款金额为:"+money);
                    System.out.println("当前余额为:"+(leo.getMoney()-money));
                }
            }
        } else {
            System.out.println("您自己还没有100元就别取款了");
        }
    }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值