面向对象实现ATM自动取款机系统,ATmSystem --java

主要分为俩个对象,用户类和ATMSystem类,功能如下


  • 登录
  • 注册
  • 存钱
  • 取钱
  • 转账
  • 修改密码
  • 查询个人信息
  • 修改限额
  • 注销账户

     

直接上代码——

main方法:

package com.zeyu.ATM;

public class Test {
    public static void main(String[] args) {
        ATMSystem atm = new ATMSystem();
        atm.Start();
    }
}

 User类:
 

package com.zeyu.ATM;

import java.text.SimpleDateFormat;
import java.util.Date;

public class User {
    private String card_number; //卡号
    private String password;    //密码
    private String name;    //姓名
    private String gender;  //性别
    private String security_question;   //密保问题
    private String security_answer; //密保答案
    private long quota; //限额
    private long deposit;   //存款

//重写toString
    @Override
    public String toString() {
        Date data = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(" yyyy年MM月dd日 HH:mm:ss");
        String time = sdf.format(data);
        return this.getName().charAt(0) + (this.getGender().equals("男") ? "先生~" : "女士~") + " 现在是" + time + " o(* ̄▽ ̄*)ブ";
    }
//set 与 get方法
    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public boolean setGender(String gender) {
        if (gender.equals("男") || gender.equals("女")) {
            this.gender = gender;
            return true;
        }
        return false;
    }

    public String getSecurity_question() {
        return security_question;
    }

    public void setSecurity_question(String security_question) {
        this.security_question = security_question;
    }

    public String getSecurity_answer() {
        return security_answer;
    }

    public void setSecurity_answer(String security_answer) {
        this.security_answer = security_answer;
    }

    public String getCard_number() {
        return card_number;
    }

    public void setCard_number(String card_number) {
        this.card_number = card_number;
    }

    public String getPassword() {
        return password;
    }

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

    public long getQuota() {
        return quota;
    }

    public boolean setQuota(long quota) {
        if(quota <= 0 ){
            return false;
        }
        this.quota = quota;
        return true;
    }

    public long getDeposit() {
        return deposit;
    }

    public void setDeposit(long deposit) {
        this.deposit = deposit;
    }

//构造函数
    public User() {
    }
}


ATMSystem类:

package com.zeyu.ATM;

import java.text.SimpleDateFormat;
import java.util.*;

public class ATMSystem {
    private ArrayList userlist = new ArrayList<>();   //用户列表

//登录界面————————————————————————————————————————————————————————————————————————————————
    private String Login_InterFace(){   //登录系统界面
        System.out.println("\n欢迎使用ATMSystem");
        System.out.println("——————————————————————————————————————————————————");
        System.out.println("1、登录");
        System.out.println("2、注册");
        System.out.println("0、退出");
        System.out.print("请输入您的选择:");
        Scanner sc = new Scanner(System.in);
        return sc.next();   //返回选择
    }

    public void Start(){    //登录系统
        while (true) {
            String select = Login_InterFace();
            switch (select) {
                case "1" -> Login();
                case "2" -> register();
                case "0" -> {
                    System.out.println("感谢使用ATMSystem,祝您生活愉快~");
                    return;
                }
                default -> System.out.println("请输入正确的选择~");
            }
        }
    }
//登录界面结束—————————————————————————————————————————————————————————————————————————————

//登录部分————————————————————————————————————————————————————————————————————————————————
    private void Login(){   //登录操作
        if(this.userlist.size() == 0){
            System.out.println("当前系统中暂无用户,请先注册哦亲~");
            return;
        }
        System.out.println("\n登陆界面");
        System.out.println("——————————————————————————————————————————————————");
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入您的卡号:");
        String card_number = sc.next();

        for (User user : this.userlist) {   //从用户列表寻找有没有该该卡号用户
                if(user.getCard_number().equals(card_number)){  //对比卡号
                    while (true) {
                        System.out.print("清输入密码:");
                            if (user.getPassword().equals(sc.next())) { //对比密码
                                if (Main_System(user) == 1) {   //密码正确进入主界面,同时,若主界面返回1代表要销号
                                    this.userlist.remove(user);
                                }
                                return;
                            } else {    //否则进入密码错误界面
                                String select = Password_Error();
                                switch (select) {
                                    case "2":
                                        Retrieve_Password(user);
                                        break;
                                    case "0":
                                        return;
                                    default:
                                        System.out.println("请输入正确的选择~");
                                    case "1":
                                        break;
                            }
                        }
                    }
                }
                System.out.println("\n登陆界面");
                System.out.println("——————————————————————————————————————————————————");
        }
        System.out.println("该卡号不存在哦亲~");
    }

    private String Password_Error(){    //密码错误界面
        System.out.println("\n密码错误!");
        System.out.println("——————————————————————————————————————————————————");
        System.out.println("1、重新输入");
        System.out.println("2、找回密码");
        System.out.println("0、退出");
        System.out.print("请输入您的选择:");
        Scanner sc = new Scanner(System.in);
        return sc.next();
    }

    private void Retrieve_Password(User user) { //密码找回,询问基本信息,如果有设置密保则问密保
        Scanner sc = new Scanner(System.in);
        System.out.println("\n密码找回");
        System.out.println("——————————————————————————————————————————————————");
        System.out.println("随时可输入0退出哦~");

        System.out.print("请输入你的姓名:");
        String name = sc.next();
        if(name.equals("0")) return;
        if(!user.getName().equals(name)){
            System.out.println("您的输入有误哦~(*^_^*)~");
            return;
        }

        System.out.print("请输入您的性别:");
        String gender = sc.next();
        if(gender.equals("0")) return;
        if(!user.getGender().equals(gender)){
            System.out.println("您的输入有误哦~(*^_^*)~");
            return;
        }

        if(user.getSecurity_question() != null) {
            System.out.println(user.getSecurity_question());
            System.out.println("请回答密保问题哦亲~:");
            String answer = sc.next();
            if(answer.equals("0")) return;
            if (!user.getSecurity_answer().equals(answer)) {
                System.out.println("您的输入有误哦~(*^_^*)~");
                return;
            }
        }

        System.out.print("恭喜您通过验证,");

        while (true) {
            System.out.print("请输入您的新密码:");
            String password = sc.next();
            if(password.equals("0")) return;
            System.out.print("请确认密码:");
            if(password.equals(sc.next())){
                System.out.println("恭喜您密码找回成功!");
                user.setPassword(password);
                return;
            }else{
                System.out.println("您的输入有误哦~(*^_^*)~");
            }
        }

    }
//登录部分结束——————————————————————————————————————————————————————————————————————————————

//注册部分—————————————————————————————————————————————————————————————————————————————————
    private void register(){    //用户注册,输入基本信息,密保可选是否输入
        System.out.println("\n用户注册");
        System.out.println("——————————————————————————————————————————————————");
        System.out.println("随时可输入0退出哦~");
        Scanner sc = new Scanner(System.in);
        User user = new User();

        System.out.print("请输入您的姓名:");
        String name = sc.next();
        if(name.equals("0")) return;
        user.setName(name);

        while(true) {
            System.out.print("请输入您的性别:");
            String gender = sc.next();
            if(gender.equals("0")) return;
            if(user.setGender(gender)) break;
            System.out.println("请输入正确的性别哦亲~");
        }

        while (true) {
            System.out.print("请输入您的密码:");
            String password = sc.next();
            if(password.equals("0")) return;
            System.out.print("请确认密码:");
            if(password.equals(sc.next())){
                user.setPassword(password);
                break;
            }else{
                System.out.println("您的输入有误哦~(*^_^*)~");
            }
        }


        while(true){
            System.out.print("请输入您的限额:");
            long quota = sc.nextInt();
            if(quota == 0) return;
            if(user.setQuota(quota)){
                break;
            }else {
                System.out.println("限额必须大于等于0哦亲~");
            }
        }

        System.out.print("请输入您的密保问题(输入0可跳过):");
        String security_question = sc.next();
        if(!security_question.equals("0")){
            user.setSecurity_question(security_question);
            System.out.print("请输入您的密保答案:");
            user.setSecurity_answer(sc.next());
        }

        user.setCard_number(Card_Number_Random());

        System.out.println("恭喜您注册成功!"+ user);
        System.out.println("您的卡号为:" + user.getCard_number());   //用卡号生成方法随机生成卡号
        this.userlist.add(user);
    }

    private String Card_Number_Random() {   //卡号随机生成方法
        Random r = new Random();
        String numbers = "1234567890";  //数字池
        int digit = r.nextInt(6,13);    //卡号位数随机
        StringBuilder card_number = new StringBuilder();

        while(digit-- != 0){
            if(card_number.toString().equals("")) { //卡号第一位不为0
                card_number.append(numbers.charAt(r.nextInt(0, 9)));
            }else {     //从数字池随机抽数字组成卡号
                card_number.append(numbers.charAt(r.nextInt(0, 10)));
            }
        }

        return card_number.toString();
    }
//注册部分结束——————————————————————————————————————————————————————————————————————————————

//主界面部分————————————————————————————————————————————————————————————————————————————————
    private int Main_System(User user){ //主系统
        Scanner sc = new Scanner(System.in);
        while (true) {
            String select = Main_InterFace(user);
            switch (select) {
                case "1" -> Save_Money(user);
                case "2" -> Draw_Money(user);
                case "3" -> Transfer_accounts(user);
                case "4" -> Change_Password(user);
                case "5" -> Search_Information(user);
                case "6" -> Change_Quota(user);
                case "7" -> {
                    System.out.print("您确认要注销账户吗?(y/n):");
                    if(sc.next().equals("y")) {
                        if(user.getDeposit() == 0){ //余额大于零无法销号
                            return 1;
                        }
                        System.out.println("注销失败!您账户上还有余额" + user.getDeposit() + "元,请取出余额再注销~");
                    }
                }
                case "0" -> {
                    System.out.println("退出成功~");
                    return 0;
                }
                default -> {
                    System.out.println("请输入正确的选择哦~");
                }
            }
        }

    }

    private String Main_InterFace(User user){   //主系统界面
        System.out.println("\n欢迎您!" + user);
        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("0、退出");
        System.out.print("请输入您的选择:");
        Scanner sc = new Scanner(System.in);
        return sc.next();
    }

    private void Save_Money(User user){ //存钱
        System.out.println("\n欢迎您!" + user);
        System.out.println("——————————————————————————————————————————————————");
        System.out.println("可随时输入0退出哦~");
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.print("请输入你要存入的金额:");
            long money = sc.nextInt();

            if(money == 0) return;

            if(money < 0) {
                System.out.println("请输入正确的金额哦~");
                continue;
            }

            user.setDeposit(user.getDeposit() + money);
            System.out.println("存钱成功,现在账户上有 " + user.getDeposit() + "元~");
            return;
        }
    }

    private void Draw_Money(User user){ //取钱
        System.out.println("\n欢迎您!" + user);
        System.out.println("——————————————————————————————————————————————————");
        System.out.println("可随时输入0退出哦~");
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.print("请输入您要取出的金额:");
            long money = sc.nextInt();

            if(money == 0) return;

            if(money < 0) {
                System.out.println("请输入正确的金额哦~");
                continue;
            }

            if(money > user.getQuota()){
                System.out.println("取出金额超出用户余额,请重新输入~");
                continue;
            }

            user.setDeposit(user.getDeposit() - money);
            System.out.println("存钱成功,现在账户上有 " + user.getDeposit() + "元~");
            return;
        }
    }

    private void Transfer_accounts(User user){  //转账
        System.out.println("\n欢迎您!" + user);
        System.out.println("——————————————————————————————————————————————————");
        System.out.println("可随时输入0退出哦~");
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入对方卡号:");
        String card_number = sc.next();
        if(card_number.equals("0")) return;
        for (User user1 : this.userlist) {  //从用户列表找对方卡号
            if(!user1.getCard_number().equals(card_number)){    //没找到找下一个
                continue;
            }
            while (true) {  //找到才执行
                System.out.print("请输入转账金额:");
                long money = sc.nextInt();

                if(money == 0) return;

                if(money < 0){
                    System.out.println("请输入正确的转账金额哦~");
                    continue;
                }

                if (money > user.getDeposit()){
                    System.out.println("转账金额超过账户余额,请重新输入~");
                    continue;
                }

                System.out.println("转账成功~");
                user.setDeposit(user.getDeposit() - money);
                user1.setDeposit(user1.getDeposit() + money);
                return;
            }
        }
        System.out.println("没有找到该卡号用户哦~");
    }

    private void Change_Password(User user){    //修改密码
        System.out.println("\n欢迎您!" + user);
        System.out.println("——————————————————————————————————————————————————");
        System.out.println("可随时输入0退出哦~");
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.print("请输入您当前密码:");
            String password = sc.next();
            if(password.equals("0")) return;
            if(!user.getPassword().equals(password)){
                System.out.println("您的输入有误哦~(*^_^*)~");
                continue;
            }

            if(user.getSecurity_question() != null){    //如果有就问
                System.out.println(user.getSecurity_question());
                System.out.print("请回答您的密保问题:");
                String answer = sc.next();
                if(answer.equals("0")) return;
                if(!user.getSecurity_answer().equals(answer)){
                    System.out.println("您的回答有误哦~(*^_^*)~");
                    continue;
                }
            }

            System.out.print("请输入您的新密码:");
            String new_password = sc.next();
            if(new_password.equals("0")) return;
            System.out.println("修改成功~");
            user.setPassword(new_password);
            return;
        }
    }

    private void Search_Information(User user){ //显示个人信息
        System.out.println("\n——————————————————————————————————————————————————");
        System.out.println("卡号:" + user.getCard_number());
        System.out.println("姓名:" + user.getName());
        System.out.println("性别:" + user.getGender());
        System.out.println("存款:" + user.getDeposit());
        System.out.println("限额:" + user.getQuota());
    }

    private void Change_Quota(User user){   //修改限额
        System.out.println("\n欢迎您!" + user);
        System.out.println("——————————————————————————————————————————————————");
        System.out.println("可随时输入0退出哦~");
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.print("请输入新的限额:");
            long Quota = sc.nextInt();

            if(Quota == 0) return;

            if(Quota < 0){
                System.out.println("请输入正确的限额~");
                continue;
            }

            System.out.println("修改成功~");
            user.setQuota(Quota);
            return;
        }
    }
//主界面部分结束————————————————————————————————————————————————————————————————————————————————

//构造函数与get、set方法————————————————————————————————————————————————————————————————————————
    public ArrayList getUserlist() {
        return userlist;
    }

    public void setUserlist(ArrayList userlist) {
        this.userlist = userlist;
    }

    public ATMSystem() {
    }

    public ATMSystem(ArrayList userlist) {
        this.userlist = userlist;
    }
}

运行截图:

面向对象实现ATM自动取款机系统,ATmSystem --java_第1张图片 

你可能感兴趣的:(学习笔记,java,开发语言,学习,笔记)