java——模拟新浪微博用户注册

Posted 高圈圈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java——模拟新浪微博用户注册相关的知识,希望对你有一定的参考价值。

1.创建用户类,重写HashCode()和equals()方法:

import java.util.*;
public class User{
    private String name;
    private String pwd;
    private Date bd;
    private String telNum;
    private String email;
    //????????
    public User(){
    }
    public User(String name, String pwd, Date bd, 
                    String telNum, String email){
        this.name = name;
        this.pwd = pwd;
        this.bd = bd;
        this.telNum = telNum;
        this.email = email;
    }
    @Override
    public int hashCode(){
        return name.hashCode();
    }
    @Override
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(obj == null){
            return false;
        }
        // getClass() 返回此 Object 的运行时的类。
        if(getClass() != obj.getClass()){
            return false;
        }
            //不这样会报错,obj是Object类,而Object类包括了User类,所以上一步即使相等,还是得强制转换成Object的子类User才行。

        User other = (User) obj;
        if(name == null){
            if(other.name != null){
                return false;
            }
        }else if(!name.equals(other.name)){
            return false;
        }
        return true;
    }
        
}

2.创建用户注册类:

import java.util.*;
public class UserRegister{
    public static HashSet<User> USER_DATA = new HashSet<User>();
    public static void main(String[] args){
        //initData();
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = scan.nextLine();
        System.out.println("请输入密码:");
        String pwd = scan.nextLine();
        System.out.println("请再次输入密码:");
        String repwd = scan.nextLine();
        System.out.println("请输入出生日期:");
        String bd = scan.nextLine();
        System.out.println("请输入手机号:");
        String telNum = scan.nextLine();
        System.out.println("请输入电子邮箱:");
        String email = scan.nextLine();
        CheckInfo checkInfo = new CheckInfo(USER_DATA);
        String result = checkInfo.checkAction(name, pwd, repwd, bd, telNum, email);
        System.out.println("注册结果:" + result);
     }
 //    private static void initData(){
 //        User user1 = new User("张三", "123", "123", new Date(), 
 //        "18812341234", "[email protected]");
 //        User user2 = new User("张六", "126", "126", new Date(), 
 //        "18812341236", "[email protected]");
 //        USER_DATA.add(user1);
 //      USER_DATA.add(user2);
 //    }
}

3.创建校验信息类:

import java.util.*;
import java.util.*;
import java.text.*;
public class CheckInfo{
    public static HashSet<User> USER_DATA = new HashSet<User>();
    public CheckInfo(HashSet<User> USER_DATA){
        this.USER_DATA = USER_DATA;
    }
    public String checkAction(String name, String pwd,
     String repwd, String bd, String telNum, String email){
        StringBuilder result = new StringBuilder();
        int state = 1;
        //密码判断
        if(!pwd.equals(repwd)){
            result.append("两次输入的密码不一样~
");
            state = 2;
        }
        //生日判断
        if(bd.length() != 10){
            result.append("生日格式不正确~
");
            state = 2;
        }else{
            for(int i=0; i<bd.length(); i++){
                Character thisChar = bd.charAt(i);
                if(i == 4 || i == 7){
                    if(thisChar != ‘-‘){
                        result.append("生日格式不正确~
");
                        state = 2;
                    }
                }else{
                    if(!Character.isDigit(thisChar)){
                        result.append("生日格式不正确~
");
                        state = 2;
                    }
                }
            }
        }
        //电话号码判断
        if(telNum.length() != 11){
            result.append("输入手机号不正确~
");
            state = 2;
        }else{
            for(int i=0; i<telNum.length(); i++){
                Character thisChar = telNum.charAt(i);
                if(!Character.isDigit(thisChar)){
                result.append("输入手机号不正确~
");
                state = 2;
            }else if(!(telNum.startsWith("13") || 
                             telNum.startsWith("15") ||
                             telNum.startsWith("18") ||
                             telNum.startsWith("17"))){
                            result.append("输入手机号不正确~
");
                            state = 2;
                }
            }
        }
        //邮箱判断
        if(!email.contains("@")){
            result.append("邮箱不正确~
");
            state = 2;
        }
        if(state == 1){
            //格式化日期返回对象
            DateFormat format = new SimpleDateFormat ("yyyy-mm-dd");
            Date datebd = null;
            try {
                datebd = format.parse(bd);    
            } catch (ParseException e){
                    e.printStackTrace();
            }
            User newUser = new User(name, repwd, datebd, telNum, email);
            if(!USER_DATA.add(newUser)){
                result.append("用户重复!");
                state = 2;
            }
            if(state == 1){
                result.append("注册成功!");
            }
        }
        return result.toString();
    }
}

 

以上是关于java——模拟新浪微博用户注册的主要内容,如果未能解决你的问题,请参考以下文章

java+selenium模拟登陆新浪微博demo

定向爬虫 - Python模拟新浪微博登录

定向爬虫 - Python模拟新浪微博登录

新浪微博模拟登陆+数据抓取(java实现)

Node.js-Koa2框架生态实战-从零模拟新浪微博 完整教程

Node.js-Koa2框架生态实战-从零模拟新浪微博