1. Create a user class, overriding the Hashcode () and Equals () methods:
ImportJava.util.*; Public classuser{PrivateString name; PrivateString pwd; PrivateDate BD; PrivateString Telnum; PrivateString Email; //???????? PublicUser () {} PublicUser (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 inthashcode () {returnName.hashcode (); } @Override Public Booleanequals (Object obj) {if( This==obj) { return true; } if(obj = =NULL){ return false; } //GetClass () returns the runtime class of this Object. if(GetClass ()! =Obj.getclass ()) { return false; } //No, this will be an error, obj is the object class, and the object class includes the user class, so the previous step, even if equal, would have to be cast to the subclass of 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. Create a user registration class:
ImportJava.util.*; Public classuserregister{ Public StaticHashset<user> User_data =NewHashset<user>(); Public Static voidMain (string[] args) {//InitData ();Scanner scan =NewScanner (system.in); System.out.println ("Please enter user name:"); String name=Scan.nextline (); System.out.println ("Please enter your password:"); String pwd=Scan.nextline (); System.out.println ("Please enter your password again:"); String repwd=Scan.nextline (); System.out.println ("Please enter your date of birth:"); String BD=Scan.nextline (); System.out.println ("Please enter your phone number:"); String Telnum=Scan.nextline (); System.out.println ("Please enter e-mail address:"); String Email=Scan.nextline (); Checkinfo Checkinfo=NewCheckinfo (User_data); String result=checkinfo.checkaction (name, PWD, REPWD, BD, Telnum, email); System.out.println ("Registration Result:" +result); } //private static void InitData () {//User User1 = new User ("Zhang San", "123", "123", New Date (),//"18812341234", "[email protected]"); //User User2 = new User ("Six", "126", "126", New Date (),//"18812341236", "[email protected]"); //User_data.add (user1);//User_data.add (user2);// }}
3. Create the Checksum information class:
ImportJava.util.*;ImportJava.util.*;Importjava.text.*; Public classcheckinfo{ Public StaticHashset<user> User_data =NewHashset<user>(); PublicCheckinfo (hashset<user>user_data) { This. User_data =User_data; } Publicstring Checkaction (string name, string pwd, String repwd, String bd, String telnum, string email) {STRINGB Uilder result=NewStringBuilder (); intState = 1; //Password Judgment if(!pwd.equals (REPWD)) {Result.append ("Two input password is not the same ~\r\n"); State= 2; } //Birthday Judgment if(Bd.length ()! = 10) {result.append ("Birthday format is incorrect ~\r\n"); State= 2; }Else{ for(inti=0; I<bd.length (); i++) {Character Thischar=Bd.charat (i); if(i = = 4 | | i = = 7){ if(Thischar! = '-')) {result.append ("Birthday format is incorrect ~\r\n"); State= 2; } }Else{ if(!character.isdigit (Thischar)) {Result.append ("Birthday format is incorrect ~\r\n"); State= 2; } } } } //Phone number judgment if(Telnum.length ()! = 11) {result.append ("Input phone number is incorrect ~\r\n"); State= 2; }Else{ for(inti=0; I<telnum.length (); i++) {Character Thischar=Telnum.charat (i); if(!character.isdigit (Thischar)) {Result.append ("Input phone number is incorrect ~\r\n"); State= 2; }Else if(! (Telnum.startswith ("13") | |Telnum.startswith ("15") | |Telnum.startswith ("18") | |Telnum.startswith ("17")) {result.append ("Input phone number is incorrect ~\r\n"); State= 2; } } } //Mailbox Judgment if(!email.contains ("@") {result.append ("Mailbox is incorrect ~\r\n"); State= 2; } if(state = = 1){ //format Date Return objectDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd"); Date DATEBD=NULL; Try{datebd=Format.parse (BD); } Catch(ParseException e) {e.printstacktrace (); } User NewUser=NewUser (name, Repwd, DATEBD, telnum, email); if(!User_data.add (NewUser)) {Result.append ("User repeat!" "); State= 2; } if(state = = 1) {result.append ("Registration is successful!" "); } } returnresult.tostring (); }}
java--analog Sina Weibo user registration