Java练习——ATM系统

练习收获:

1、学着将一些常用的代码,写为方法,进行方法的封装。比如判断用户卡号是否重复,通过使用卡号查找用户的方法来判断。确实方便后续其他功能的编写了,而且代码逻辑性强,格式更规范

2、学会使用卫语言风格“ return;”,return可以直接跳出方法,无论嵌套着多少个循环或者switch,牛哇;而break只能跳出一层循环。

    return;

3、写方法的注释,/**+直接回车

    /**
     * 用户开户
     * @param accountArrayList
     * @param scanner
     */

4、ctrl+点击方法名称,快速跳转到对应方法

5、对象中存储的是地址,因此修改对象后,无须再添加到对象列表中,因为对象列表原本就已经存储了该对象

详细代码如下:

账户类

package com.laogao.atm;

public class Account {
    /**
     *
     */
    private String cardId;
    private String username;
    private String password;
    private double money;
    private double 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;
    }
}

测试类

package com.laogao.atm;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args) {
        ArrayList<Account> accountArrayList=new ArrayList<>();
        Scanner scanner=new Scanner(System.in);
        while (true) {
            System.out.println("===========欢迎进入ATM系统===========");
            System.out.println("1.登录账号");
            System.out.println("2.注册账号");
            System.out.println("请输入1、2选择对应操作:");
            String commend=scanner.next();
            switch (commend){
                case "1":
                    login(accountArrayList,scanner);
                    break;
                case "2":
                    //创建方法快捷键,alt+enter
                    resign(accountArrayList,scanner);
                    break;
                default:
                    System.out.println("命令错误!");
                    break;
            }
        }
    }

    /**
     * 用户开户
     * @param accountArrayList
     * @param scanner
     */
    private static void resign(ArrayList<Account> accountArrayList,Scanner scanner) {
        Account account=new Account();
        System.out.println("=======欢迎注册办卡========");
        System.out.println("请输入您的名字:");
        String username=scanner.next();
        account.setUsername(username);

        while (true) {
            System.out.println("请输入您的密码:");
            String password=scanner.next();
            System.out.println("请确认您的密码:");
            String password2=scanner.next();
            if (password2.equals(password)){
                account.setPassword(password);
                break;
            }
            else {
                System.out.println("两次密码不一致,请重新输入");
            }
        }
        System.out.println("请输入您的额度:");
        double quotaMoney=scanner.nextDouble();
        account.setQuotaMoney(quotaMoney);
        //生成卡号
        String cardId=getRandomCardId(accountArrayList);
        account.setCardId(cardId);

        accountArrayList.add(account);
        System.out.println("开户成功!"+username+"先生/女士,"+"您的卡号为:"+cardId+",请牢记");

    }

    /**
     * 随机生成卡号
     * @param accountArrayList
     * @return
     */
    private static String getRandomCardId(ArrayList<Account> accountArrayList) {
        Random random =new Random();
        String cardId="";
        while (true) {
            for (int i = 0; i < 8; i++) {
                cardId+=random.nextInt(10);
            }
            //通过卡号查找对象,看有没有重复的
            Account account=getAccountByCardId(cardId,accountArrayList);
            if (account==null){//没查到对象,说明卡号没重复
                break;
            }
        }
        return cardId;
    }

    /**
     * 根据卡号查找对象
     * @param cardId
     * @param accountArrayList
     * @return
     */
    private static Account getAccountByCardId(String cardId, ArrayList<Account> accountArrayList) {
        for (int i = 0; i < accountArrayList.size(); i++) {
            if(cardId.equals(accountArrayList.get(i).getCardId())){
                //卡号重复,返回查到的对象
                return accountArrayList.get(i);
            }
        }
        return null;
    }

    /**
     * 用户登录
     */
    private static void login(ArrayList<Account> accountArrayList,Scanner scanner) {
        Account account;
        System.out.println("=======欢迎来到登录界面========");
        if (accountArrayList.size()==0){
            System.out.println("目前无用户信息,请先注册!");
            return;//卫语言风格,结束方法的执行
        }
        while (true) {
            System.out.println("请输入卡号:");
            String cardId=scanner.next();
            account=getAccountByCardId(cardId,accountArrayList);
            if (account!=null){//查到了
                break;
            }else {
                System.out.println("不存在该卡号");
            }
        }
        while (true) {
            System.out.println("请输入密码:");
            String password= scanner.next();
            if (password.equals(account.getPassword())){//密码正确
                System.out.println(account.getUsername()+"贵宾,欢迎进入系统,您的卡号为:"+account.getCardId());
                //进行更多登录后操作
                showUserCommend(account,accountArrayList,scanner);
                return;
            }else {
                System.out.println("您的密码有误,请重新确认!");
            }
        }
    }

    /**
     * 展示用户登录后的更多操作
     * @param account 当前用户信息
     * @param accountArrayList 全部用户信息列表
     */
    private static void showUserCommend(Account account, ArrayList<Account> accountArrayList ,Scanner scanner) {
        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("您选择的操作是:");
            String commend=scanner.next();
            switch (commend){
                case "1":
                    showSearch(account);
                    break;
                case "2":
                    saveMoney(account,scanner);
                    break;
                case "3":
                    withdrawMoney(account,scanner);
                    break;
                case "4":
                    transferAccounts(account,scanner,accountArrayList);
                    break;
                case "5":
                    changePassword(account,scanner);
                    return;
                case "6":
                    System.out.println("退出成功,欢迎下次登录");
                    return;//让当前方法停止执行
                case "7":
                    if(deleteAccount(account,accountArrayList,scanner)){
                        return;//返回登录页
                    }else {
                        break;//留在当前页
                    }

                default:
                    System.out.println("命令有误!");
            }
        }
    }

    /**
     * 删除账户
     * @param account
     * @param accountArrayList
     */
    private static boolean deleteAccount(Account account, ArrayList<Account> accountArrayList,Scanner scanner) {
        System.out.println("您确定销户吗?y/n");
        String ans=scanner.next();
        switch (ans){
            case "y":
                if (account.getMoney()>0){
                    System.out.println("还有钱,账号不可注销!");
                }else {
                    accountArrayList.remove(account);
                    System.out.println("账号注销成功!");
                    return true;
                }
                break;
            default:
                System.out.println("账号保留");
        }
        return false;
    }

    /**
     * 修改用户密码
     * @param account 当前用户
     * @param scanner 扫描器
     */
    private static void changePassword(Account account, Scanner scanner) {
        System.out.println("========欢迎进入修改密码界面=========");
        while (true) {
            System.out.println("请输入您的账号原密码:");
            String passwordOld=scanner.next();
            if (passwordOld.equals(account.getPassword())){
                while (true) {
                    System.out.println("请输入您的账号新密码:");
                    String passwordNew1=scanner.next();
                    System.out.println("请确认您的账号新密码:");
                    String passwordNew2=scanner.next();
                    if (passwordNew1.equals(passwordNew2)){
                        //两次密码一致
                        account.setPassword(passwordNew1);
                        System.out.println("修改密码成功,请重新登录!");
                        return;
                    }else {
                        System.out.println("两次密码不一致!");
                    }
                }
            }else {
                System.out.println("账号密码不正确!");
            }
        }
    }

    /**
     * 用户转账
     * @param account 当前转账操作用户
     * @param scanner 扫描器
     * @param accountArrayList 全部用户对象
     */
    private static void transferAccounts(Account account, Scanner scanner, ArrayList<Account> accountArrayList) {
        System.out.println("=========欢迎来到用户转账界面=========");
        if (accountArrayList.size()<2){
            System.out.println("当前系统用户信息不足,无法转账!");
            return;
        }
        if (account.getMoney()==0){
            System.out.println("no money呀~no money呀~");
            return;
        }

        while (true) {
            System.out.println("请输入转账的用户卡号:");
            String cardId=scanner.next();
            if (cardId.equals(account.getCardId())){
                System.out.println("不能给自己转账哦吼吼");
                continue;
            }
            Account account1=getAccountByCardId(cardId,accountArrayList);
            if (account1==null){
                System.out.println("卡号不存在!");
            }else {
                String username=account1.getUsername().substring(1);
                System.out.println("您要转账的用户是:*"+username);
                System.out.println("请输入他/她的姓氏:");
                String x=scanner.next();
                //System.out.println(account1.getUsername().substring(0,1));//String
                if (account1.getUsername().startsWith(x)){
                    while (true) {
                        System.out.println("请输入转账金额:");
                        double money = scanner.nextDouble();
                        if (money>account.getMoney()){
                            System.out.println("余额不足,剩余"+account.getMoney()+"元,无法转账~");
                        }
                        else {
                            account.setMoney(account.getMoney()-money);
                            account1.setMoney(account1.getMoney()+money);
                            System.out.println("转账成功,剩余"+account.getMoney()+"元");
                            return;
                        }
                    }
                }else {
                    System.out.println("姓氏错误!");
                }
            }
        }
    }

    /**
     * 用户取钱
     * @param account 当前用户
     * @param scanner 键盘扫描对象
     */
    private static void withdrawMoney(Account account, Scanner scanner) {
        System.out.println("==========欢迎进入用户取款界面===========");
        if (account.getMoney()<100) {//余额不足
            System.out.println("账户余额"+account.getMoney()+"不足100,先存钱吧!");
            return;
        }else {//余额充足
            while (true) {
                System.out.println("请输入取款金额:");
                double money=scanner.nextDouble();
                if (money>account.getQuotaMoney()){//超过限额
                    System.out.println("您取款超过当此限额!每次最多取"+account.getQuotaMoney()+"元");
                }else {
                    if (money> account.getMoney()){
                        System.out.println("余额不足");
                    }
                    else {
                        account.setMoney(account.getMoney()-money);
                        System.out.println("取款成功!取"+money+"元,当前余额"+account.getMoney());
                        showSearch(account);
                        return;
                    }
                }
            }
        }
    }

    /**
     * 存款界面
     * @param account 存款的用户
     * @param scanner
     */
    private static void saveMoney(Account account, Scanner scanner) {
        System.out.println("=========欢迎进入用户存款界面==========");
        System.out.println("请输入存款的金额:");
        double money=scanner.nextDouble();
        double moneySum=money+account.getMoney();
        account.setMoney(moneySum);
        System.out.println("存款成功!");
        showSearch(account);
    }

    /**
     * 展示当前用户信息
     * @param account
     */
    private static void showSearch(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());
    }

}

练习具体要求:

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值