Java学习笔记---ATM管理系统

 1.定义账户类。

package test200.ATM;

import java.lang.ref.SoftReference;

/*

1.定义账户类,用于后期创建账户对象封装用户的账户信息。
2.账户类中的信息至少需要包含(卡号、姓名、密码、余额、取现额度)
3.需要准备一个ArrayList的集合,用于存储系统用户的账户对象。
4.需要展示欢迎页包含2个功能:开户功能、登录账户。

 */
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 money, double quotaMoney) {
        this.cardId = cardId;
        this.userName = userName;
        this.password = password;
        this.money = money;
        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;
    }
}

2.进行主页以及所有功能代码的编写

package test200.ATM;

import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/*
ATM系统入口类
 */
public class ATMSystem {
    public static void main(String[] args) {
        //1、定义账户类
        //2、定义集合容器
        ArrayList<Account> accounts = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        //3、展示开户和登录功能
        while (true) {
            System.out.println("------欢迎使用本系统------");
            System.out.println("1.开户");
            System.out.println("2.登录");
            System.out.println("请您选择系统功能:");
            System.out.println("-------------------------");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    register(accounts, sc); //开户
                    break;
                case 2:
                    login(accounts, sc); //登录
                    break;
                default:
                    System.out.println("序号错误!");

            }
        }

    }

    /**
     * 登录方法
     *
     * @param accounts 全部对象集合
     * @param sc       输入
     */
    private static void login(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("==========欢迎进入登录界面==========");
        //1 判断是否存在账户,不存在则不能登录
        if (accounts.size() == 0) {
            System.out.println("账户为空,不能登录,请先注册!");
            return;  //为语言风格,解决方法执行
        }
        while (true) {
            //2 正式进入登录操作
            System.out.println("请输入卡号:");
            String cardId = sc.next();
            //3 判断卡号是否存在,根据卡号去集合中查找对象
            Account acc = getAccountByCardId(cardId, accounts);
            if (acc != null) {
                //查询到该卡号
                //4 用户输入密码  核对密码是否正确
                while (true) {
                    System.out.println("请输入密码:");
                    String passWord = sc.next();
                    if (passWord.equals(acc.getPassword())) {
                        //登录成功
                        System.out.println("欢迎您登录系统!" + acc.getUserName() + "先生/女士!" + "您的余额为:" + acc.getMoney());

                        //登录成功后进入用户操作界面  通过调用方法实现
                        showUserCommand(sc, acc, accounts);
                        //break;
                        return;                 //每一个while死循环里,可以用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:
                    //存款
                    depositMoney(sc, acc);
                    break;
                case 3:
                    //取款
                    drawMoney(sc, acc);
                    break;
                case 4:
                    //转账
                    transferMoney(acc, sc, accounts);
                    break;
                case 5:
                    //修改密码
                    updatePassWord(acc, sc);
                    //break;
                    return; //此处用return 打破用户登录后的这个界面,回到首页
                case 6:
                    //退出
                    System.out.println("退出成功!");
                    return;
                //break;
                case 7:
                    //注销
                    //deleteAccount(acc,sc,accounts);
                    //break;
                    //return; //此处用return 注销账户后,回到首页
                    //判断接收道德Boolean值,是否销户了看是否返回首页
                    if (deleteAccount(acc, sc, accounts)) {
                        return;  //销户成功返回首页
                    } else {
                        break;   //取消销户留在操作页
                    }
                default:
                    System.out.println("输入的序号不存在!");

            }
        }

    }

    /**
     * 销户方法:注销当前登录的账户
     *
     * @param acc      当前账户
     * @param sc       扫描器
     * @param accounts 所有用户集合
     */
    private static boolean deleteAccount(Account acc, Scanner sc, ArrayList<Account> accounts) {
        System.out.println("==========销户界面==========");
        System.out.println("您确定要注销账号吗?y/n");
        String rs = sc.next();
        switch (rs) {
            case "y":
                //提示用户还有余额
                if (acc.getMoney() > 0) {
                    System.out.println("您的账户余额还有" + acc.getMoney() + "元,请取完所有余额后再进行销户操作!"); //跳到下面的break
                } else {
                    //销户
                    //从当前用户集合中删除掉当前登录的用户对象
                    accounts.remove(acc);
                    System.out.println("销户成功!");
                    return true; //销户成功返回true
                }
                break;

            default:
                System.out.println("销户已取消!");
        }
        return false;  //取消销户 返回false
    }

    /**
     * 转账功能:实现转账功能
     *
     * @param acc      当前账户
     * @param sc       扫描器
     * @param accounts 所有账户对象集合
     */
    private static void transferMoney(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("您的账户中没有钱!");
            return;  //结束转账
        }
        while (true) {
            //满足条件进行转账
            System.out.println("请输入对方卡号:");
            String cardId = sc.next();

            //判断是否是自己的卡号
            if (cardId.equals(acc.getCardId())) {
                System.out.println("您不能给自己转账!");
                continue;  //结束当前循环 执行下一次死循环
            }

            //判断对方卡号是否存在
            Account account = getAccountByCardId(cardId, accounts);
            if (account == null) {
                System.out.println("所输入的卡号不存在!请重新输入!");
            } else {
                //卡号存在,认证姓氏
                String userName = account.getUserName();  //将他人的用户名赋值给userName
                String tip = "*" + userName.substring(1);  //*+他人用户名称的第二位往后几位
                System.out.println("请您输入[" + tip + "]的姓氏");
                String preName = sc.next();

                //认证姓氏是否正确
                if (userName.startsWith(preName)) {
                    //认证完成,进行转账
                    while (true) {
                        System.out.println("请您输入转账金额:");
                        double money = sc.nextDouble();
                        //判断金额是否足够
                        if (money > acc.getMoney()) {
                            System.out.println("您的余额不足,您最多可以转账" + acc.getMoney());
                        } else {
                            acc.setMoney(acc.getMoney() - money);
                            account.setMoney(account.getMoney() + money);
                            System.out.println("转账成功!");
                            System.out.println("您的余额还有:" + acc.getMoney());
                            return;  //干掉转账方法
                        }
                    }

                } else {
                    System.out.println("您输入的姓氏不对!");
                }
            }
        }

    }


    /**
     * 修改密码功能:修改当前用户的密码
     *
     * @param acc 当前用户对象
     * @param sc  扫描器
     */
    private static void updatePassWord(Account acc, Scanner sc) {
        while (true) {
            System.out.println("==========修改密码界面==========");
            System.out.println("请输入您原来的密码");
            String password = sc.next();
            if (acc.getPassword().equals(password)) {
                while (true) {
                    System.out.println("请输入新的密码:");
                    String newPassWord = sc.next();
                    //二次输入新密码
                    System.out.println("请再次输入您的新密码:");
                    String reNewPassWord = sc.next();
                    if (newPassWord.equals(reNewPassWord)) {
                        //相同修改成功
                        acc.setPassword(newPassWord);
                        System.out.println("修改成功,请您重新登录!");
                        return;
                    } else {
                        System.out.println("您两次输入的密码不同,请重新输入:");
                    }
                }
            } else {
                System.out.println("您输入的密码错误,请重新输入:");
            }
        }
    }

    /**
     * 取款功能:实现当前用户取款
     *
     * @param sc  扫描器
     * @param acc 当前用户对象
     */
    private static void drawMoney(Scanner sc, Account acc) {
        System.out.println("==========取款界面==========");
        //判断用户里是否有钱
        if (acc.getMoney() != 0) {
            while (true) {
                System.out.println("请输入您要取款的金额:");
                double money = sc.nextDouble();
                if (money < acc.getQuotaMoney()) {
                    if (money <= acc.getMoney()) {
                        System.out.println("取款成功!");
                        acc.setMoney(acc.getMoney() - money);
                        return;
                    } else {
                        System.out.println("您的余额不足!");
                    }
                    return;
                } else {
                    System.out.println("您的取款金额大于一次限额!请重新输入取款金额!");
                }
            }
        } else {
            System.out.println("您的账户里没有钱,请先存款!");
            depositMoney(sc, acc);
        }
    }

    /**
     * 存钱功能:实现为当前账户存款功能
     *
     * @param sc  输入存款数量
     * @param acc 当前账户对象
     */
    private static void depositMoney(Scanner sc, Account acc) {
        System.out.println("==========存款界面==========");
        System.out.println("请输入您的存款金额:");
        double money = sc.nextDouble();
        acc.setMoney(money + acc.getMoney());
        System.out.println("存款成功!");
        System.out.println("您的当前余额为:" + acc.getMoney());
    }

    /**
     * 查询功能:查询该用户信息
     *
     * @param acc 当前账户对象
     */
    private static void showAccount(Account acc) {
        System.out.println("==========当前账户信息==========");
        System.out.println("当前账户名称为:" + acc.getUserName());
        System.out.println("当前账户卡号为:" + acc.getCardId());
        System.out.println("当前账户余额为:" + acc.getMoney());
        System.out.println("当前账户限额为:" + acc.getQuotaMoney());
    }


    //定义开户方法
    //参数:ArrayList<Account> accounts{...}
    //返回值:void
    private static void register(ArrayList<Account> accounts, Scanner sc) {
        System.out.println("==========欢迎进入开户界面==========");
        //1 创建一个用户对象 用于封装信息
        Account a = new Account();

        //Scanner sc = new Scanner(System.in);        //从上方导入sc接收就不用再次创建sc对象
        //2 录入账户信息 注入到账户对象中
        System.out.println("请输入开户名称:");
        String userName = sc.next();
        a.setUserName(userName);
        while (true) {
            System.out.println("请输入您的密码:");
            String passWord = sc.next();
            System.out.println("请再次输入您的密码:");
            String rePassword = sc.next();
            if (rePassword.equals(passWord)) {
                //System.out.println("开户成功!");
                a.setPassword(rePassword);
                break;
            } else {
                System.out.println("密码不正确,请重新输入密码:");
            }
        }

        //限额
        System.out.println("请输入限额:");
        double quotaMoney = sc.nextDouble();
        a.setQuotaMoney(quotaMoney);

        //生成卡号 系统生成8位数,且不重复
        String cardId = getRandomCardId(accounts);
        a.setCardId(cardId);

        //3 把账户对象添加到集合中去
        accounts.add(a);
        System.out.println("恭喜您!" + a.getUserName() + "先生/女士,您开户成功!" + "您的卡号为:" + a.getCardId());
    }

    //生成随机卡号
    private static String getRandomCardId(ArrayList<Account> accounts) {
        Random r = new Random();
        while (true) {
            String cardId = "";
            for (int i = 0; i < 8; i++) {
                cardId += r.nextInt(10);
            }
            //调用方法查询是否重复
            Account acc = getAccountByCardId(cardId, accounts);
            if (acc == null) {
                return cardId;
            }
        }
    }

    /**
     * 根据卡号查询一个账户对象出来
     *
     * @param cardId   卡号
     * @param accounts 账户对象集合
     * @return 所查卡号账户对象  null
     */
    private static Account getAccountByCardId(String cardId, ArrayList<Account> accounts) {
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId)) {
                //System.out.println("卡号重复");
                return acc;
            }
        }
        return null; //查无此号
    }
}

3.系统总结:

package test200.ATM;

public class Sign {
    /*

系统准备、首页设计

*   用户开户功能实现
*   用户登录功能实现
*   用户操作页设计、查询账户、退出账户功能实现
*   用户存款功能实现
*   用户取款功能实现
*   用户转账功能实现
*   用户密码修改功能实现
*   用户销户功能实现

     */

    //一个方法里面用一个return  就可以彻底打断该方法循环
    //每一部分功能层层递进,逻辑清晰
    //每一个功能构建,都要有一个清晰的蓝图,每一步每一次逻辑判断,保证运行测试的时候bug少一些
    //选中一大段循环代码:ctrl+alt+T  快捷生成循环while等
    //  方法前输入/**点回车可以快捷生成方法注释
    //在一个类中,方法可以互相调用,要注意传入参数是否正确
    //username.substring(1)   意思是返回字符串username第一位后面的所有,例如:
    /*
    *  String username = yonghu;     或者  用户 
    * String tip = "*" + username.substring(1);
    * tip就是 *onghu    或者   *户
    * */
    //username.startsWith  表示字符串username的开头字符  如上例中的y或者用
    //if语句,可以不选择else,只有if条件,只执行if内的语句

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值