登录与注册模块设计

该博客介绍了如何设计和实现一个用户登录与注册的模块。首先,定义了一个`User`类,包含用户的基本信息和账户信息。接着,设计了注册功能,检查用户输入的UID是否存在重复,并将新用户信息存储到文件。最后,实现了登录功能,根据用户输入的UID和密码验证用户身份,成功后返回不同的用户界面。整个过程涉及文件操作、数据验证和用户账户管理。
摘要由CSDN通过智能技术生成

实现登录与设计功能的模块设计

分析:

1.首先封装一个对象,用来封装用户的信息,要设计两种构造器来分开用户和管理员的信息。

2.设计注册功能,实现把注册的用户存入文件内,要注意注册的用户是否与已注册的用户存在UID重复的问题。

3.设计登录功能,登录要实现登录成功后实现用户界面和管理员界面。

功能实现:

封装对象:

public class User implements Serializable {
    //成员变量
    private String UID;//用户编号
    private int password;//密码
    private String name;//姓名
    private int age;//年龄
    private String gender;//性别
    private String telNumber;//电话
    private String schoolName;//院系
    private String date;//注册日期
    private UserAccount userAccount;//普通账户信息
    //构造方法
    //管理账户注册
    public User(String UID, int password, String name, int age, String gender, String telNumber, String schoolName, String date) {
        this.UID = UID;
        this.password = password;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.telNumber = telNumber;
        this.schoolName = schoolName;
        this.date = date;
    }
    //普通用户注册
    public User(String UID, int password, String name, int age, String gender, String telNumber, String schoolName, String date, UserAccount userAccount) {
        this.UID = UID;
        this.password = password;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.telNumber = telNumber;
        this.schoolName = schoolName;
        this.date = date;
        this.userAccount = userAccount;
    }
    //get/set方法
    public String getUID() {
        return UID;
    }

    public void setUID(String UID) {
        this.UID = UID;
    }

    public int getPassword() {
        return password;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getTelNumber() {
        return telNumber;
    }

    public void setTelNumber(String telNumber) {
        this.telNumber = telNumber;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public UserAccount getUserAccount() {
        return userAccount;
    }

    public void setUserAccount(UserAccount userAccount) {
        this.userAccount = userAccount;
    }

    @Override
    public String toString() {
        return "User{" +
                "UID='" + UID + '\'' +
                ", password=" + password +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", telNumber='" + telNumber + '\'' +
                ", schoolName='" + schoolName + '\'' +
                ", date='" + date + '\'' +
                ", userAccount=" + userAccount +
                '}';
    }
}
public class UserAccount implements Serializable {
    //成员变量
    private String ACID;//账户编号
    private int credit_rating;//信用等级
    private int balance;//余额
    //构造器
    public UserAccount(String ACID, int credit_rating, int balance) {
        this.ACID = ACID;
        this.credit_rating = credit_rating;
        this.balance = balance;
    }

    public String getACID() {
        return ACID;
    }

    public void setACID(String ACID) {
        this.ACID = ACID;
    }

    public int getCredit_rating() {
        return credit_rating;
    }

    public void setCredit_rating(int credit_rating) {
        this.credit_rating = credit_rating;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }
}

注册功能:

//注册功能
    public void register() throws IOException, ClassNotFoundException {
        //获取已经存在的用户
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("Job\\src\\SaveFile\\UserList.txt"));
        ArrayList<User> arr = (ArrayList<User>) in.readObject();
        in.close();//释放登入资源
        //旗帜
        label:
        while (true) {
            System.out.println("请输入你的学号/教师编号:");//输入学号或者教师编号
            System.out.println("请按以下格式书写(不得超过10位):");
            System.out.println("学号:student+xxx");
            System.out.println("教师编号:teacher+xxx");
            System.out.print("请输入:");
            String UID = TSUtility.readKeyBoard(10, false);
            for (User u : arr) {
                if (u.getUID().equals(UID)) {
                    System.out.println("你输入的学号或者教师编号已经存在");
                    System.out.println();
                    continue label;//返回从新输出学号或者教师编号
                }
            }
            StringBuilder U = new StringBuilder();//输入的学号的前半段
            StringBuilder A = new StringBuilder();//输入学号的后半段
            boolean isFlag = false;//做旗帜
            boolean isStudent = false;//普通用户旗帜
            boolean isTeacher = false;//管理用户旗帜
            //正则表达式
            String regex = "\\w{10}";
            Pattern pat = Pattern.compile(regex);
            Matcher mat = pat.matcher(UID);
            if (mat.matches()) {//使用正则表达式
                //记录学号前半段
                for (int x = 0; x < 7; x++) {
                    U = U.append(UID.charAt(x));
                }
                //记录学号后半段
                for (int x = 7; x < 10; x++) {
                    A = A.append(UID.charAt(x));
                }
            }
            //把UID和ACID转为String
            String num = U.toString();
            String ACID = A.toString();
            //判断输入的学号或者教师编号是否符合格式
            if (num.equals("student")) {//判断是普通用户
                isFlag = true;
                isStudent = true;//开启学生旗帜
            } else if (num.equals("teacher")) {//判断是管理用户
                isFlag = true;
                isTeacher = true;//开启老师旗帜
            } else {
                System.out.println("你输入的学号/教师编号不对");
                System.out.println();
                continue label;//返回从新输入学号或者教师编号
            }
            if (isFlag) {
                System.out.print("请输入密码(密码不超过6位):");//密码
                int password = TSUtility.readstock();
                System.out.print("请输入你的名字:");//姓名
                String name = TSUtility.readKeyBoard(4, false);
                System.out.print("请输入你的年龄:");//年龄
                int age = TSUtility.readstock();
                System.out.print("请输入你的性别:");//性别
                String gender = TSUtility.readKeyBoard(2, false);
                System.out.print("请输入你的电话:");//电话
                String telNumber = TSUtility.readKeyBoard(11, false);
                System.out.print("请输入你的院系:");//院系
                String schoolName = TSUtility.readKeyBoard(20, false);
                SimpleDateFormat sim = new SimpleDateFormat("yyyy--MM--dd");//使用SimpleDateFormat,同时给定个格式
                Date d = new Date();//创建一个Date对象,该Date对象是创建时的事件
                String date = sim.format(d);//记录存入的日期
                if (isStudent) {//如果是普通用户
                    //创建普通用户
                    User commonUser = new User(UID, password, name, age, gender, telNumber, schoolName, date, new UserAccount(ACID, 6, 0));
                    arr.add(commonUser);//添加到集合中
                    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Job\\src\\SaveFile\\UserList.txt"));
                    out.writeObject(arr);//把集合写入文件
                    out.close();//释放资源
                    System.out.println("注册成功");
                    return;
                }
                if (isTeacher) {//如果是管理用户
                    //创建管理用户
                    User administrator = new User(UID, password, name, age, gender, telNumber, schoolName, date);
                    arr.add(administrator);//添加到集合中
                    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Job\\src\\SaveFile\\UserList.txt"));
                    out.writeObject(arr);//把集合写入文件
                    out.close();//释放资源
                    System.out.println("注册成功");
                    return;
                }
            }
        }
    }

登录功能:

//登录功能
    public ActiveUser landing() throws IOException, ClassNotFoundException {
        //获取已经存在的用户
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("Job\\src\\SaveFile\\UserList.txt"));
        ArrayList<User> arr = (ArrayList<User>) in.readObject();
        in.close();//释放登入资源
        System.out.print("请输入学号或者教师编号:");
        String UID = TSUtility.readKeyBoard(10, false);
        System.out.print("请输入密码:");
        int password = TSUtility.readstock();
        boolean isTrue = false;//判断是否与文件中的账户密码匹配
        for (User u : arr) {//遍历从文件中取到的账户信息
            if (u.getUID().equals(UID)) {//账号正确
                if (u.getPassword() == password) {//密码正确
                    isTrue = true;  //旗帜变为true
                }
            }
        }
        if (isTrue) {//匹配
            StringBuilder A = new StringBuilder();//学号或者教师编号的格式
            for (int x = 0; x < 7; x++) {
                A = A.append(UID.charAt(x));//拼接得到对应的格式
            }
            String name = A.toString();//转为String
            if (name.equals("student")) {//如果是普通用户
                //读入用户列表数据
                ObjectInputStream loginIn = new ObjectInputStream(new FileInputStream("Job\\src\\SaveFile\\UserList.txt"));
                ArrayList<User> arr1 = (ArrayList<User>) loginIn.readObject();//存放用户列表
                in.close();//释放资源
                int index = 0;对应用户列表中的索引
                int credit_rating = 0;//信用系数
                String credit = "";//信用等级
                for (int x = 0; x < arr1.size(); x++) {
                    if (arr1.get(x).getUID().equals(UID)) {//找到对应的用户
                        index = x;//记录对应用户列表中的索引
                        credit_rating = arr1.get(x).getUserAccount().getCredit_rating();//对应用户列表中当前用户的信用系数
                        if (credit_rating >= 0 && credit_rating < 3) {//极差系数
                            credit = "极差";
                        } else if (credit_rating >= 3 && credit_rating < 6) {//较差系数
                            credit = "较差";
                        } else if (credit_rating >= 6 && credit_rating < 9) {//良好系数
                            credit = "良好";
                        } else if (credit_rating >= 9 && credit_rating < 12) {//优秀系数
                            credit = "优秀";
                        } else {//极优系数
                            credit = "极优";
                        }
                        break;
                    }
                }
                ActiveUser activeUser = new ActiveUser(arr1.get(index),index,credit);
                return activeUser;
            } else {//如果是管理员
                //读入用户列表数据
                ObjectInputStream loginIn = new ObjectInputStream(new FileInputStream("Job\\src\\SaveFile\\UserList.txt"));
                ArrayList<User> arr1 = (ArrayList<User>) loginIn.readObject();//存放用户列表
                in.close();//释放资源
                int index = 0;//对应数组索引
                for (int x = 0; x < arr1.size(); x++) {//找到对应的管理员用户
                    if (arr1.get(x).getUID().equals(UID)) {
                        index = x;//记录对应用户列表中的索引
                    }
                }
                ActiveUser activeUser = new ActiveUser(arr1.get(index),index);
                return activeUser;
            }
        } else {//不匹配
            return null;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值