package pojo;
/**
* 用户基本信息类
* @author pingfan
*
*/
public class User {
private String username;
private String password;
private String email;
private String phone;
public User() {
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
-------------------------------------------------------------------------------------
package dao;
import pojo.User;
/**
* 用户操作接口类
* @author pingfan
*
*/
public interface UserDao {
/**
* 用户登录功能
* @param username 用户名
* @param password 密码
* @return 是否登录成功
*/
public abstract boolean isLogin(String username,String password);
/**
* 用户注册功能
* @param user
* @return
*/
public abstract boolean regist(User user);
}
-----------------------------------------------------------------------------------------------------------------------
package dao_impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import pojo.User;
import dao.UserDao;
public class UserDaoImpl implements UserDao {
// 创建一个集合,用来存储注册用户
private static List<User> l = new ArrayList<User>();
@Override
/*
* 用户登录功能实现方法
*/
public boolean isLogin(String username, String password) {
boolean flag = false;
Iterator<User> it = l.iterator();
while (it.hasNext()) {
User user = (User) it.next();
if (user != null) {
if (user.getUsername().equals(username)
&& user.getPassword().equals(password)) {
flag = true;
break;
}
}
}
return flag;
}
/*
* (non-Javadoc) 用户注册功能实现方法
*
* @see dao.UserDao#regist(pojo.User)
*/
@Override
public boolean regist(User user) {
if (l.size() == 0) {
return l.add(user);
} else {
ListIterator<User> it = l.listIterator();
while (it.hasNext()) {
User user1 = (User) it.next();
if (!user1.getUsername().equals(user.getUsername())
&& !user1.getPassword().equals(user.getPassword())) {
it.add(user);
return true;
}
else {
return false;
}
}
return false;
}
}
public void zhaohui() {
if (l.size() == 0) {
System.out.println("没有注册用户了");
}
for(int x=0;x<l.size();x++){
User user = l.get(x);
System.out.println("你注册的用户名是:" + user.getUsername() + "--密码是:"
+ user.getPassword());
}
}
}
----------------------------------------------------------------------------------------------------------------------
package userlogin;
import game.RandomGame;
import java.util.Scanner;
import pojo.User;
import dao_impl.UserDaoImpl;
public class LoginTest {
public static void main(String[] args) {
System.out.println("欢迎光临!");
Y: while (true) {
System.out.println("请选择:");
System.out.println("1:登录");
System.out.println("2:注册");
System.out.println("3:退出");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
X: while (true) {
switch (line) {
case "1":
System.out.println("欢迎来到登录界面");
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
UserDaoImpl udi = new UserDaoImpl();
boolean flag = udi.isLogin(username, password);
if (flag) {
System.out.println("开始玩游戏吧");
System.out.println("1:猜数游戏");
System.out.println("切换用户请选: 0");
System.out.println("3:退出");
String st = sc.nextLine();
switch(st){
case "1":
RandomGame ra = new RandomGame();
ra.gamea();
System.exit(0);
break;
case "0":
continue Y;
case "3":
System.out.println("欢迎下次光临!再见!");
System.exit(0);
}
// System.exit(0);
} else {
System.out.println("账户或密码错误");
System.out.println("重新输入请选7");
System.out.println("返回主界面请选8");
System.out.println("找回密码请选9");
System.out.println("退出请选6");
String xuanze = sc.nextLine();
// String tc = sc.nextLine();
if (xuanze.equals("6")) {
System.out.println("欢迎下次光临!");
System.exit(0);
} else if (xuanze.equals("9")) {
//创建找回对象
UserDaoImpl zhaoudi = new UserDaoImpl();
//调用找回方法
zhaoudi.zhaohui();
System.out.println("是否继续登录:y:是 n:退出");
String xuanze1 = sc.nextLine();
switch(xuanze1) {
case "y":
continue X;
// break;
case "n":
System.out.println("欢迎下次光临!");
System.exit(0);
}
} else if (xuanze.equals("8")) {
continue Y;
} else if (xuanze.equals("7")) {
continue X;
}
}
break;
case "2":
System.out.println("欢迎进入注册界面:");
System.out.println("输入用户名:");
String newUsername = sc.nextLine();
System.out.println("请输入密码:");
String newPassword = sc.nextLine();
System.out.println("请输入邮箱:");
String newEmail = sc.nextLine();
System.out.println("请输入电话:");
String newPhone = sc.nextLine();
User user = new User();
user.setUsername(newUsername);
user.setPassword(newPassword);
user.setEmail(newEmail);
user.setPhone(newPhone);
UserDaoImpl newUdi = new UserDaoImpl();
boolean fa = newUdi.regist(user);
if(fa){
System.out.println("注册成功!");
continue Y;
}else{
System.out.println("该账户以被注册!");
System.out.println("请重新注册!");
continue Y;
}
// break;
default:
System.out.println("欢迎下次光临!");
System.exit(0);
break;
}
}
}
}
}
------------------------------------------------------------------------------------------------------------------
package game;
import java.util.Random;
import java.util.Scanner;
/**
* 这是一个随机数的小游戏
*
* @author pingfan
*
*/
public class RandomGame {
public static void main(String[] args) {
RandomGame ge = new RandomGame();
ge.gamea();
}
public void gamea() {
System.out.println("猜1--100之间的数!");
Random ra = new Random();
int sum = ra.nextInt(100) + 1;
for (int x=0;x<5;x++) {
Scanner sc = new Scanner(System.in);
int gamesum = sc.nextInt();
if(4-x==0){
System.out.println("很遗憾,你没猜中!");
System.out.println(sum);
break;
}
if (gamesum > sum) {
System.out.println("大了");
System.out.println("还剩"+(4-x)+"次机会");
} else if (gamesum < sum) {
System.out.println("小了");
System.out.println("还剩"+(4-x)+"次机会");
} else if (gamesum == sum) {
System.out.println("猜对了");
System.out.println("游戏结束!");
break;
}
}
}
}
|
|