java 登录注册的验证
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Person{
String zhanhao;
String mima;
public Person(String name,String id){
this.zhanhao=name;
this.mima=id;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "账号="+this.zhanhao+"密码="+this.mima;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (obj instanceof Person) {
Person p = (Person)obj;
if (p.zhanhao==this.zhanhao&&p.mima==this.mima) {
return true;
}else {
return false;
}
}else {
return false;
}
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.zhanhao.hashCode();
}
}
class ZuCe{
static ArrayList shuju=new ArrayList();
public void paly(){
System.out.println("请选择功能: 注册(A)登录(B)退出系统(q)");
Scanner sca = new Scanner(System.in);
String str = sca.next();
if (str.equalsIgnoreCase("a")) {
yi();
}else if (str.equalsIgnoreCase("b")) {
er();
}else if (str.equalsIgnoreCase("c")) {
System.out.println("谢谢你的使用");
}else {
System.out.println("输入错误请重启");
}
}
public void yi(){
System.out.println("请输入注册账号:");
Scanner sca = new Scanner(System.in);
String str = sca.next();
for (int i = 0; i < shuju.size(); i++) {
Person p = (Person) shuju.get(i);
if (p.zhanhao.equalsIgnoreCase(str)) {
System.out.println("你输入的账号已经存在请重新输入");
yi();
break;
}
}
System.out.println("请输入密码:");
Scanner sca1 = new Scanner(System.in);
String sum = sca1.next();
Person p=new Person(str, sum);
shuju.add(p);
System.out.println(shuju);
paly();
}
public void er(){
System.out.println("请输入账号:");
Scanner sca = new Scanner(System.in);
String str = sca.next();
System.out.println("请输入密码:");
Scanner sca1 = new Scanner(System.in);
String sum = sca1.next();
Person p=new Person(str, sum);
for (int i = 0; i < shuju.size(); i++) {
Person p1=(Person)shuju.get(i);
if (p1.zhanhao.equals(p.zhanhao)&&p1.mima.equals(p.mima)) {
System.out.println("登录成功");
System.out.println("用户名为:"+p.zhanhao);
paly();
}else {
System.out.println("登录失败");
paly();
}
}
}
}
public class Work2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ZuCe zz=new ZuCe();
zz.paly();
}
}