Java如何实现ATM系统

发布时间:2022-03-17 09:10:46 作者:小新
来源:亿速云 阅读:109

这篇文章主要介绍Java如何实现ATM系统,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1.系统准备,首页,用户开户功能

系统准备,首页设计

系统准备内容分析:

实现步骤:

package com.wangxinhua;

import java.util.ArrayList;
import java.util.Scanner;

public class ATMSystem
{
    public static void main(String[] args)
    {
//        1.准备系统需要的容器对象,用户存储账户对象
        ArrayList<Account> accounts = new ArrayList();

//        2.准备系统的首页,登入,开户
        showMain(accounts);
    }
    public static void showMain(ArrayList<Account> accounts)
    {
        System.out.println("========欢迎进入首页=====");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("请您输入您想做的操作:");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("您可以输入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登入
                    break;
                case 2:
                    //开户
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持!");
            }
        }
    }
}

Java如何实现ATM系统

总结

定义账户类Account,定义系统关心的属性信息

ArrayList<Account> accounts = new ArrayList<> ();

用户开户功能实现

开户功能其实就是往系统的集合容器中存入一个新的账户对象的信息

定义方法完成开户:

public static void register(ArrayList<Account>  accounts){...}
package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem
{
    public static void main(String[] args)
    {
//        1.准备系统需要的容器对象,用户存储账户对象
        ArrayList<Account> accounts = new ArrayList();

//        2.准备系统的首页,登入,开户
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
    {
        System.out.println("=============欢迎进入首页===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("请您输入您想做的操作:");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("您可以输入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登入
                    break;
                case 2:
                    //开户
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持!");
            }
        }
    }


    /**
     * 用户开户功能
     * @param accounts 账户的集合对象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用户开户功能==========");
        //键盘录入 姓名 密码 确认密码
        System.out.println("请您输入开户名称:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("请您输入开户密码:");
            password = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassword = sc.next();

//        判断两次输入的密码是否一致
            if (okPassword.equals(password))
                            //字符串比较用equals
            {
                break;
            }
            else
            {
                System.out.println("两次密码必须一致~~~");
            }
        }


        System.out.println("请您输入当次限额:");
        double quotaMoney = sc.nextDouble();

//        3.生成账户的卡号,卡号是8位,而且不能与其他账户卡号重复。
        String cardId = creatCardId(accounts);


//        4.创建一个账户对象封装账户的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

//        5.把账户对象添加到集合中去
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCardId() + ",请您妥善保管");


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位随机的数字代表卡号
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判断卡号是否重复了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            说明当前卡号没有重复
                return cardId;
            }
        }
    }

    public 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))
            {
                return acc;
            }
        }
        return null;//查无此账户,说明卡号没有重复了!
    }
}

Java如何实现ATM系统

总结

开户功能的实现需要哪几步操作,需要注意什么问题?

2.????用户登入,操作页展示,查询账户,退出账户

用户登入功能实现

分析

public static void login(ArrayList<Account>accounts){...}
package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem
{
    public static void main(String[] args)
    {
//        1.准备系统需要的容器对象,用户存储账户对象
        ArrayList<Account> accounts = new ArrayList();

//        2.准备系统的首页,登入,开户
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
    {
        System.out.println("=============欢迎进入首页===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("请您输入您想做的操作:");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("您可以输入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登录
                    login(accounts,sc);
                    break;
                case 2:
                    //开户
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用户登录
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必须系统中存在账户才可以登录
        if (accounts.size() == 0)
        {
            //没有任何账户
            System.out.println("当前系统中无任何账户,您需要先注册!");
            return;//直接结束方法的执行!
        }
        //2.让用户键盘录入卡号,根据卡号查询账户对象
        while (true) {
            System.out.println("请您输入登录的卡号:");
            String cardId = sc.next();
            //根据卡号查询账户对象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判断账户对象是否存在,存在说明卡号没问题
            if (acc != null)
            {
                while (true)
                {
//                    4.让用户继续输入密码
                    System.out.println("请您输入登录的密码:");
                    String password = sc.next();
//              5.判断密码是否正确
                    if (acc.getPassWord().equals(password))
                    {
                        //密码正确,登入成功
                        //展示系统登录后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId());
//
                    }
                    else
                    {
                        System.out.println("您的密码有误,请确认!");

                    }
                }
            }
            else
            {
                System.out.println("对不起,不存在该卡号的账户!");
            }
        }
    }


    /**
     * 用户开户功能
     * @param accounts 账户的集合对象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用户开户功能==========");
        //键盘录入 姓名 密码 确认密码
        System.out.println("请您输入开户名称:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("请您输入开户密码:");
            password = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassword = sc.next();

//        判断两次输入的密码是否一致
            if (okPassword.equals(password))
                            //字符串比较用equals
            {
                break;
            }
            else
            {
                System.out.println("两次密码必须一致~~~");
            }
        }


        System.out.println("请您输入当次限额:");
        double quotaMoney = sc.nextDouble();

//        3.生成账户的卡号,卡号是8位,而且不能与其他账户卡号重复。
        String cardId = creatCardId(accounts);


//        4.创建一个账户对象封装账户的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

//        5.把账户对象添加到集合中去
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCardId() + ",请您妥善保管");


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位随机的数字代表卡号
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判断卡号是否重复了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            说明当前卡号没有重复
                return cardId;
            }
        }
    }

    public 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))
            {
                return acc;
            }
        }
        return null;//查无此账户,说明卡号没有重复了!
    }
}

Java如何实现ATM系统

总结

登录功能如何实现的?

用户操作页设计,查询账户,退出账户功能

用户操作页,查询账户,退出账户功能分析

package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.准备系统需要的容器对象,用户存储账户对象
        ArrayList<Account> accounts = new ArrayList();

//        2.准备系统的首页,登入,开户
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 开户首页的意思
//            ArrayList<Account> accounts   使用方法定义功能传入容器中  accounts是传参
    {
        System.out.println("=============欢迎进入首页===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("请您输入您想做的操作:");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("您可以输入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登录
                    login(accounts,sc);
                    break;
                case 2:
                    //开户
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用户登录
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必须系统中存在账户才可以登录
        if (accounts.size() == 0)
        {
            //没有任何账户
            System.out.println("当前系统中无任何账户,您需要先注册!");
            return;//直接结束方法的执行!
        }
        //2.让用户键盘录入卡号,根据卡号查询账户对象
        while (true) {
            System.out.println("请您输入登录的卡号:");
            String cardId = sc.next();
            //根据卡号查询账户对象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判断账户对象是否存在,存在说明卡号没问题
            if (acc != null)
            {
                while (true)
                {
//                    4.让用户继续输入密码
                    System.out.println("请您输入登录的密码:");
                    String password = sc.next();
//              5.判断密码是否正确
                    if (acc.getPassWord().equals(password))
                    {
                        //密码正确,登入成功
                        //展示系统登录后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId());
                        //展示操作页面
                        showUserCommand(sc,acc);
                        return;//继续结束登录方法
                    }
                    else
                    {
                        System.out.println("您的密码有误,请确认!");

                    }
                }
            }
            else
            {
                System.out.println("对不起,不存在该卡号的账户!");
            }
        }
    }

    private static void showUserCommand(Scanner sc,Account acc) {
        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 = sc.nextInt();
            switch (command)
            {
                case 1:
                    //查询账户
                    showAccount(acc);
                    break;
                case 2:
                    //存款
                    break;
                case 3:
                    //取款
                    break;
                case 4:
                    //转账
                    break;
                case 5:
                    //修改密码
                    break;
                case 6:
                    //退出
                    System.out.println("欢迎下次光临!!");
                    return; //结束当前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注销账户
                    break;
                default:
                    System.out.println("您输入有误!");
            }
        }
    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========当前账户详情=========");
        System.out.println("卡号" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余额" + acc.getMoney());
        System.out.println("当次限额:" + acc.getQuotaMoney());

    }


    /**
     * 用户开户功能
     * @param accounts 账户的集合对象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用户开户功能==========");
        //键盘录入 姓名 密码 确认密码
        System.out.println("请您输入开户名称:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("请您输入开户密码:");
            password = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassword = sc.next();

//        判断两次输入的密码是否一致
            if (okPassword.equals(password))
                            //字符串比较用equals
            {
                break;
            }
            else
            {
                System.out.println("两次密码必须一致~~~");
            }
        }


        System.out.println("请您输入当次限额:");
        double quotaMoney = sc.nextDouble();

//        3.生成账户的卡号,卡号是8位,而且不能与其他账户卡号重复。
        String cardId = creatCardId(accounts);


//        4.创建一个账户对象封装账户的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

//        5.把账户对象添加到集合中去
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCardId() + ",请您妥善保管");


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位随机的数字代表卡号
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判断卡号是否重复了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            说明当前卡号没有重复
                return cardId;
            }
        }
    }

    public 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))
            {
                return acc;
            }
        }
        return null;//查无此账户,说明卡号没有重复了!
    }
}

Java如何实现ATM系统

总结

用户操作页设计,查询账户,退出账户功能注意事项

3.????用户存款与取款

用户存款

存款分析

package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.准备系统需要的容器对象,用户存储账户对象
        ArrayList<Account> accounts = new ArrayList();

//        2.准备系统的首页,登入,开户
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 开户首页的意思
//            ArrayList<Account> accounts   使用方法定义功能传入容器中  accounts是传参
    {
        System.out.println("=============欢迎进入首页===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("请您输入您想做的操作:");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("您可以输入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登录
                    login(accounts,sc);
                    break;
                case 2:
                    //开户
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用户登录
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必须系统中存在账户才可以登录
        if (accounts.size() == 0)
        {
            //没有任何账户
            System.out.println("当前系统中无任何账户,您需要先注册!");
            return;//直接结束方法的执行!
        }
        //2.让用户键盘录入卡号,根据卡号查询账户对象
        while (true) {
            System.out.println("请您输入登录的卡号:");
            String cardId = sc.next();
            //根据卡号查询账户对象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判断账户对象是否存在,存在说明卡号没问题
            if (acc != null)
            {
                while (true)
                {
//                    4.让用户继续输入密码
                    System.out.println("请您输入登录的密码:");
                    String password = sc.next();
//              5.判断密码是否正确
                    if (acc.getPassWord().equals(password))
                    {
                        //密码正确,登入成功
                        //展示系统登录后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId());
                        //展示操作页面
                        showUserCommand(sc,acc);
                        return;//继续结束登录方法
                    }
                    else
                    {
                        System.out.println("您的密码有误,请确认!");

                    }
                }
            }
            else
            {
                System.out.println("对不起,不存在该卡号的账户!");
            }
        }
    }

    private static void showUserCommand(Scanner sc,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.注销账户");
            System.out.println("请您输入操作命令:");
            int command = sc.nextInt();
            switch (command)
            {
                case 1:
                    //查询账户
                    showAccount(acc);
                    break;
                case 2:
                    //存款
                    depositMoney(acc,sc);
                    break;
                case 3:
                    //取款
                    break;
                case 4:
                    //转账
                    break;
                case 5:
                    //修改密码
                    break;
                case 6:
                    //退出
                    System.out.println("欢迎下次光临!!");
                    return; //结束当前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注销账户
                    break;
                default:
                    System.out.println("您输入有误!");
            }
        }
    }

    /**
     * 专门存钱的
     * @param acc
     */
    private static void depositMoney(Account acc,Scanner sc) {
        System.out.println("===========存钱操作=========");
        System.out.println("请您输入存款的金额:");
        double money = sc.nextDouble();

        //直接把金额修改到账户对象的money属性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========当前账户详情=========");
        System.out.println("卡号" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余额" + acc.getMoney());
        System.out.println("当次限额:" + acc.getQuotaMoney());

    }


    /**
     * 用户开户功能
     * @param accounts 账户的集合对象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用户开户功能==========");
        //键盘录入 姓名 密码 确认密码
        System.out.println("请您输入开户名称:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("请您输入开户密码:");
            password = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassword = sc.next();

//        判断两次输入的密码是否一致
            if (okPassword.equals(password))
                            //字符串比较用equals
            {
                break;
            }
            else
            {
                System.out.println("两次密码必须一致~~~");
            }
        }


        System.out.println("请您输入当次限额:");
        double quotaMoney = sc.nextDouble();

//        3.生成账户的卡号,卡号是8位,而且不能与其他账户卡号重复。
        String cardId = creatCardId(accounts);


//        4.创建一个账户对象封装账户的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

//        5.把账户对象添加到集合中去
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCardId() + ",请您妥善保管");


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位随机的数字代表卡号
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判断卡号是否重复了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            说明当前卡号没有重复
                return cardId;
            }
        }
    }

    public 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))
            {
                return acc;
            }
        }
        return null;
        //查无此账户,说明卡号没有重复了!
    }
}

Java如何实现ATM系统

总结

存款分析

取款分析

package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.准备系统需要的容器对象,用户存储账户对象
        ArrayList<Account> accounts = new ArrayList();

//        2.准备系统的首页,登入,开户
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 开户首页的意思
//            ArrayList<Account> accounts   使用方法定义功能传入容器中  accounts是传参
    {
        System.out.println("=============欢迎进入首页===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("请您输入您想做的操作:");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("您可以输入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登录
                    login(accounts,sc);
                    break;
                case 2:
                    //开户
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用户登录
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必须系统中存在账户才可以登录
        if (accounts.size() == 0)
        {
            //没有任何账户
            System.out.println("当前系统中无任何账户,您需要先注册!");
            return;//直接结束方法的执行!
        }
        //2.让用户键盘录入卡号,根据卡号查询账户对象
        while (true) {
            System.out.println("请您输入登录的卡号:");
            String cardId = sc.next();
            //根据卡号查询账户对象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判断账户对象是否存在,存在说明卡号没问题
            if (acc != null)
            {
                while (true)
                {
//                    4.让用户继续输入密码
                    System.out.println("请您输入登录的密码:");
                    String password = sc.next();
//              5.判断密码是否正确
                    if (acc.getPassWord().equals(password))
                    {
                        //密码正确,登入成功
                        //展示系统登录后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId());
                        //展示操作页面
                        showUserCommand(sc,acc);
                        return;//继续结束登录方法
                    }
                    else
                    {
                        System.out.println("您的密码有误,请确认!");

                    }
                }
            }
            else
            {
                System.out.println("对不起,不存在该卡号的账户!");
            }
        }
    }

    private static void showUserCommand(Scanner sc,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.注销账户");
            System.out.println("请您输入操作命令:");
            int command = sc.nextInt();
            switch (command)
            {
                case 1:
                    //查询账户
                    showAccount(acc);
                    break;
                case 2:
                    //存款
                    depositMoney(acc,sc);
                    break;
                case 3:
                    //取款
                    drawMoney(acc,sc);
                    break;
                case 4:
                    //转账
                    break;
                case 5:
                    //修改密码
                    break;
                case 6:
                    //退出
                    System.out.println("欢迎下次光临!!");
                    return; //结束当前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注销账户
                    break;
                default:
                    System.out.println("您输入有误!");
            }
        }
    }

    /**
     * 取款操作
     * @param acc
     * @param sc
     */
    private static void drawMoney(Account acc, Scanner sc) {
        System.out.println("==========取款操作=========");
        //1.判断它的账户是否足够100元
        if (acc.getMoney() >= 100)
        {
            while (true) {
                System.out.println("请您输入取款的金额:");
                double money = sc.nextDouble();
                //2.判断整个金额有没有超过当次限额
                if (money > acc.getQuotaMoney())
                {
                    System.out.println("您当次取款金额超过每次限额,不要取那么多,每次最多可以取:" + acc.getQuotaMoney());
                }
                else
                {
                    //3.判断当前余额是否足够你取钱
                    if (acc.getMoney() >= money)
                    {
                        //够了,可以取钱了
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您,取钱" + money + "成功了!当前账户还剩余:" + acc.getMoney());
                        return;//取钱后干掉了取钱方法
                    }
                    else
                    {
                        System.out.println("余额不足啊!!");
                    }
                }
            }
        }
        else
        {
            System.out.println("您自己的金额没有超过100元,该努力工作了~~~");
        }

    }

    /**
     * 专门存钱的
     * @param acc
     */
    private static void depositMoney(Account acc,Scanner sc) {
        System.out.println("===========存钱操作=========");
        System.out.println("请您输入存款的金额:");
        double money = sc.nextDouble();

        //直接把金额修改到账户对象的money属性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========当前账户详情=========");
        System.out.println("卡号" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余额" + acc.getMoney());
        System.out.println("当次限额:" + acc.getQuotaMoney());

    }


    /**
     * 用户开户功能
     * @param accounts 账户的集合对象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用户开户功能==========");
        //键盘录入 姓名 密码 确认密码
        System.out.println("请您输入开户名称:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("请您输入开户密码:");
            password = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassword = sc.next();

//        判断两次输入的密码是否一致
            if (okPassword.equals(password))
                            //字符串比较用equals
            {
                break;
            }
            else
            {
                System.out.println("两次密码必须一致~~~");
            }
        }


        System.out.println("请您输入当次限额:");
        double quotaMoney = sc.nextDouble();

//        3.生成账户的卡号,卡号是8位,而且不能与其他账户卡号重复。
        String cardId = creatCardId(accounts);


//        4.创建一个账户对象封装账户的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

//        5.把账户对象添加到集合中去
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCardId() + ",请您妥善保管");


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位随机的数字代表卡号
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判断卡号是否重复了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            说明当前卡号没有重复
                return cardId;
            }
        }
    }

    public 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))
            {
                return acc;
            }
        }
        return null;
        //查无此账户,说明卡号没有重复了!
    }
}

Java如何实现ATM系统

总结温习

4.????用户转账,修改密码,销户

用户转账功能

分析

package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.准备系统需要的容器对象,用户存储账户对象
        ArrayList<Account> accounts = new ArrayList();

//        2.准备系统的首页,登入,开户
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 开户首页的意思
//            ArrayList<Account> accounts   使用方法定义功能传入容器中  accounts是传参
    {
        System.out.println("=============欢迎进入首页===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("请您输入您想做的操作:");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("您可以输入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登录
                    login(accounts,sc);
                    break;
                case 2:
                    //开户
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用户登录
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必须系统中存在账户才可以登录
        if (accounts.size() == 0)
        {
            //没有任何账户
            System.out.println("当前系统中无任何账户,您需要先注册!");
            return;//直接结束方法的执行!
        }
        //2.让用户键盘录入卡号,根据卡号查询账户对象
        while (true) {
            System.out.println("请您输入登录的卡号:");
            String cardId = sc.next();
            //根据卡号查询账户对象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判断账户对象是否存在,存在说明卡号没问题
            if (acc != null)
            {
                while (true)
                {
//                    4.让用户继续输入密码
                    System.out.println("请您输入登录的密码:");
                    String password = sc.next();
//              5.判断密码是否正确
                    if (acc.getPassWord().equals(password))
                    {
                        //密码正确,登入成功
                        //展示系统登录后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId());
                        //展示操作页面
                        showUserCommand(sc,acc,accounts);
                        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(acc,sc);
                    break;
                case 3:
                    //取款
                    drawMoney(acc,sc);
                    break;
                case 4:
                    //转账
                    transferMoney(accounts,acc ,sc);
                    break;
                case 5:
                    //修改密码
                    break;
                case 6:
                    //退出
                    System.out.println("欢迎下次光临!!");
                    return; //结束当前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注销账户
                    break;
                default:
                    System.out.println("您输入有误!");
            }
        }
    }

    /**
     * 转账功能
     * @param accounts
     * @param acc
     * @param sc
     */
    private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc)
    {
        //1.判断系统中是否有2个账户及以上
        if (accounts.size() < 2)
        {
            System.out.println("对不起,系统中无其他账户,您不可以转账!!");
            return;
        }

        //2.判断自己的账户对象中是否有钱
        if (acc.getMoney() == 0)
        {
            System.out.println("对不起,您自己都快吃土了,就别装逼了!!");
            return;
        }

        //3.开始转账逻辑
        while (true)
        {
            System.out.println("请您输入对方账户的卡号:");
            String cardId = sc.next();
            Account account = getAccountBycardId(cardId,accounts);
            //判断整个账户对象是否存在,存在说明对方卡号输入正确
            if (account != null)
            {
                //判断这个账户对象是否是当前自己登录的账户
                if (account.getCardId().equals(acc.getCardId()))
                {
                    //也就是这里企图想给自己转账
                    System.out.println("您不能给自己转账!");
                }
                else
                {
                    //确认对方的姓氏
                    String name = "*" + account.getUserWord().substring(1);
                    System.out.println("请您确认【" + name + "】的姓氏:");
                    String preName = sc.next();
                    //判断
                    if (account.getUserWord().startsWith(preName))
                    {
                        //真正的转账才刚刚开始
                        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("恭喜您,转账成功了,已经为" + account.getUserWord() + "转账了:" + money);
                            showAccount(acc);
                            return;

                        }
                    }
                    else
                    {
                        System.out.println("对不起,您认证的信息有误~~~");
                    }
                }

            }
            else
            {
                System.out.println("对不起,您输入的转账卡号有问题!!");

            }
        }
    }

    /**
     * 取款操作
     * @param acc
     * @param sc
     */
    private static void drawMoney(Account acc, Scanner sc)
    {
        System.out.println("==========取款操作=========");
        //1.判断它的账户是否足够100元
        if (acc.getMoney() >= 100)
        {
            while (true) {
                System.out.println("请您输入取款的金额:");
                double money = sc.nextDouble();
                //2.判断整个金额有没有超过当次限额
                if (money > acc.getQuotaMoney())
                {
                    System.out.println("您当次取款金额超过每次限额,不要取那么多,每次最多可以取:" + acc.getQuotaMoney());
                }
                else
                {
                    //3.判断当前余额是否足够你取钱
                    if (acc.getMoney() >= money)
                    {
                        //够了,可以取钱了
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您,取钱" + money + "成功了!当前账户还剩余:" + acc.getMoney());
                        return;//取钱后干掉了取钱方法
                    }
                    else
                    {
                        System.out.println("余额不足啊!!");
                    }
                }
            }
        }
        else
        {
            System.out.println("您自己的金额没有超过100元,该努力工作了~~~");
        }

    }

    /**
     * 专门存钱的
     * @param acc
     */
    private static void depositMoney(Account acc,Scanner sc) {
        System.out.println("===========存钱操作=========");
        System.out.println("请您输入存款的金额:");
        double money = sc.nextDouble();

        //直接把金额修改到账户对象的money属性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========当前账户详情=========");
        System.out.println("卡号" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余额" + acc.getMoney());
        System.out.println("当次限额:" + acc.getQuotaMoney());

    }


    /**
     * 用户开户功能
     * @param accounts 账户的集合对象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用户开户功能==========");
        //键盘录入 姓名 密码 确认密码
        System.out.println("请您输入开户名称:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("请您输入开户密码:");
            password = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassword = sc.next();

//        判断两次输入的密码是否一致
            if (okPassword.equals(password))
                            //字符串比较用equals
            {
                break;
            }
            else
            {
                System.out.println("两次密码必须一致~~~");
            }
        }


        System.out.println("请您输入当次限额:");
        double quotaMoney = sc.nextDouble();

//        3.生成账户的卡号,卡号是8位,而且不能与其他账户卡号重复。
        String cardId = creatCardId(accounts);


//        4.创建一个账户对象封装账户的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

//        5.把账户对象添加到集合中去
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCardId() + ",请您妥善保管");


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位随机的数字代表卡号
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判断卡号是否重复了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            说明当前卡号没有重复
                return cardId;
            }
        }
    }

    public 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))
            {
                return acc;
            }
        }
        return null;
        //查无此账户,说明卡号没有重复了!
    }
}

Java如何实现ATM系统

总结温习

分析

这里为止所有的ATM系统的操作代码就已经完成

package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.准备系统需要的容器对象,用户存储账户对象
        ArrayList<Account> accounts = new ArrayList();

//        2.准备系统的首页,登入,开户
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 开户首页的意思
//            ArrayList<Account> accounts   使用方法定义功能传入容器中  accounts是传参
    {
        System.out.println("=============欢迎进入首页===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("请您输入您想做的操作:");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("您可以输入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登录
                    login(accounts,sc);
                    break;
                case 2:
                    //开户
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用户登录
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必须系统中存在账户才可以登录
        if (accounts.size() == 0)
        {
            //没有任何账户
            System.out.println("当前系统中无任何账户,您需要先注册!");
            return;//直接结束方法的执行!
        }
        //2.让用户键盘录入卡号,根据卡号查询账户对象
        while (true) 
        {
            System.out.println("请您输入登录的卡号:");
            String cardId = sc.next();
            //根据卡号查询账户对象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判断账户对象是否存在,存在说明卡号没问题
            if (acc != null)
            {
                while (true)
                {
//                    4.让用户继续输入密码
                    System.out.println("请您输入登录的密码:");
                    String password = sc.next();
//              5.判断密码是否正确
                    if (acc.getPassWord().equals(password))
                    {
                        //密码正确,登入成功
                        //展示系统登录后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId());
                        //展示操作页面
                        showUserCommand(sc,acc,accounts);
                        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(acc,sc);
                    break;
                case 3:
                    //取款
                    drawMoney(acc,sc);
                    break;
                case 4:
                    //转账
                    transferMoney(accounts,acc ,sc);
                    break;
                case 5:
                    //修改密码
                    updataPassWord(acc,sc);
                    return;//结束当前…………
                case 6:
                    //退出
                    System.out.println("欢迎下次光临!!");
                    return; //结束当前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注销账户
                    //从当前集合中抹掉当前账户对象即可
                    accounts.remove(acc);
                    System.out.println("销户成功了!!");
                    return;
                default:
                    System.out.println("您输入有误!");
            }
        }
    }

    /**
     * 修改密码
     * @param acc
     */
    private static void updataPassWord(Account acc,Scanner sc)
    {
        System.out.println("===========修改密码=========");
        while (true)
        {
            System.out.println("请您输入正确的密码:");
            String okPassWord = sc.next();
            //判断密码是否正确
            if (acc.getPassWord().equals(okPassWord))
            {
                //可以输入新密码
                System.out.println("请您输入新的密码:");
                String newPassWord = sc.next();

                System.out.println("请您输入确认密码:");
                String okNewPassWord = sc.next();

                if (newPassWord.equals(okNewPassWord))
                {
                    //修改账户对象的密码为新密码
                    acc.setPassWord(newPassWord);
                    return;//直接结束方法!
                }
                else
                {
                    System.out.println("您两次输入的密码不一致~~");
                }
            }
            else
            {
                System.out.println("当前输入的密码不正确~~~");
            }
        }

    }

    /**
     * 转账功能
     * @param accounts
     * @param acc
     * @param sc
     */
    private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc)
    {
        //1.判断系统中是否有2个账户及以上
        if (accounts.size() < 2)
        {
            System.out.println("对不起,系统中无其他账户,您不可以转账!!");
            return;
        }

        //2.判断自己的账户对象中是否有钱
        if (acc.getMoney() == 0)
        {
            System.out.println("对不起,您自己都快吃土了,就别装逼了!!");
            return;
        }

        //3.开始转账逻辑
        while (true)
        {
            System.out.println("请您输入对方账户的卡号:");
            String cardId = sc.next();
            Account account = getAccountBycardId(cardId,accounts);
            //判断整个账户对象是否存在,存在说明对方卡号输入正确
            if (account != null)
            {
                //判断这个账户对象是否是当前自己登录的账户
                if (account.getCardId().equals(acc.getCardId()))
                {
                    //也就是这里企图想给自己转账
                    System.out.println("您不能给自己转账!");
                }
                else
                {
                    //确认对方的姓氏
                    String name = "*" + account.getUserWord().substring(1);
                    System.out.println("请您确认【" + name + "】的姓氏:");
                    String preName = sc.next();
                    //判断
                    if (account.getUserWord().startsWith(preName))
                    {
                        //真正的转账才刚刚开始
                        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("恭喜您,转账成功了,已经为" + account.getUserWord() + "转账了:" + money);
                            showAccount(acc);
                            return;

                        }
                    }
                    else
                    {
                        System.out.println("对不起,您认证的信息有误~~~");
                    }
                }

            }
            else
            {
                System.out.println("对不起,您输入的转账卡号有问题!!");

            }
        }
    }

    /**
     * 取款操作
     * @param acc
     * @param sc
     */
    private static void drawMoney(Account acc, Scanner sc)
    {
        System.out.println("==========取款操作=========");
        //1.判断它的账户是否足够100元
        if (acc.getMoney() >= 100)
        {
            while (true) {
                System.out.println("请您输入取款的金额:");
                double money = sc.nextDouble();
                //2.判断整个金额有没有超过当次限额
                if (money > acc.getQuotaMoney())
                {
                    System.out.println("您当次取款金额超过每次限额,不要取那么多,每次最多可以取:" + acc.getQuotaMoney());
                }
                else
                {
                    //3.判断当前余额是否足够你取钱
                    if (acc.getMoney() >= money)
                    {
                        //够了,可以取钱了
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您,取钱" + money + "成功了!当前账户还剩余:" + acc.getMoney());
                        return;//取钱后干掉了取钱方法
                    }
                    else
                    {
                        System.out.println("余额不足啊!!");
                    }
                }
            }
        }
        else
        {
            System.out.println("您自己的金额没有超过100元,该努力工作了~~~");
        }

    }

    /**
     * 专门存钱的
     * @param acc
     */
    private static void depositMoney(Account acc,Scanner sc) 
    {
        System.out.println("===========存钱操作=========");
        System.out.println("请您输入存款的金额:");
        double money = sc.nextDouble();

        //直接把金额修改到账户对象的money属性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========当前账户详情=========");
        System.out.println("卡号" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余额" + acc.getMoney());
        System.out.println("当次限额:" + acc.getQuotaMoney());

    }


    /**
     * 用户开户功能
     * @param accounts 账户的集合对象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用户开户功能==========");
        //键盘录入 姓名 密码 确认密码
        System.out.println("请您输入开户名称:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("请您输入开户密码:");
            password = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassword = sc.next();

//        判断两次输入的密码是否一致
            if (okPassword.equals(password))
                            //字符串比较用equals
            {
                break;
            }
            else
            {
                System.out.println("两次密码必须一致~~~");
            }
        }


        System.out.println("请您输入当次限额:");
        double quotaMoney = sc.nextDouble();

//        3.生成账户的卡号,卡号是8位,而且不能与其他账户卡号重复。
        String cardId = creatCardId(accounts);


//        4.创建一个账户对象封装账户的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

//        5.把账户对象添加到集合中去
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCardId() + ",请您妥善保管");


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位随机的数字代表卡号
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判断卡号是否重复了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            说明当前卡号没有重复
                return cardId;
            }
        }
    }

    public 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))
            {
                return acc;
            }
        }
        return null;
        //查无此账户,说明卡号没有重复了!
    }
}

Java如何实现ATM系统

5. ????源代码在这里这里拿

package com.wangxinhua;


/**
    账户类
 */
public class Account {
    private String CardId;//卡号
    private String UserWord;//客户名称
    private String PassWord;//密码
    private double Money;//余额
    private double QuotaMoney;//当次取现限额

//无参函数
    public Account() {
    }

//    构造好了有参函数,那么就会有无参函数
//    有参函数
    public Account(String cardId, String userWord, String passWord, double quotaMoney) {
        CardId = cardId;
        UserWord = userWord;
        PassWord = passWord;
        QuotaMoney = quotaMoney;
    }

    public String getCardId() {
        return CardId;
    }

    public void setCardId(String cardId) {
        CardId = cardId;
    }

    public String getUserWord() {
        return UserWord;
    }

    public void setUserWord(String userWord) {
        UserWord = userWord;
    }

    public String getPassWord() {
        return PassWord;
    }

    public void setPassWord(String passWord) {
        PassWord = passWord;
    }

    public double getMoney() {
        return Money;
    }

    public void setMoney(double money) {
        Money = money;
    }

    public double getQuotaMoney() {
        return QuotaMoney;
    }

    public void setQuotaMoney(double quotaMoney) {
        QuotaMoney = quotaMoney;
    }
}

这里是第一个类用于构造函数 下面这个是第二个类

package com.wangxinhua;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args)
    {
//        1.准备系统需要的容器对象,用户存储账户对象
        ArrayList<Account> accounts = new ArrayList();

//        2.准备系统的首页,登入,开户
        showMain(accounts); 
    }
    public static void showMain(ArrayList<Account> accounts)
            //showMain 开户首页的意思
//            ArrayList<Account> accounts   使用方法定义功能传入容器中  accounts是传参
    {
        System.out.println("=============欢迎进入首页===========");
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("请您输入您想做的操作:");
            System.out.println("1.登录");
            System.out.println("2.开户");
            System.out.println("您可以输入命令了:");
            int command = sc.nextInt();
            switch(command)
            {
                case 1:
                    //登录
                    login(accounts,sc);
                    break;
                case 2:
                    //开户
                    register(accounts,sc);
                    break;
                default:
                    System.out.println("您当前输入的操作命令不被支持!");
            }
        }
    }


    /**
     * 完成用户登录
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc)
    {
        //必须系统中存在账户才可以登录
        if (accounts.size() == 0)
        {
            //没有任何账户
            System.out.println("当前系统中无任何账户,您需要先注册!");
            return;//直接结束方法的执行!
        }
        //2.让用户键盘录入卡号,根据卡号查询账户对象
        while (true)
        {
            System.out.println("请您输入登录的卡号:");
            String cardId = sc.next();
            //根据卡号查询账户对象
            Account acc = getAccountBycardId(cardId,accounts);
//        3.判断账户对象是否存在,存在说明卡号没问题
            if (acc != null)
            {
                while (true)
                {
//                    4.让用户继续输入密码
                    System.out.println("请您输入登录的密码:");
                    String password = sc.next();
//              5.判断密码是否正确
                    if (acc.getPassWord().equals(password))
                    {
                        //密码正确,登入成功
                        //展示系统登录后的操作界面
                        System.out.println("恭喜您," + acc.getUserWord() +",成功登入系统,您的卡号是:" + acc.getCardId());
                        //展示操作页面
                        showUserCommand(sc,acc,accounts);
                        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(acc,sc);
                    break;
                case 3:
                    //取款
                    drawMoney(acc,sc);
                    break;
                case 4:
                    //转账
                    transferMoney(accounts,acc ,sc);
                    break;
                case 5:
                    //修改密码
                    updataPassWord(acc,sc);
                    return;//结束当前…………
                case 6:
                    //退出
                    System.out.println("欢迎下次光临!!");
                    return; //结束当前showUserCommand(Scanner sc,Account acc)的方法
                case 7:
                    //注销账户
                    //从当前集合中抹掉当前账户对象即可
                    accounts.remove(acc);
                    System.out.println("销户成功了!!");
                    return;
                default:
                    System.out.println("您输入有误!");
            }
        }
    }

    /**
     * 修改密码
     * @param acc
     */
    private static void updataPassWord(Account acc,Scanner sc)
    {
        System.out.println("===========修改密码=========");
        while (true)
        {
            System.out.println("请您输入正确的密码:");
            String okPassWord = sc.next();
            //判断密码是否正确
            if (acc.getPassWord().equals(okPassWord))
            {
                //可以输入新密码
                System.out.println("请您输入新的密码:");
                String newPassWord = sc.next();

                System.out.println("请您输入确认密码:");
                String okNewPassWord = sc.next();

                if (newPassWord.equals(okNewPassWord))
                {
                    //修改账户对象的密码为新密码
                    acc.setPassWord(newPassWord);
                    return;//直接结束方法!
                }
                else
                {
                    System.out.println("您两次输入的密码不一致~~");
                }
            }
            else
            {
                System.out.println("当前输入的密码不正确~~~");
            }
        }

    }

    /**
     * 转账功能
     * @param accounts
     * @param acc
     * @param sc
     */
    private static void transferMoney(ArrayList<Account> accounts, Account acc, Scanner sc)
    {
        //1.判断系统中是否有2个账户及以上
        if (accounts.size() < 2)
        {
            System.out.println("对不起,系统中无其他账户,您不可以转账!!");
            return;
        }

        //2.判断自己的账户对象中是否有钱
        if (acc.getMoney() == 0)
        {
            System.out.println("对不起,您自己都快吃土了,就别装逼了!!");
            return;
        }

        //3.开始转账逻辑
        while (true)
        {
            System.out.println("请您输入对方账户的卡号:");
            String cardId = sc.next();
            Account account = getAccountBycardId(cardId,accounts);
            //判断整个账户对象是否存在,存在说明对方卡号输入正确
            if (account != null)
            {
                //判断这个账户对象是否是当前自己登录的账户
                if (account.getCardId().equals(acc.getCardId()))
                {
                    //也就是这里企图想给自己转账
                    System.out.println("您不能给自己转账!");
                }
                else
                {
                    //确认对方的姓氏
                    String name = "*" + account.getUserWord().substring(1);
                    System.out.println("请您确认【" + name + "】的姓氏:");
                    String preName = sc.next();
                    //判断
                    if (account.getUserWord().startsWith(preName))
                    {
                        //真正的转账才刚刚开始
                        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("恭喜您,转账成功了,已经为" + account.getUserWord() + "转账了:" + money);
                            showAccount(acc);
                            return;

                        }
                    }
                    else
                    {
                        System.out.println("对不起,您认证的信息有误~~~");
                    }
                }

            }
            else
            {
                System.out.println("对不起,您输入的转账卡号有问题!!");

            }
        }
    }

    /**
     * 取款操作
     * @param acc
     * @param sc
     */
    private static void drawMoney(Account acc, Scanner sc)
    {
        System.out.println("==========取款操作=========");
        //1.判断它的账户是否足够100元
        if (acc.getMoney() >= 100)
        {
            while (true) {
                System.out.println("请您输入取款的金额:");
                double money = sc.nextDouble();
                //2.判断整个金额有没有超过当次限额
                if (money > acc.getQuotaMoney())
                {
                    System.out.println("您当次取款金额超过每次限额,不要取那么多,每次最多可以取:" + acc.getQuotaMoney());
                }
                else
                {
                    //3.判断当前余额是否足够你取钱
                    if (acc.getMoney() >= money)
                    {
                        //够了,可以取钱了
                        acc.setMoney(acc.getMoney() - money);
                        System.out.println("恭喜您,取钱" + money + "成功了!当前账户还剩余:" + acc.getMoney());
                        return;//取钱后干掉了取钱方法
                    }
                    else
                    {
                        System.out.println("余额不足啊!!");
                    }
                }
            }
        }
        else
        {
            System.out.println("您自己的金额没有超过100元,该努力工作了~~~");
        }

    }

    /**
     * 专门存钱的
     * @param acc
     */
    private static void depositMoney(Account acc,Scanner sc)
    {
        System.out.println("===========存钱操作=========");
        System.out.println("请您输入存款的金额:");
        double money = sc.nextDouble();

        //直接把金额修改到账户对象的money属性中去
        acc.setMoney(acc.getMoney() + money);
        System.out.println("存款完成!");
        showAccount(acc);

    }

    private static void showAccount(Account acc)
    {
        System.out.println("===========当前账户详情=========");
        System.out.println("卡号" + acc.getCardId());
        System.out.println("姓名" + acc.getUserWord());
        System.out.println("余额" + acc.getMoney());
        System.out.println("当次限额:" + acc.getQuotaMoney());

    }


    /**
     * 用户开户功能
     * @param accounts 账户的集合对象
      */
    private static void register(ArrayList<Account> accounts, Scanner sc)
    {


        System.out.println("=========用户开户功能==========");
        //键盘录入 姓名 密码 确认密码
        System.out.println("请您输入开户名称:");
        String name = sc.next();


        String password = "";
        while (true)
        {
            System.out.println("请您输入开户密码:");
            password = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassword = sc.next();

//        判断两次输入的密码是否一致
            if (okPassword.equals(password))
                            //字符串比较用equals
            {
                break;
            }
            else
            {
                System.out.println("两次密码必须一致~~~");
            }
        }


        System.out.println("请您输入当次限额:");
        double quotaMoney = sc.nextDouble();

//        3.生成账户的卡号,卡号是8位,而且不能与其他账户卡号重复。
        String cardId = creatCardId(accounts);


//        4.创建一个账户对象封装账户的信息
//        public Account(String cardId, String userWord, String passWord, double money, double quotaMoney)
        Account account = new Account(cardId,name,password,quotaMoney);

//        5.把账户对象添加到集合中去
        accounts.add(account);
        System.out.println("恭喜您,您开户成功,您的卡号是:" + account.getCardId() + ",请您妥善保管");


    }

    public static String creatCardId(ArrayList<Account> accouts)
    {
        while (true) {
//        生成8位随机的数字代表卡号
            String cardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; i++)
            {
                cardId += r.nextInt(10);
            }

//        判断卡号是否重复了
            Account acc = getAccountBycardId(cardId,accouts);
            if (acc == null)
            {
    //            说明当前卡号没有重复
                return cardId;
            }
        }
    }

    public 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))
            {
                return acc;
            }
        }
        return null;
        //查无此账户,说明卡号没有重复了!
    }
}

Java如何实现ATM系统

以上是“Java如何实现ATM系统”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. java连接MySQL。ATM
  2. 如何使用Python实现ATM系统

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java atm

上一篇:python3怎么给数据添加高斯噪声

下一篇:怎么利用feign调用返回object类型转换成实体

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》