Java学习笔记(二)—— 简单的微博用户注册程序
User类
package webo;
import java.util.Date;
public class User {
private String name;
public User(String name, String mima, Date birthday, String telephone, String email) {
this.name = name;
}
public int hashCode() {
return name.hashCode();
}
public boolean equals(Object obj) {
if(this==obj) {//判断是否为同一个对象
return true;
}
if(obj==null) { //判断这个对象是否为空
return false;
}
if(getClass()!=obj.getClass()) { //判断这个对象是否为user类型
return false;
}
User u1=(User)obj;//强制类型转换
if(name==null) {//判断集合中用户名是否为空
if(u1.name!=null) {//判断对象中用户名是否为空
return false;
}
else if(!name.equals(u1.name)) {//判断用户名是否相同
return false;
}
}
return true;
}
}
Check类
package webo;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;
public class Check {
public static HashSet<User> hs=new HashSet<User>();
public Check(HashSet<User> hs) {
this.hs=hs;
}
//检验用户信息,返回登录信息
public String checkaction(String name, String mima, String remima, String birthday, String telephone, String email) {
StringBuilder result=new StringBuilder();
//1表示成功, 2表示失败
int state=1;
//密码判断
if(!mima.equals(remima)) {
result.append("两次输入密码不一致!\n");
state=2;
}
//生日判断
if(birthday.length()!=10) {
result.append("生日格式不正确!\n");
state=2;
}else {
for(int i=0;i<birthday.length();i++) {
Character thischar=birthday.charAt(i);
if(i==4 || i==7) {
if(!(thischar=='-')) {
result.append("生日格式不正确!\n");
state=2;
}else {
if((Character.isDigit(thischar))) {
result.append("");
state=2;
}
}
}
}
}
if(telephone.length()!=11) {
result.append("手机号不正确!\n");
state=2;
}
else if(!(telephone.startsWith("13") ||telephone.startsWith("15") ||telephone.startsWith("17") ||telephone.startsWith("18"))) {
result.append("手机号不正确!\n");
state=2;
}
if(!email.contains("@")) { //不带@符号的邮箱为无效邮箱
result.append("邮箱不正确!\n");
state=2;
}
if(state==1) {
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date databirthday=null;
try {
databirthday=format.parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
User newuser=new User(name, remima, databirthday, telephone, email);
if(!hs.add(newuser)) {
result.append("用户重复");
state=2;
}
if(state==1) {
result.append("注册成功!");
}
}
return result.toString();
}
}
UserRegister类
package webo;
import java.util.Date;
import java.util.HashSet;
import java.util.Scanner;
//注册类信息
public class UserRegister {
public static HashSet<User> hs=new HashSet<User>();
public static void main(String[] args) {
initdata();
Scanner scan=new Scanner(System.in);
System.out.print("请输入用户名: ");
String name=scan.nextLine();
System.out.print("请输入密码: ");
String mima=scan.nextLine();
System.out.print("请重复密码: ");
String remima=scan.nextLine();
System.out.print("请输入出生日期: ");
String birthday=scan.nextLine();
System.out.print("请输入手机号码: ");
String telephone=scan.nextLine();
System.out.print("请输入电子邮箱: ");
String email=scan.nextLine();
//检验用户信息,返回登录信息
Check checkinfo=new Check(hs);
String result=checkinfo.checkaction(name, mima, remima, birthday, telephone, email);
System.out.println("注册结果: "+result);
}
//,创建用户信息
private static void initdata() {
User use1=new User("zhangsan", "zhangsan123", new Date(), "18256656661", "zhangsan@qq.com");
User use2=new User("lisi", "lisi123", new Date(), "18256656662", "lisi@qq.com");
User use3=new User("wangwu", "wangwu123", new Date(), "18256656663", "wangwu@qq.com");
hs.add(use1);
hs.add(use2);
hs.add(use3);
}
}
运行上面的代码,填入信息进行注册
注册新用户