【案例5-3】 模拟用户注册
【案例介绍】
1.任务描述
互联网为我们提供了巨大的便利,如微信带给我们的视频、淘宝带给我们便利的购物等,但这些APP都需要有一个账户才可以登录,而账户需要注册可以获取。
本例要求编写一个程序,模拟用户注册。用户输入用户名、密码、确认密码、生日(格式为yyyy–mm—dd为正确)、手机号(手机号长度为11位,并且以13、15、17、18为开头的手机号为正确)、邮箱(包含符号“@”为正确)信息之后,判断信息正确后,验证用户是否重复,重复则给出相应提示,如果不重复则注册成功。案例要求使用HashSet集合实现。
package com.j2se.myInstances.example6_3;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;
public class UserRegister {
private static HashSet<User> USER_DATA = new HashSet<User>();
public static void main(String[] a) {
initData();
Scanner sc = new Scanner(System.in);
System.out.println("用户名:");
String uname = sc.next();
System.out.println("密码:");
String upass = sc.next();
System.out.println("重复密码:");
String repass = sc.next();
System.out.println("邮箱:");
String email = sc.next();
System.out.println("生日:");
String birthdate = sc.next();
System.out.println("手机号:");
String pnumber = sc.next();
// 校验用户信息,返回登录状态信息
CheckInfo checkInfo = new CheckInfo(USER_DATA);
String str = checkInfo.checkAction(uname, upass, repass, email, pnumber, birthdate);
System.out.println("注册结果:" + str);
}
// 初始化数据,创建两个已存在的用户信息
private static void initData() {
User user = new User("James", "js123", "[email protected]",
"18897779984", new Date().toString());
User user2 = new User("Harden", "hd123", "[email protected]",
"13454647878", new Date().toString());
USER_DATA.add(user);
USER_DATA.add(user2);
}
}
package com.j2se.myInstances.example6_3;
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<>();
public CheckInfo(HashSet<User> USER_DATA) {
this.USER_DATA = USER_DATA;
}
// 校验用户信息,返回登录状态信息
public String checkAction(String uname, String upass, String repass, String email,
String uphone, String birthdate) {
StringBuilder result = new StringBuilder();
int state = 1; // 1: 成功,2:失败
// 密码判断
if (!upass.equals(repass)) {
result.append("两次输入密码不一致\n");
state = 2;
}
// 生日判断
if (birthdate.length() != 10) {
result.append("生日格式错误!\n");
state = 2;
} else {
for (int i = 0; i < birthdate.length(); i++) {
Character c = birthdate.charAt(i);
if (i == 4 || i == 7) {
if (c != '-') {
result.append("生日格式错误!\n");
state = 2;
}
}
}
}
// 手机号判断
if (uphone.length() != 11) {
result.append("手机号码无效!, 必须是11位\n");
state = 2;
} else if (!uphone.startsWith("13") || !uphone.startsWith("15") || !uphone.startsWith("17") || !uphone.startsWith("18")) {
result.append("手机号码不正确!\n");
state = 2;
}
// 邮箱判断
// 判断邮箱地址,默认不带@符号的邮箱为无效邮箱
if (!email.contains("@")) {
result.append("邮箱格式不正确!\n");
state = 2;
}
// 如果以上信息校验无误,则将新用户加入到集合
if (state == 1) {
// 格式化日期返回Date对象
// 定义日期格式
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date dateBirthday = null;
try {
dateBirthday = format.parse(birthdate);
} catch (ParseException e) {
e.printStackTrace();
}
User user = new User(uname, repass, email, uphone, birthdate);
if (!USER_DATA.add(user)) {
result.append("用户已存在!\n");
state = 2;
}
if (state == 1) result.append("用户已存在!\n");
}
return result.toString();
}
}
package com.j2se.myInstances.example6_3;
public class User {
private String username;
private String password;
private String email;
private String phoneNumber;
private String birthdate;
public User() {
}
public User(String username, String password, String email, String phoneNumber, String birthdate) {
this.username = username;
this.password = password;
this.email = email;
this.phoneNumber = phoneNumber;
this.birthdate = birthdate;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (username != null ? !username.equals(user.username) : user.username != null) return false;
if (password != null ? !password.equals(user.password) : user.password != null) return false;
if (email != null ? !email.equals(user.email) : user.email != null) return false;
if (phoneNumber != null ? !phoneNumber.equals(user.phoneNumber) : user.phoneNumber != null) return false;
return birthdate != null ? birthdate.equals(user.birthdate) : user.birthdate == null;
}
@Override
public int hashCode() {
return username.hashCode(); // 以用户名作为是否重复的依据
}
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
}