首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >基于HashSet集合完成的微博用户注册的案例

基于HashSet集合完成的微博用户注册的案例

作者头像
杨校
发布2021-11-08 10:42:26
发布2021-11-08 10:42:26
5280
举报
文章被收录于专栏:Java技术分享圈Java技术分享圈
步骤
1. 设计 用户的实体类
代码语言:javascript
复制
import java.util.Date;
import java.util.Objects;

public class User {
    // 用户名
    private String username;
    // 密码
    private String password;
    // 生日
    private Date birthday;
    // 手机号
    private String phone;
    // 邮箱
    private String email;

// 基于  IntellJ Idea 软件开发的快捷键:
    // Alt + Insert   Getter  And  Setter
    // Alt + Insert   equals  And  hashCode
    // Alt + Insert   toString


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(username, user.username) &&
                Objects.equals(password, user.password) &&
                Objects.equals(birthday, user.birthday) &&
                Objects.equals(phone, user.phone) &&
                Objects.equals(email, user.email);
    }

    @Override
    public int hashCode() {
        return Objects.hash(username, password, birthday, phone, email);
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
2. 编写 注册用户 的测试类
代码语言:javascript
复制
import java.util.HashSet;
import java.util.Scanner;

public class UserRegister {

    static HashSet hs = new HashSet();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入用户名:");
        String username = sc.nextLine();

        System.out.println("请输入密码:");
        String password = sc.nextLine();

        System.out.println("请重复输入密码:");
        String repassword = sc.nextLine();

        System.out.println("请输入出生日期:");
        String birthday = sc.nextLine();

        System.out.println("请输入手机号码:");
        String phone = sc.nextLine();

        System.out.println("请输入电子邮箱:");
        String email = sc.nextLine();


        CheckInfo ci = new CheckInfo();
        String sb = ci.checkInfo(username, password, repassword, birthday, phone, email);
        System.out.println("注册的结果是:" + sb);
    }
}
3. 编写 用户校验的 工具类
代码语言:javascript
复制
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;

public class CheckInfo {

    HashSet  hashSet = new HashSet();

    StringBuilder sb = new StringBuilder();
    int  status = 1 ; // 1 代表成功  2 代表 失败
    public String checkInfo(String username,String password,String repassword,String birthday,String phone, String email){
        // 生日的判断
        if (birthday.length() != 10){ //  1999-09-09
            sb.append("您填写的生日格式有误,请检查!");
            status = 2 ;
        }
        // 密码的判断
        if (!password.equals(repassword)){
            sb.append("两次密码填写不一样,请检查!");
            status = 2 ;
        }
        // 手机格式和开头号码的判断
        if (phone.length() != 11){ //  1999-09-09
            sb.append("您填写的手机号码格式有误,请检查!");
            status = 2 ;
        }else if (!(phone.startsWith("13") ||phone.startsWith("15") || phone.startsWith("17") || phone.startsWith("18") ) ){ //  1999-09-09
            sb.append("您填写的手机号码不正确,请检查!");
            status = 2 ;
        }
        // 邮箱的判断
        if (!email.contains("@")){
            sb.append("您填写的电子邮箱格式有误,请检查!");
            status = 2 ;
        }

        // 如果以上信息的校验都是无误  则可以将新用户加入到集合内
        if(status == 1){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dateBirthday = null;

            try {
                dateBirthday = sdf.parse(birthday); //  字符串转为 日期类型 有可能出现错误、错误了 则执行catch内的代码 否则 不执行!
            } catch (ParseException e) {
                e.printStackTrace();// Stack  栈      数据结构与算法:  内存分析
            }
            // 创建user 对象  
            User user = new User();

			//  完成对象的内容封装
            user.setUsername(username);
            user.setPassword(repassword);
            user.setBirthday(dateBirthday);
            user.setPhone(phone);
            user.setEmail(email);
        }

        if (status == 1){
            sb.append("注册成功!");
        }
        return  sb.toString();
    }
}
4 运行程序:

在这里插入图片描述


请输入用户名: 张三 请输入密码: 1 请重复输入密码: 1 请输入出生日期: 1999-09-09 请输入手机号码: 14696963322 请输入电子邮箱: 369@qq.com 您填写的手机号码不正确,请检查!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/11/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 步骤
    • 1. 设计 用户的实体类
    • 2. 编写 注册用户 的测试类
    • 3. 编写 用户校验的 工具类
    • 4 运行程序:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档