JAVA初学者练手项目1(银行ATM系统)

JAVASE基础阶段练手项目

ATM银行系统

  • 实现一个简单的ATM系统
  • 1、登录注册功能(账号(卡号),密码(大小写字母+数字+其他符号))
  • 2、账户类、每个用户拥有独立的信息(卡号、密码、用户名、账户余额、取款额度)
  • 3.1、查询功能,可以显示账户信息
  • 3.2、存取款功能
  • 3.3、转账功能
  • 3.4、汇款功能(通过输入相应的账户名、卡号,汇给对方账户内的金额)
  • 4、账号信息更改
  • 5管理员(账号:adminster 密码:14520)
  • 5.1、可以看到集合内的所有的对象信息
  • 由于目前只是初步学习了集合的思想,和最基础的JAVA语法,所以整个系统代码显得十分臃肿,难以读取,但是照着我的思路走还是十分简单的
    */
    ATMDemo
package ATMSystem;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

/**
 * 实现一个简单的ATM系统
 * 1、登录注册功能(账号(卡号),密码(大小写字母+数字+其他符号))
 * 2、账户类、每个用户拥有独立的信息(卡号、密码、用户名、账户余额、取款额度)
 * 3.1、查询功能,可以显示账户信息
 * 3.2、存取款功能
 * 3.3、转账功能
 * 3.4、汇款功能(通过输入相应的账户名、卡号,汇给对方账户内的金额)
 * 4、账号信息更改
 * 5管理员(账号:adminster 密码:14520)
 * 5.1、可以看到集合内的所有的对象信息
 * 由于目前只是初步学习了集合的思想,和最基础的JAVA语法,所以整个系统代码显得十分臃肿,难以读取,但是照着我的思路走还是十分简单的
 */
//问题
/*

 * */
public class ATMDemo {


    public static void main(String[] args) {
        ArrayList users = new ArrayList<>();
        ArrayList systemers = new ArrayList<>();
        while (true) {
            System.out.println("=========欢迎进入ATM系统,请登录========");
            Time();
            Scanner sc = new Scanner(System.in);
            User users1 = new User();
            System.out.println("             请输入数字      ");
            System.out.println("1:             登录");
            System.out.println("2:             注册");
            System.out.println("3:          进入管理系统");
//            System.out.println("4:            退出ATM系统");
/*            System.out.println("3:           查询用户信息");
            System.out.println("4:            账户操作");
            System.out.println("5:          用户信息更改");*/
            System.out.println("6:            退出ATM系统");

            System.out.println();
            String choose = sc.next();
            switch (choose) {
                case "1":
                    System.out.println("登录");
                    Sign(users, users1, sc);

                    break;
                case "2":
                    System.out.println("注册");
                    register(users, users1, sc);
                    break;
                case "3":
                    System.out.println("----管理员登录---");
                    sign_system(systemers, users, sc);
                    System.out.println(" ");

                    break;
                  /*  break;
                case "4":
                    System.out.println("账户操作");

                    break;
                case "5":
                    System.out.println("用户信息更改");

                    break;*/
                case "6":
                    System.out.println("=====欢迎下次使用====");
                    System.exit(0);
                    break;
                default:
                    System.out.println("=====您输入的有误请重新输入=====");
            }


        }
    }


    //    用户登录注册功能
    public static void Sign(ArrayList users, User users1, Scanner sc) {
        if (users.size() > 0) {
            while (true) {

                System.out.println("请输入账户");
                String s = sc.next();
                int index = -1;
                User users2 = new User();

                for (int i = 0; i < users.size(); i++) {
                    users2 = users.get(i);
                    if (users2.getAccount().equals(s)) {
                        System.out.println("请输入密码");
                        String mima = sc.next();
                        if (mima.equals(users2.getPassword())) {
                            index = i;
//                    找到了开始进行下一步操作
                            System.out.println("             登陆成功\n请输入如下操作\n");
                            Time();
                            while (true) {
                                System.out.println("3:           查询用户信息");
                                System.out.println("4:            账户操作");
                                System.out.println("5:          用户信息更改");
                                System.out.println("6:            返回登录界面");
//                                System.out.println("0:             进入管理员");
                                String choose = sc.next();
                                switch (choose) {
                                    case "3":
                                        Information(users, users2);
                                        break;
                                    case "4":
                                        Change(sc);
                                        String choose2 = sc.next();
                                        switch (choose2) {
                                            case "1":
                                                Addmoney(users, users2, sc);
                                                break;
                                            case "2":
                                                Reducemoney(users, users2, sc);
                                                break;
                                            case "3":
                                                Exchange(users, users2, sc);
                                                break;
                                        }
                                        break;
                                    case "5":
                                        Changeinformation(users, users2, sc);
                                        break;
                                    case "6":
//                                System.exit(0);
                                        return;
                                 /*   case "0":
                                        System.out.println("----先跳过这个待完善功能:管理员登录---");
                                        System.out.println("");
                                        if (users.size() != 0) {
                                            Showall(users);
                                        } else {
                                            System.out.println("系统内无数据");
                                        }
                                        break;*/

                                    default:
                                        System.out.println("您输入的有误请重新输入");

                                }
                            }
                        } else {
                            System.out.println("您输入的密码有误,请重新输入");
                            break;

                        }


                    }

                }
                System.out.println("系统内没有符合的账号,请重新输入");

            }
        } else {
            System.out.println("系统内没有账户,请先注册后再登录");
        }

        /*System.out.println("请输入用户名");
        String ID_s = sc.nextLine();

        System.out.println("请输入用户密码");
        String Password_s = sc.nextLine();*/

    }

    private static void Exchange(ArrayList users, User users2, Scanner sc) {
        System.out.println("=====转账界面=====");
        System.out.println("您目前的账户信息:");
        Information(users, users2);
        System.out.println("请输入您要转帐的卡号");
        String s = sc.next();


        System.out.println("请输入要转账的账户名");
        String sname = sc.next();
        int index = -1;
        for (int i = 0; i < users.size(); i++) {
            boolean flage = ((users.get(i).getID()).equals(s));
            boolean flag2 = (users.get(i).getAccount()).equals(sname);
            if (flage && flag2) {
                index = i;
                System.out.println("请输入您要转帐的金额");
                double exchangdeyuan = sc.nextDouble();
                if (users2.getCexIOBalance() > 0) {
                    int n = 3;
                    while (n > 0) {
                        System.out.println("请输入您的密码");
                        String certainpasswo = sc.next();

                        if (certainpasswo.equals(users2.getPassword())) {
                            System.out.println("密码正确开始转账");
                            users2.setCexIOBalance(users2.getCexIOBalance() - exchangdeyuan);
//            记住取款额度的限制
                            users2.setGetWithdrawals(users2.getGetWithdrawals() - exchangdeyuan);

                            System.out.println("====您的账户目前的信息======");
                            Information(users, users2);
//            对方的账户数字也会相应的改变
                            users.get(index).setCexIOBalance(users.get(index).getCexIOBalance() + exchangdeyuan);
                            break;
                        } else {
                            System.out.println("密码错误,请重新输入");
                            /*增加一个验证,还有输入超过三次就会锁定账号*/
                            --n;
                        }
                        System.out.println("您的账号已被锁定无法转账,请24H后再转");

                    }
                } else {
                    System.out.println("无法转账,因为您的账户内不够");
                }
            }

        }


    }

    //注册功能
    public static void register(ArrayList users, User users1, Scanner sc) {
        System.out.println("=========欢迎进入注册界面===========");

        System.out.println("请输入用户名");
        String username = sc.next();
        users1.setAccount(username);
        while (true) {
            System.out.println("请输入用户密码(大小写字母+数字+其他符号)");
            String password = sc.next();
            System.out.println("请再输一次密码");
            String password2 = sc.next();
            if (password.equals(password2)) {
                if (CheckM(password)) {
                    users1.setPassword(password);
                    System.err.println("密码设置成功");

                    IDrandom(users, users1);
/*
//       系统随机分配卡号ID
                    System.out.println("您的卡号为:" + IDrandom(users) + "\n请您记好");
*/

                    break;
                } else {
                    System.out.println("两次输入的密码不一致,请重新输入");
                }
            } else {
                System.out.println("您输入的密码格式有误请重新输入(大小写字母+数字+其他符号)");
            }

        }


    }

    //检查输入的用户是否正确
    public static boolean CheckM(String s) {
        boolean flag = true;
        while (flag) {

            int len = s.length();
            int Big = 0, Small = 0, dig = 0, other = 0;

            int[] arr = new int[len];
            for (int i = 0; i < len; i++) {
                arr[i] = s.charAt(i);

            }

            for (int i = 0; i < arr.length; i++) {
              /*  if (arr[i] <= 'A' && arr[i] >= 'Z') {
                    break;*/
                if (arr[i] >= 'A' && arr[i] <= 'Z') {
                    Big++;
                } else if (arr[i] >= 'a' && arr[i] <= 'z') {
                    Small++;
                } else if (arr[i] >= '0' && arr[i] <= '9') {
                    dig++;
                } else {
                    other++;
                }

            }
            if (Big * Small * dig * other != 0) {
                flag = false;
            }


        }

        return true;
    }

    //        随机分配8位卡号ID
    public static void IDrandom(ArrayList users, User users1) {
        String sb = "";

        for (int i = 0; i < 8; i++) {
            Random r = new Random();
            int line = r.nextInt(10);
            sb += line;

        }
//        到这里已经添加了ID到系统内
        users1.setID(sb);
        users.add(users1);
        System.out.println("请记好您的银行卡号");
        System.out.println(users1.getID());

    }

    //    单独用户信息显示
    public static void Information(ArrayList users, User users1) {

        System.out.println("账户名\t账户卡号\t  账户余额\t账户剩余取款额度");


        System.out.println(users1.getAccount() + "   \t"
                + users1.getID() + "\t"
                + users1.getCexIOBalance() + "\t   "
                + users1.getGetWithdrawals());
    }

    //所有账户的信息
    public static void Showall(ArrayList users) {
        System.out.println("账户名\t账户卡号\t  账户余额\t账户剩余取款额度");
        for (int i = 0; i < users.size(); i++) {

            System.out.println(users.get(i).getAccount() + "   \t"
                    + users.get(i).getID() + "\t"
                    + users.get(i).getCexIOBalance() + "\t  "
                    + users.get(i).getGetWithdrawals());
        }
    }

    //             账户操作
    public static void Change(Scanner sc) {
        System.out.println("请选择您要存钱还是取钱");
        System.out.println("1:存钱");
        System.out.println("2:取钱");
        System.out.println("3:转账");

    }

    //    存款
    public static void Addmoney(ArrayList users, User users1, Scanner sc) {
        System.out.println("请您输入需要存款的金额¥");
        double addmoney = sc.nextDouble();
//增加账户的余额
        users1.setCexIOBalance(users1.getCexIOBalance() + addmoney);


    }

    //    取钱
    public static void Reducemoney(ArrayList users, User users1, Scanner sc) {
        System.out.println("请您输入需要取走的金额¥");
        double addmoney = sc.nextDouble();
        if (users1.getCexIOBalance() > 0) {

//增加账户的余额
            users1.setCexIOBalance(users1.getCexIOBalance() - addmoney);
//        取款额度要改变
            users1.setGetWithdrawals(users1.getGetWithdrawals() - addmoney);
        } else {
            System.out.println("您的账户很干净,请努力赚钱\n返回上一步");
            return;
        }
    }

    //    用户信息更改
    public static void Changeinformation(ArrayList users, User users1, Scanner sc) {
        if (users.size() == 0) {
            System.out.println("系统内没有该用户信息");
            return;
        }
        System.out.println("请输入账户名的更改名");
        String rename = sc.next();
        users1.setAccount(rename);
        System.out.println("请输入新的密码");
        String repassword = sc.next();
        users1.setPassword(repassword);
        System.err.println("信息更改成功");

    }

    //    显示目前系统实时时间
    public static void Time() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("当前时间:yyyy年MM月dd日 HH:mm:ss");
        String s = sdf.format(d);
        System.out.println(s);
    }

    //    管理员登录
    public static void sign_system(ArrayList systemers, ArrayList users, Scanner sc) {



            System.out.println("请输入管理员账号");
            String s = sc.next();
            System.out.println("请输入管理员密码");
            String p = sc.next();

                boolean flagzhanghu = "adminster".equals(s);
                boolean flagmima = "14520".equals(p);
                if (flagzhanghu && flagmima) {
                    System.out.println("登陆成功");
                    System.out.println("请输入");
                    while (true) {
                    System.out.println("1:查看所有用户信息");
                    System.out.println("2:更改用户信息");
                    System.out.println("0:返回系统初界面");
                    String choose = sc.next();
                    switch (choose) {
                        case "1":
                            if (users.size() != 0) {
                                Showall(users);
                                break;
                            } else {
                                System.out.println("系统内无数据");
                              break;
                            }

                        case "2":
                            finalchange(users, sc);
                            break;
                        case "0":
                            return;
                        default:
                            System.out.println("功能还在开发,请尽情期待");
                            break;
                    }

                }

                }

                else {
                    System.out.println("您输入的账户或密码有误,请重新输入");
        }

    }

    //    最高权限的更改
    public static void finalchange(ArrayList users, Scanner sc) {
        System.out.println("请输入您要更改的账户");
        String names = sc.next();
        if (users.size()>0) {
            for (int i = 0; i < users.size(); i++) {

                if (names.equals(users.get(i).getAccount())) {
                    User user_s = users.get(i);
                    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:返回上一步");
                    String c = sc.next();
                    switch (c) {
                        case "1":
                            System.out.println("请输入账户名的更改名");
                            String rename = sc.next();
                            user_s.setAccount(rename);
                            System.err.println("信息更改成功");
                            break;
                        case "2":
                            System.out.println("请输入新的密码");
                            String repassword = sc.next();
                            user_s.setPassword(repassword);
                            System.err.println("信息更改成功");
                            break;
                        case "3":
                            System.out.println("请输入更改后的余额");
                            double cmoney = sc.nextDouble();
                            user_s.setCexIOBalance(cmoney);
                            System.err.println("信息更改成功");
                            break;
                        case "4":
                            System.out.println("请输入调整后的额度");
                            double cgetwithdraws = sc.nextDouble();
                            user_s.setGetWithdrawals(cgetwithdraws);
                            System.err.println("信息更改成功");
                            break;
                        case "5":

                            System.out.println("您当前修改账户的卡号为");
                            System.out.println(user_s.getID());

                            while (true) {
                                System.out.println("请您输入修改后的卡号");
                                String newID = sc.next();
//                        int index_S = -1;
                                for (int i1 = 0; i1 < users.size(); i1++) {
                                    if (users.get(i1).getID().indexOf(newID) != i1) {
                                        System.out.println("与其他账号卡号有冲突");
                                        break;
                                    }
                                }
                                user_s.setID(newID);
                                System.err.println("修改成功");
                                break;
                            }
                            break;
                        case "6":
                            return;
                        default:
                            System.out.println("功能还未开发,请耐心等待");

                    }

                }
            }
        }
        else {
            System.out.println("系统内没有用户信息");

        }
    }
}


银行用户类封装

package ATMSystem;

public class User {
// 账户名
private String account;
// 卡号
private String ID;
// 密码
private String Password;
// 账户余额
private double CexIOBalance=0;
// 取款额度
private double getWithdrawals=500000;

public User(){

}

public User(String account, String ID, String password, double cexIOBalance, double getWithdrawals) {
    this.account = account;
    this.ID = ID;
    Password = password;
    CexIOBalance = cexIOBalance;
    this.getWithdrawals = getWithdrawals;
}

public String getAccount() {
    return account;
}

public void setAccount(String account) {
    this.account = account;
}

public String getID() {
    return ID;
}

public void setID(String ID) {
    this.ID = ID;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    Password = password;
}

public double getCexIOBalance() {
    return CexIOBalance;
}

public void setCexIOBalance(double cexIOBalance) {
    CexIOBalance = cexIOBalance;
}

public double getGetWithdrawals() {
    return getWithdrawals;
}

public void setGetWithdrawals(double getWithdrawals) {
    this.getWithdrawals = getWithdrawals;
}

}
管理员封装
如果觉得可以增加管理员人数,可以直接增加数据

package ATMSystem;

public class Systemer {
    private String name="adminster";
    private String passwords="14520";

    public Systemer() {

    }

    public Systemer(String name, String passwords) {
        this.name = name;
        this.passwords = passwords;
    }

    public String getName() {
        return name;
    }

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

    public String getPasswords() {
        return passwords;
    }

    public void setPasswords(String passwords) {
        this.passwords = passwords;
    }



}

代码写的不是特别好,仅供参考

你可能感兴趣的:(java,开发语言,后端)