javase:String+正则实现注册功能
使用String工具类实现简单的注册功能,主要包括使用正则表达式判断输入的字符串是否合法,结合异常抛出,完善了注册的功能,使其更加像一个完整项目中的功能
User.java
package StringRegularExpression;
import java.io.Serializable;
import java.util.Objects;
public class User implements Serializable {
private int id ;
private String userName;
private String password;
private int age;
private boolean gender;
public String getUserName() {
return userName;
}
public boolean isGender() {
return gender;
}
public int getAge() {
return age;
}
public String getPassword() {
return password;
}
public int getId() {
return id;
}
public User(){
super();
}
public User(int id,String userName,String password,int age,boolean gender){
super();
this.id = id;
this.userName = userName;
this.password = password;
this.age = age;
this.gender = gender;
}
public void setId(int id){
this.id = id;
}
public void setUserName(String password){
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setAge(int age){
this.age = age;
}
public void setGender(boolean gender){
this.gender = gender;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return id == user.id &&
age == user.age &&
gender == user.gender &&
userName.equals(user.userName) &&
password.equals(user.password);
}
@Override
public int hashCode() {
return Objects.hash(id, userName, password, age, gender);
}
}
UserDemo.java
package StringRegularExpression;
import java.io.Serializable;
import java.util.Objects;
public class User implements Serializable {
private int id ;
private String userName;
private String password;
private int age;
private boolean gender;
public String getUserName() {
return userName;
}
public boolean isGender() {
return gender;
}
public int getAge() {
return age;
}
public String getPassword() {
return password;
}
public int getId() {
return id;
}
public User(){
super();
}
public User(int id,String userName,String password,int age,boolean gender){
super();
this.id = id;
this.userName = userName;
this.password = password;
this.age = age;
this.gender = gender;
}
public void setId(int id){
this.id = id;
}
public void setUserName(String password){
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setAge(int age){
this.age = age;
}
public void setGender(boolean gender){
this.gender = gender;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return id == user.id &&
age == user.age &&
gender == user.gender &&
userName.equals(user.userName) &&
password.equals(user.password);
}
@Override
public int hashCode() {
return Objects.hash(id, userName, password, age, gender);
}
}
InputInfoException.java
package StringRegularExpression;
public class InputInfoException extends Exception {
public InputInfoException() {
}
public InputInfoException(String message) {
super(message);
}
public InputInfoException(String message, Throwable cause) {
super(message, cause);
}
public InputInfoException(Throwable cause) {
super(cause);
}
public InputInfoException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
小结:这算是一个结构比较完整的注册功能,User类中的get方法,set方法,hasCode方法等都以定义,主函数中也有对异常的处理。通过这个demo主要还是学会使用正则表达式处理数据,在完整项目中可以更加方便的处理数据。
|