1、完成书上157页实验题第一题。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/***
* 注册功能--检查用户的各种输入是否合法
* 用户名只能是字母、数字和下划线,6-8个字符
* 密码只能是字母,并且两次要相同,4-8个字符
* 邮箱
* QQ号
* 生日
* 电话:区号-数字
*/
public class RegisteUser {
public User registe() {
//定义正则表达式
String regex_name="\\w{6,8}";//匹配数0-9,字母a-z,A-Z,下划线
String regex_password="[a-zA-Z]{4,8}";//匹配字符
String regex_email="[a-zA-Z0-9_+.-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z0-9]{2,4}";//匹配邮箱
String regex_qq="[1-9][0-9]{4,11}";//匹配QQ
String regex_birthday="(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)";//
String regex_phoneNumber="((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}";//匹配手机号码
Scanner scn = new Scanner(System.in);
System.out.print("用户名(字母、数字和下划线)6-8个字符:");
String name = scn.next();
while(!name.matches(regex_name)){
System.out.println("请用户名只能包含字母、数字和下划线,且为6-8位");
System.out.print("请输入用户名:");
name=scn.next();
}
System.out.print("密码(只能是字母)4-8个字符:");
String password = scn.next();
while(!password.matches(regex_password)){
System.out.println("密码只能由字母组成,且为4-8位");
System.out.print("请输入密码:");
password=scn.next();
}
System.out.print("请再次输入密码:");
String repassword = scn.next();
while(!repassword.equals(password)){
System.out.println("两次输入密码不符");
System.out.print("请再次输入密码:");
repassword = scn.next();
}
System.out.print("邮箱:");
String email = scn.next();
while(!email.matches(regex_email)){
System.out.println("输入的邮箱格式错误");
System.out.print("请输入邮箱:");
email=scn.next();
}
System.out.print("QQ:");
String qq = scn.next();
while(!qq.matches(regex_qq)){
System.out.println("输入的QQ格式错误");
System.out.print("请输入QQ:");
qq=scn.next();
}
System.out.print("生日(yyyy-MM-dd):");
String birth = scn.next();
while(!birth.matches(regex_birthday)){
System.out.println("输入的生日格式有误");
System.out.print("请输入生日:");
birth=scn.next();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date birthday=null;
try {
birthday = sdf.parse(birth);
} catch (ParseException e) {
System.out.println("输入的生日格式有误");
}
System.out.print("手机:");
String phoneNumber = scn.next();
while(!phoneNumber.matches(regex_phoneNumber)){
System.out.println("输入的手机格式有误");
System.out.print("请输入手机:");
phoneNumber=scn.next();
}
return new User(name,password,email,qq,birthday,phoneNumber);
}
public static void main(String[] args) throws ParseException {
RegisteUser ru = new RegisteUser();
User user = ru.registe();
System.out.println("\n\n注册用户信息为:");
System.out.println(user);
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class User {
private String name;
private String password;
private String email;
private String qq;
private Date birthday;
private String phoneNumber;
public String toString(){
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
StringBuilder builder = new StringBuilder("");
builder.append("用户名:"+name+"\n");
builder.append("密码:"+password+"\n");
builder.append("email:"+email+"\n");
builder.append("QQ:"+qq+"\n");
builder.append("生日:"+sdf.format(birthday)+"\n");
builder.append("手机:"+phoneNumber);
return builder.toString();//用户类
}
public User(String name, String password, String email, String qq,
Date birthday, String phoneNumber) {
super();
this.name = name;
this.password = password;
this.email = email;
this.qq = qq;
this.birthday = birthday;
this.phoneNumber = phoneNumber;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}