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

2021-07-04 14:05

阅读:672

标签:register   contain   cep   out   line   注册   stack   rgs   add   

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_DATA = new HashSet();
    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", "1123456@163.com");
 //        User user2 = new User("张六", "126", "126", new Date(), 
 //        "18812341236", "66666666@163.com");
 //        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_DATA = new HashSet();
    public CheckInfo(HashSet 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("两次输入的密码不一样~\r\n");
            state = 2;
        }
        //生日判断
        if(bd.length() != 10){
            result.append("生日格式不正确~\r\n");
            state = 2;
        }else{
            for(int i=0; i){
                Character thisChar = bd.charAt(i);
                if(i == 4 || i == 7){
                    if(thisChar != ‘-‘){
                        result.append("生日格式不正确~\r\n");
                        state = 2;
                    }
                }else{
                    if(!Character.isDigit(thisChar)){
                        result.append("生日格式不正确~\r\n");
                        state = 2;
                    }
                }
            }
        }
        //电话号码判断
        if(telNum.length() != 11){
            result.append("输入手机号不正确~\r\n");
            state = 2;
        }else{
            for(int i=0; i){
                Character thisChar = telNum.charAt(i);
                if(!Character.isDigit(thisChar)){
                result.append("输入手机号不正确~\r\n");
                state = 2;
            }else if(!(telNum.startsWith("13") || 
                             telNum.startsWith("15") ||
                             telNum.startsWith("18") ||
                             telNum.startsWith("17"))){
                            result.append("输入手机号不正确~\r\n");
                            state = 2;
                }
            }
        }
        //邮箱判断
        if(!email.contains("@")){
            result.append("邮箱不正确~\r\n");
            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——模拟新浪微博用户注册

标签:register   contain   cep   out   line   注册   stack   rgs   add   

原文地址:https://www.cnblogs.com/gaoquanquan/p/9613052.html


评论


亲,登录后才可以留言!