アナログ新浪微博ユーザー登録


アナログ新浪微博ユーザー登録
1初期化画面
package   2;

import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;

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 userName = scan.nextLine();
	
	System.out.println("       :");
	String password =scan.nextLine();
	
	System.out.println("       :");
	String repassword =scan.nextLine();
	
	System.out.println("    :");
	String birthday =scan.nextLine();
	
	System.out.println("    :");
	String telNumber=scan.nextLine();
	
	System.out.println("    ");
	String email =scan.nextLine();
	//      ,        
	CheckInfo checkInfo =new CheckInfo(USER_DATA);
	String rsult=checkInfo.checkAction(userName,password,
			repassword,birthday,telNumber,email);
	System.out.println("    "+rsult);
}
 //     ,           
private static void initData() {
     
	// TODO Auto-generated method stub
    User user1=new User("  ","zz,123",new Date(),"18810319240","[email protected]");
    User user2=new User("  ","zz,123",new Date(),"18618121193","[email protected]");
    USER_DATA.add(user1);
    USER_DATA.add(user2);
}
}


2、データパッケージと書き換え
package   2;

import java.util.Date;

public class User {
     
	private String userName;//    
	private String password;//   
	private Date birthday;//     
	private String telNumber;//     
	private String email;

	public User() {
     
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String userName, String password, Date birthday, String telNumber, String email) {
     
		super();
		this.userName = userName;
		this.password = password;
		this.birthday = birthday;
		this.telNumber = telNumber;
		this.email = email;
	}

	@Override
	public int hashCode() {
     
		return userName.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
     
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (userName == null) {
     
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		return true;
	}

}


3、データ検査
package   2;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;

public class CheckInfo {
     
	public static HashSet<User> USER_DATA = new HashSet<User>();//     

	public CheckInfo(HashSet<User> USER_DATA) {
     //     
		// TODO Auto-generated constructor stub
		this.USER_DATA = USER_DATA;
	}

	//       ,        ;
	public String checkAction(String userName, String password, String repassword, String birthday, String telNumber,
			String email) {
     
		// TODO Auto-generated method stub
		StringBuilder result = new StringBuilder();
		// 1      , 2    
		int state = 1;
		//     
		if (!password.equals(repassword)) {
     
			result.append("         !\r
"
); state = 2; } // 2000-11-39 if (birthday.length() != 10) { result.append(" !\r
"
); state = 2; } else { for (int i = 0; i < birthday.length(); i++) { Character thisChar = birthday.charAt(i); if (i == 4 || i == 7) { if (!(thisChar == '-')) { result.append(" \r
"
); state = 2; } } else { if (!(Character.isDigit(thisChar))) { result.append(" \r
"
); state = 2; } } } if(telNumber.length()!=11) { result.append(" 11\r
"
); state=2; }else if(!(telNumber.startsWith("13")|| telNumber.startsWith("15")|| telNumber.startsWith("17")||telNumber.startsWith("18"))){ result.append(" \r
"
); state =2; } // if(!email.contains("@")) { result.append(" \r
"
); state=2; } // , if(state==1) { // Date DateFormat format=new SimpleDateFormat("yyyy-MM-dd"); Date dateBirthday =null; try { dateBirthday =format.parse(birthday) ; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } User newUser=new User(userName,repassword, dateBirthday,telNumber,email); if(!(USER_DATA.add(newUser))) { result.append(" "); state=2; }if(state==1) { result.append(" "); } } } return result.toString(); } }