import java.util.Date;
import java.util.Objects;
public class User {
// 用户名
private String username;
// 密码
private String password;
// 生日
private Date birthday;
// 手机号
private String phone;
// 邮箱
private String email;
// 基于 IntellJ Idea 软件开发的快捷键:
// Alt + Insert Getter And Setter
// Alt + Insert equals And hashCode
// Alt + Insert toString
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(username, user.username) &&
Objects.equals(password, user.password) &&
Objects.equals(birthday, user.birthday) &&
Objects.equals(phone, user.phone) &&
Objects.equals(email, user.email);
}
@Override
public int hashCode() {
return Objects.hash(username, password, birthday, phone, email);
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", birthday=" + birthday +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
'}';
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}import java.util.HashSet;
import java.util.Scanner;
public class UserRegister {
static HashSet hs = new HashSet();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
System.out.println("请重复输入密码:");
String repassword = sc.nextLine();
System.out.println("请输入出生日期:");
String birthday = sc.nextLine();
System.out.println("请输入手机号码:");
String phone = sc.nextLine();
System.out.println("请输入电子邮箱:");
String email = sc.nextLine();
CheckInfo ci = new CheckInfo();
String sb = ci.checkInfo(username, password, repassword, birthday, phone, email);
System.out.println("注册的结果是:" + sb);
}
}import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
public class CheckInfo {
HashSet hashSet = new HashSet();
StringBuilder sb = new StringBuilder();
int status = 1 ; // 1 代表成功 2 代表 失败
public String checkInfo(String username,String password,String repassword,String birthday,String phone, String email){
// 生日的判断
if (birthday.length() != 10){ // 1999-09-09
sb.append("您填写的生日格式有误,请检查!");
status = 2 ;
}
// 密码的判断
if (!password.equals(repassword)){
sb.append("两次密码填写不一样,请检查!");
status = 2 ;
}
// 手机格式和开头号码的判断
if (phone.length() != 11){ // 1999-09-09
sb.append("您填写的手机号码格式有误,请检查!");
status = 2 ;
}else if (!(phone.startsWith("13") ||phone.startsWith("15") || phone.startsWith("17") || phone.startsWith("18") ) ){ // 1999-09-09
sb.append("您填写的手机号码不正确,请检查!");
status = 2 ;
}
// 邮箱的判断
if (!email.contains("@")){
sb.append("您填写的电子邮箱格式有误,请检查!");
status = 2 ;
}
// 如果以上信息的校验都是无误 则可以将新用户加入到集合内
if(status == 1){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateBirthday = null;
try {
dateBirthday = sdf.parse(birthday); // 字符串转为 日期类型 有可能出现错误、错误了 则执行catch内的代码 否则 不执行!
} catch (ParseException e) {
e.printStackTrace();// Stack 栈 数据结构与算法: 内存分析
}
// 创建user 对象
User user = new User();
// 完成对象的内容封装
user.setUsername(username);
user.setPassword(repassword);
user.setBirthday(dateBirthday);
user.setPhone(phone);
user.setEmail(email);
}
if (status == 1){
sb.append("注册成功!");
}
return sb.toString();
}
}在这里插入图片描述
请输入用户名: 张三 请输入密码: 1 请重复输入密码: 1 请输入出生日期: 1999-09-09 请输入手机号码: 14696963322 请输入电子邮箱: 369@qq.com 您填写的手机号码不正确,请检查!