1. 程式人生 > >java——模擬新浪微博用戶註冊

java——模擬新浪微博用戶註冊

hashset nta 格式不正確 bject object ring tst null append

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("兩次輸入的密碼不一樣~\r\n");
            state = 2;
        }
        //生日判斷
        if(bd.length() != 10){
            result.append("生日格式不正確~\r\n");
            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("生日格式不正確~\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<telNum.length(); 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——模擬新浪微博用戶註冊