package cn.itcast.pojo;
public class User {
private String username;
private String password;
private String phonenum;
public User() {
super();
}
public User(String username, String password, String phonenum) {
super();
this.username = username;
this.password = password;
this.phonenum = phonenum;
}
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 getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
}
package cn.itcast.dao;
import cn.itcast.pojo.User;
public interface UserDao {
public abstract boolean isLogin(String username,String password);
public abstract void register(User user);
}
package cn.itcast.daoimpl;
import java.util.ArrayList;
import cn.itcast.dao.UserDao;
import cn.itcast.pojo.User;
public class UserDaoImpl implements UserDao {
private static ArrayList<User> arrayList = new ArrayList<User>();
@Override
public boolean isLogin(String username, String password) {
boolean flag = false;
/*
* for (int i = 0; i < arrayList.size(); i++) { User user =
* arrayList.get(i); if (!arrayList.isEmpty()) { if
* (user.getUsername()==username && user.getPassword()==password) { flag
* = true; break; } } }
*/
for (User u : arrayList) {
User user = u;
if ((user.getUsername().equals(username))
&& (user.getPassword().equals(password))) {
flag = true;
break;
}
}
return flag;
}
@Override
public void register(User user) {
arrayList.add(user);
}
}
package cn.itcast.test;
import java.util.Scanner;
import cn.itcast.daoimpl.UserDaoImpl;
import cn.itcast.pojo.User;
public class UserTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("欢迎来到itcast!");
System.out.println("1、登录");
System.out.println("2、注册");
System.out.println("3、退出");
System.out.println("请选择:");
String s = sc.nextLine();
switch (s) {
case "1":
System.out.println("欢迎来到登录界面!");
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
UserDaoImpl ud = new UserDaoImpl();
if (ud.isLogin(username, password)) {
System.out.println("登陆成功");
System.exit(0);
}
if (i == 2) {
System.out.println("你的账号已经冻结,请于周丹玲联系!");
System.exit(0);
}
System.out.println("账号或密码错误,你还有" + (2 - i) + "次机会!!!");
}
break;
case "2":
System.out.println("欢迎来到注册界面!");
System.out.println("请输入用户名");
String newUsername = sc.nextLine();
System.out.println("请输入密码:");
String newPassword = sc.nextLine();
System.out.println("请输入手机号:");
String newPhonenum = sc.nextLine();
User user = new User();
user.setUsername(newUsername);
user.setPassword(newPassword);
user.setPhonenum(newPhonenum);
UserDaoImpl newudi = new UserDaoImpl();
newudi.register(user);
System.out.println("注册成功!");
break;
default:
System.out.println("欢迎下次再来!");
System.exit(0);
}
}
}
}
运行结果
欢迎来到itcast!
1、登录
2、注册
3、退出
请选择:
2
欢迎来到注册界面!
请输入用户名
admin
请输入密码:
123456
请输入手机号:
12345678
注册成功!
欢迎来到itcast!
1、登录
2、注册
3、退出
请选择:
1
欢迎来到登录界面!
请输入用户名:
admin
请输入密码:
123456
登陆成功
|
|