模拟ATM,java基础语法逻辑练习

/**
 用户类
 */
public class Account {
    private String cardID;//卡号
    private String userName;//用户名
    private String passWord;//密码
    private double money;//账户余额
    private double quotaMoney;//单次操作限额

    public Account() {
    }

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

    public String getCardID() {
        return cardID;
    }

    public void setCardID(String cardID) {
        this.cardID = cardID;
    }

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

功能段

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);
    }
    public static void showMain(ArrayList<Account>accounts){
        Scanner sc=new Scanner(System.in);
        System.out.println("==================首页===================");
        while (true) {
            System.out.println("请您输入命令:");
            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("您输入的命令不被支持。");
            }
        }
    }

    /**
     * 用户登录功能
     * @param accounts
     * @param sc
     */
    private static void login(ArrayList<Account> accounts,Scanner sc){
        while (true) {
            System.out.println("========================登录页面======================");
            if (accounts.size()==0){
                System.out.println("系统中无账户,请先注册!");
                return;
            }
            System.out.print("请您输入卡号:");
            String cardID=sc.next();
            //在集合中查找卡号是否存在
            Account acc=getAccountByCardID(accounts,cardID);
            if (acc==null){
                System.out.println("您输入的卡号不存在。请重新输入!");
            }else {
                while (true) {
                    System.out.print("请输入您的账户密码:");
                    String passWord=sc.next();
                    //判断密码是否正确
                    if(acc.getPassWord().equals(passWord)){
                        //登录成功
                        System.out.println("登录成功。"+acc.getUserName()+"卡号:"+acc.getCardID()+"你好!");
                        //展示功能页面
                        showUserCommand(sc,acc,accounts);
                        return;
                    }else {
                        System.out.println("密码不正确,登录失败!请重新输入密码。");
                    }
                }
            }
        }
    }

    private static void showUserCommand(Scanner sc,Account account,ArrayList<Account>acc) {
        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.注销账号");
        int command=sc.nextInt();
            switch (command){
                case 1:
                    //查询
                    showAccount(account);
                    break;
                case 2:
                    //存款
                    depositMoney(sc,account);
                    break;
                case 3:
                    //取款
                    drawmoney(sc,account);
                    break;
                case 4:
                    //转账
                    transmoey(acc,sc,account);
                    break;
                case 5:
                    //修改密码
                    updataPassword(sc,account);
                    return;
                case 6:
                    //退出
                    System.out.println("欢迎下次再来!");
                    return;
                case 7:
                    //注销
                    acc.remove(account);
                    System.out.println("注销成功!");
                    return;
            }
        }
    }

    private static void updataPassword(Scanner sc, Account account) {
        while (true) {
            System.out.println("输入原始密码");
            String password=sc.next();
            while (true) {
                if(account.getPassWord().equals(password)){
                    System.out.println("输入新密码:");
                    String newPassword=sc.next();
                    System.out.println("再次确认新密码:");
                    String oknewPassword=sc.next();
                    if (newPassword.equals(oknewPassword)){
                        account.setPassWord(newPassword);
                        System.out.println("密码修改成功");
                        return;
                    }
                }else {
                    System.out.println("密码错误,重新输入!");
                    break;
                }
            }
        }
    }

    /**
     * 转账功能
     * @param accounts
     * @param sc
     */
    private static void transmoey(ArrayList<Account>accounts,Scanner sc,Account acc) {
        //系统中是否有最少两个账户

            if(accounts.size()<2){
                System.out.println("系统中没有其他账户信息。");
                return;
            }else{
                if (acc.getMoney()<=0){
                    System.out.println("您的账户余额为0,不可转账");
                    return;
                }else {
                    while (true) {
                        System.out.println("请您输入要转入的账户卡号:");
                        String other=sc.next();
                        Account otherAccount=getAccountByCardID(accounts,other);
                        if (otherAccount!=null){
                            //验证信息
                            System.out.println("请输入【*"+otherAccount.getUserName().substring(1)+"】的姓氏");
                            String preName=sc.next();
                            if(otherAccount.getUserName().startsWith(preName)){
                                while (true) {
                                    System.out.println("输入您要转账的金额");
                                    Double money=sc.nextDouble();
                                    if (acc.getMoney()>=money){
                                        acc.setMoney(acc.getMoney()-money);
                                        otherAccount.setMoney(otherAccount.getMoney()+money);
                                        System.out.println("转账成功,目前余额:"+acc.getMoney());
                                        return;
                                    }else {
                                        System.out.println("您的账户余额不足");
                                    }
                                }
                            }
                        }else{
                            System.out.println("您输入的账号信息不存在。");
                            return;
                        }
                    }
                }
            }

    }

    /**
     * 取款操作
     */
    private static void drawmoney(Scanner sc,Account account) {
        //判断账户余额大于100
        if (account.getMoney()>100){
            while (true) {
                System.out.println("请输入取款金额:");
                double money=sc.nextDouble();
                if (money>account.getQuotaMoney()){
                    System.out.println("超过单次操作限额,不可超过:"+account.getQuotaMoney());
                }else {
                    if (account.getMoney()<money){
                        System.out.println("取款失败,余额不足");
                    }else {
                        account.setMoney(account.getMoney()-money);
                        System.out.println("取款成功,当前账户余额:"+account.getMoney());
                        return;
                    }
                }
            }
        }else {
            System.out.println("账户余额小于100,不可取款");
        }
    }

    /**
     * 存款操作
     * @param sc
     * @param acc
     */
    private static void depositMoney(Scanner sc,Account acc) {
        System.out.println("=============================存款操作===================================");
        System.out.print("输入存款金额:");
        double money=sc.nextDouble();
        acc.setMoney(acc.getMoney()+money);
        System.out.println("存款成功");
        showAccount(acc);
        return;
    }

    /*
    账户信息展示
     */
    private static void showAccount(Account account) {
        System.out.println("============================用户详情页面=======================");
        System.out.println("卡号:"+account.getCardID());
        System.out.println("用户名:"+account.getUserName());
        System.out.println("余额:"+account.getMoney());
        System.out.println("单次交易限额:"+account.getQuotaMoney());
    }

    /**
     *用户开户功能
     * @param accounts
     */
    private static void  register(ArrayList<Account>accounts,Scanner sc){
        System.out.println("====================注册系统================");
        System.out.println("请输入账户名称:");
        String accountName=sc.next();
        //当次限额
        System.out.println("请您输入单次限额");
        double quota= sc.nextDouble();
        String passWord="";
        while (true) {
            System.out.println("请输入密码:");
            passWord=sc.next();
            System.out.println("请再输入一次确认密码:");
            String okpassword= sc.next();
            //判断两次密码是否一致
            if (okpassword.equals(passWord)){
                break;
            }else {
                System.out.println("两次密码不一致,请重新输入");
            }
        }
        //生成不重复的卡号cardID
        String cardID=creatCardID(accounts);
        //创建对象 封装用户信息
        //public Account(String cardID, String userName, String passWord, double quotaMoney)
        Account account=new Account(cardID,accountName,passWord,quota);
        //将新建对象放入集合
        accounts.add(account);
        System.out.println("恭喜您开户成功!您的卡号是:"+cardID+"请您牢记");
    }
    //生成不重复的8位卡号
    public static String creatCardID(ArrayList<Account>accounts){
        Random r=new Random();
        String cardId="";
        while (true) {
            for (int i = 0; i < 8; i++) {
                cardId+=r.nextInt(10);
            }
            //遍历整个集合,查看卡号是否重复
            Account acc=getAccountByCardID(accounts,cardId);
            if (acc==null){
                return cardId;
            }
        }
    }
    //按卡号查找账户
    public static Account getAccountByCardID(ArrayList<Account>accounts,String cardID){
        for (int i = 0; i < accounts.size(); i++) {
            Account acc=accounts.get(i);
            if (acc.getCardID().equals(cardID)){
                return acc;
            }
        }
        return null;//集合内无此卡号
    }

}

上一篇:(转)程序猿面试需要的知识点总结


下一篇:POP Animation 和 layoutSubviews 的冲突