ismohney-and-curlie

练习:JavaSE之模拟ATM系统实现功能项目

需求分析:模拟ATM系统功能实现

功能 --- 用户层面(注册、登录、注销)

系统层面(改密、存款、取款、转账)

逻辑层面(选择功能)

一、账户Bean创建(Account)

· 成员属性:id、password、username、amount、balance

/**账户类  用于得到每个账户对象*/
public class Account {
    private String name;//账户名
    private String password;//账户密码
    private String id;//卡号  5个数组5个字母
    private int amount; //账户金额
    private int balance; //取款限制额度

    public Account() { /*无参构造器*/ }

    public Account(String name, String password, String id,int balance ) {
        /*有参构造器*/
             this.name = name;
             this.id = id;
             this.password = password;
             this.balance = balance ;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
}

    public int getAmount() {
                return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }
}

二、main方法

· 包含逻辑层面的具体交互【主要语法:switch、循环、标志符、集合、Scanner】

1、前提准备:创建账户,准备集合封装多个账户

        Account a = new Account();
        ArrayList<Account> list = new ArrayList<>();//封装用户
        System.out.println("======ATM=======");
        Scanner sc = new Scanner(System.in);
        String id = "";//用来实现登录功能
        boolean flag = false;//标志符改变死循环

2、第一次交互

【循环运用:while死循环】

//巨大的死循环,包住第一次功能、第二次功能
        while (true) {
            //入口和操作项目并列
            while (true) {
                System.out.println("请选择你要进行的操作:");
                //第一次选择功能
                System.out.println("1:登录账户" + "\n" + "2:注册账户");
                int select1 = sc.nextInt();
                switch (select1) {
                    case 1:
                         id =login(list, sc);
                         if (id == null) {
                             continue;
                         }else {
                             flag = true;   }
                        break;
                    case 2:
                        register(list, sc);
                        break;
                    default:
                        System.out.println("你的输入有误!");
                        break;
                }
                if (flag == true) {
                    break;
                }
            }
                //结束登录和注册
            System.out.println("=======欢迎进入ATM系统========");

3、第二次交互

【具体功能选择】

           //第二次选择功能
            boolean end = false ;
            while (true) {
                System.out.println("请选择要进行的项目:" + "\n" + "1. 存款           2. 取款" + "\n" + "3. 转账            4. 修改密码" + "\n" + "5. 注销账户           6. 退出系统");
                int select2 = sc.nextInt();
                switch (select2) {
                    case 1://存款
                        deposit(list, sc,id);
                        flag = false;
                        break;
                    case 2://取款
                        GetMoney(list,sc,id);
                        flag = false;
                        break;
                    case 3://转账
                        transfer(list,sc,id);
                        flag = false;
                        break;
                    case 4://修改密码
                        modify(list,sc,id);
                        end = true;
                        break;
                   case 5://注销账户
                        end = cancel(list,sc,id,end);
                        break;
                    case 6://退出系统,结束整个系统
                        end = true;
                        break;
                    default:
                        System.out.println("你的输入有误!");
                        break;
                }
                //完成功能,再来一遍功能选择循环
                if (flag = false  ) {
                    continue;
                }
                //退出死循环,重新到登录功能选择
              if (end == true){
                  break;
              }
            }
        }

 三、ATM功能实现【符合main中逻辑设计】

1、登录功能(login)【集合、标志符、循环、if】

//接收集合作为判定,接收Scanner节约内存、提高性能 
public static String login(ArrayList<Account> list ,Scanner sc) { System.out.println("==================登录界面===================="); boolean flag = false; boolean end = true; String id = ""; String password = ""; //限定三次循环,当信息输入有误就重新操作且只有三次机会 for (int j = 1; j < 4; j++) { //是否退出操作 System.out.println("是否退出登录去注册账户?1:yes/0:no"); int select = sc.nextInt(); switch (select) { case 1: end = false; break; case 0: break; } //接收账户登录信息 if (end == true) { System.out.println("输入你的卡号:"); id = sc.next(); System.out.println("输入你的密码:"); password = sc.next(); //通过集合遍历调出数据,对比是否存在该账户 for (Account a = list) { //一个一个索引的对比 if (id.equals(a.getId()) && password.equals(a.getPassword())) { flag = true; break;//退出遍历 } } //判断信息错误 for (Account b : list) { if (password.equals(b.getPassword()) && !id.equals(b.getId())) { System.out.println("账户不存在!请重新输入(你还有三次输入机会)"); break;//结束遍历开始下一次循环,重新输入信息 } else { System.out.println("密码不正确,请重新输入(你还有三次输入机会)"); break;//结束遍历开始下一次循环,重新输入信息 } } if (flag == true) { break; } //当信息正确,退出循环 else { System.out.println("不存在账户!重新输入或去注册"); } } if (end == false) { System.out.println("正在返回开始界面......"); return null; } } //信息正确 if (flag == true) { System.out.println("登录成功!"); return id; }else { System.out.println("今日登录次数已用尽,账户已上锁!请到相关机构解锁!"); } return null; }

2、注册功能(register)

· id卡号,系统生成,设计多个方法嵌套

    //注册账户
    public static void register(ArrayList<Account> list, Scanner sc) {
        System.out.println("============注册账户==============");
        Account a = new Account();
        System.out.println("输入您的用户名:");
        String name = sc.next();
        String password1= "";
        while (true) {
            System.out.println("输入您的密码:");
            password1 = sc.next();
            System.out.println("再次确认密码:");
            String password2 = sc.next();
            if (password1.equals(password2)) {
                System.out.println("注册成功!");
                break;
            } else {
                System.out.println("两次密码不一致!重新输入");
                continue;
            }
        }
        //得到卡号
        String id = obtainId(list);
        //设置限取金额
        System.out.println("设置你的每次最高限取金额:");
        int balance = sc.nextInt(); //直接存入账户对象
        list.add(new Account(name, password1,id,balance));
        System.out.println("注册成功!");
        for (int i = list.size()-1 ; i < list.size(); i++) {
            Account display = list.get(i);
            System.out.println("您是本行第"+(i+1)+"位用户");
            System.out.println("您的用户名:" + display.getName()
                    + "\n"  + display.getId());
        }
    }

2.1 获得id卡号(obtainId)

【随机数、正则表达式、循环、字符串拼接】

    //获得卡号
    public static String obtainId(ArrayList<Account> list) {
        Random r = new Random();
        StringBuilder sb = new StringBuilder("您的卡号:");
        String id = "";
        //得到随机卡号
        while (true) { 
            for (int i = 0; i < 10; i++) {
                if (sb.length()<=5) {
                    sb.append(r.nextInt(10));
                }else {
                    //a-Z的对应范围是65-90  97-122
                    String ch = String.valueOf(r.nextInt(58)+65);
                    if (ch.matches("[a-zA-Z]")) {
                        sb.append(ch);
                    }
                }
            }
            id=sb.toString();
            //判断卡号有无重复【单独方法会方便之后调用】
            Account only = okid(list, id);
            if (only == null) {//没有相同的,卡号就是符合条件的
                return id;
            }else {//卡号重复,再来一次循环
                continue;
            }
      }
}

2.2  判断id是否重复【id的唯一性】(okid)

    //判断集合中有无相同卡号
    private static Account okid (ArrayList<Account> list , String id ) {
        //遍历集合内容,取已有的id做判断
if(list.len())
for (Account only : list) { if (only.getId().equals(id)) { return only; } } return null; }

3、注销功能(cancel)

    //注销账户
    private static boolean cancel (ArrayList<Account> list,Scanner sc,String id,boolean flag){
             int i = Index(id,list);
             Account a = list.get(i);
            if (id.equals(a.getId())){
                System.out.println("=========账户注销==========");
                System.out.println("确认注销账户吗?1:yes/0:no");
                int answer = sc.nextInt();
                int money = a.getAmount();
                switch (answer){
                    case 1:
                        if (money == 0) {
                            System.out.println("您的余额为0,已直接进行注销");
                            list.remove(i);
                            System.out.println("注销成功!即将退出账户!");
                            return true;//直接回到登录界面
                        }else {
                            System.out.println("您的余额不为0,请先将余额取出!");
                        }
                        break;
                    case 0:
                        System.out.println("正在退出注销界面......");
                        break;
                }
        }
        return false;//再次进行功能选择【取款】
    }

3.1 Inddex方法,用于确定账户索引【集合遍历】

 //确定账户索引
    private  static int Index(String id,ArrayList<Account> list) {
        int i = 0;
        for (Account a : list) {
            if (id.equals(a.getId())){
                return i;
            }
        }
        return 0;
    }

4、修改密码(modify)

   //修改密码
    private static void modify(ArrayList<Account> list,Scanner sc,String id){
        for (Account a : list) {
            if (id.equals(a.getId())){
                System.out.println("=============修改密码=============");
                        while (true) {
                            System.out.println("请输入您的旧密码:");
                            String password_Old = sc.next();
                            if (password_Old.equals(a.getPassword())) {
                                //确认密码死循环
                                while (true) {
                                    System.out.println("请输入新密码:");
                                    String password_new = sc.next();
                                    System.out.println("确认新密码:");
                                    String password_NEW = sc.next();
                                    if (password_NEW.equals(password_new)) {
                                        a.setPassword(password_NEW);
                                        System.out.println("密码修改成功!即将重新登录");
                                        break;//结束新密码循环
                                    } else {
                                        System.out.println("两次密码输入不一致,重新输入!");
                                        continue;
                                    }
                                }
                                break;//结束旧密码循环
                            } else {
                                System.out.println("您输入的旧密码不正确!请重新输入");
                                continue;
                            }
                        }
            }
            break;//结束遍历循环,节省运算空间
        }
    }

5、存款(deposit)

 //账户存款
    public static void deposit(ArrayList<Account> list,Scanner sc,String id){
        for (Account a : list) {
            //登录输入的卡号与集合对象中的卡号相等,锁定该对象
            if (id.equals(a.getId())){
                System.out.println("==============存款================");
                System.out.println("账户名:"+a.getName());
                System.out.println("账户余额:"+a.getAmount());
                System.out.println("输入您要存入的金额:");
                int money = sc.nextInt();
                int amount = a.getAmount() ;
                a.setAmount(amount += money);
                System.out.println("---存款"+money+"元成功---");
                System.out.println("账户名:"+a.getName());
                System.out.println("账户余额:"+a.getAmount());
            }
        }//在已经登录的前提下
    }

6、取款(GetMoney)

 //账户取款
    private static void GetMoney(ArrayList<Account> list,Scanner sc,String id){
        for (Account a : list) {
            if (id.equals(a.getId())) {
                System.out.println("==============取款================");
                System.out.println("账户名:" + a.getName());
                System.out.println("账户余额:" + a.getAmount());
                System.out.println("账户每次取款限额为"+a.getBalance()+"元");
                while (true) {
                    System.out.println("输入您的取款金额:");
                    int money = sc.nextInt();
                    int balance = a.getBalance();
                    int amount = a.getAmount();
                    if (money > amount || money > balance) {
                        System.out.println("取款金额超过限制,不予取款!重新输入取款金额");
                    continue;
                    }else {
                        a.setAmount(amount -= money);
                        System.out.println("---取款"+money+"元成功---");
                        System.out.println("账户名:" + a.getName());
                        System.out.println("账户余额:" + a.getAmount());
                        break;
                    }
                    }
            }
        }
    }

7、转账(transfer)

    //账户转账
    private  static void transfer(ArrayList<Account> list,Scanner sc,String id) {
        //接收登录时的id,遍历整个集合做对比,找到相等的索引即可确定用户位置【也可以用Index方法】
        for (Account a : list) {
            if (id.equals(a.getId())) {
                System.out.println("==============转账================");
                System.out.println("账户名:" + a.getName());
                System.out.println("账户余额:" + a.getAmount());
                while (true) {
                    System.out.println("请输入要转账的卡号:[如要退出输入0]");
                    String card = sc.next();
                    //当输入退出选项时,不再进入流程,直接结束循环
                    if (card.equals(0)) {
                        break;
                    } else {
                        Account only = okid(list, card);
                        if (only == null) {
                            System.out.println("该卡号不存在,请重新输入!");
                            continue;
                        } else {
                            while (true) {
                                System.out.println("输入您要转账的金额:");
                                int money = sc.nextInt();
                                if (money > a.getAmount()) {
                                    System.out.println("金额超出您的余额!重新输入");
                                    continue;
                                } else {
                                    int amountOther = only.getAmount();
                                    int amount = a.getAmount();
                                    a.setAmount(amount -= money);
                                    only.setAmount(amountOther += money);
                                    System.out.println("---转账" + money + "元成功---");
                                    System.out.println("账户名:" + a.getName());
                                    System.out.println("账户余额:" + a.getAmount());
                                    break;//结束转账金额循环
                                }
                            }
                        }
                    }
                    break;//结束转账卡号循环
                }
            }
//            break;//退出选项,结束程序
        }
    }

 

posted on 2023-04-02 01:36  ooyls  阅读(50)  评论(0编辑  收藏  举报

导航