java——類比新浪微博使用者註冊

來源:互聯網
上載者:User

標籤: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> 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——類比新浪微博使用者註冊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.