/**
* 3、 定义一个用户类,提供一个注册方法,
* 注册可以用邮箱注册或者手机号注册,判断用户是否输入正确账号信息,正确返回 true ,
* 错误返回false(百度生成正则表达式)
*/
public class User {
private String name;
private String email;
private String phone;
public static Boolean isEmail(String str){
Boolean result=str.matches("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}");
return result;
}
public static Boolean isPhone(String str){
Boolean result=str.matches("0?(13|14|15|18)[0-9]{9}");
return result;
}
//get set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
/**
* 功能描述:测试类
*
* @author 顾英明
* @date 2022/12/05 22:27
*/
public class Test {
public static final String PHONEError="手机号输入错误";
public static final String EMAILERROR="邮箱输入错误";
public static final String INPUTERROR="输入错误,请输入1或0来选择注册的方式";
public static final String SUCCESS="注册成功";
public static void main(String[] args) {
User user=new User();
//当用户输入1时用邮箱注册 ,输入0时用手机号注册
String str="1";
Boolean isSuccess=false;
for (int i = 0; i <=3 ; i++) {
user.setName("张三");
//通过手机号注册
if (str.equals("1")){
String myPhone="13912572979";
if (User.isPhone(myPhone)){
user.setPhone(myPhone);
isSuccess=true;
}
else{
System.out.println(PHONEError);
isSuccess =false;
}
//通过邮箱注册
}else if(str.matches("0")){
String myEmail="2919925034@qq.com";
if (User.isEmail(myEmail)){
user.setEmail(myEmail);
isSuccess=true;
}else {
System.out.println(EMAILERROR);
isSuccess=false;
}
}else {
System.out.println(INPUTERROR);
isSuccess=false;
}
}
if (isSuccess){
System.out.println(SUCCESS);
System.out.println(user.toString());
}
}
}
运行结果:
注册成功
User{name='张三', email='null', phone='13912572979'}
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8