package cn.itcast.pojo;
public class User {
private String username;
private String password;
private String phonenum;
public User() {
super();
// TODO Auto-generated constructor stub
}
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 {
/**
* 这是用户登录功能
*
* @param name
* 用户名
* @param password
* 密码
* @return 是否登录成功
*/
public abstract boolean isIogin(String name, String password);
/**
* 这是一个用户注册功能
*
* @param user
* 用户
*/
public abstract void register(User user);
}
package cn.itcast.dao.impl;
import cn.itcast.dao.UserDao;
import cn.itcast.pojo.User;
/**
*
* @author gzy
*
*/
public class UserDaoImpl implements UserDao {
private static User[] users = new User[5];
private static int index = 0;
@Override
public boolean isIogin(String name, String password) {
boolean flag = false;
for (int i = 0; i < users.length; i++) {
if (users[i] != null) {
if (name.equals(users[i].getUsername())
&& password.equals(users[i].getPassword())) {
flag = true;
break;
}
}
}
return flag;
}
@Override
public void register(User user) {
users[index++] = user;
}
}
package cn.itcast.test;
import java.util.Scanner;
import cn.itcast.dao.impl.UserDaoImpl;
import cn.itcast.pojo.User;
public class UserTest {
public static void main(String[] args) {
while (true) {
System.out.println("Welcome to itcast!");
System.out.println("1、land");
System.out.println("2、register");
System.out.println("3、exit");
System.out.println("Please choose:");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
switch (line) {
case "1":
System.out.println("Welcome!");
System.out.println("Input your username:");
String username = sc.nextLine();
System.out.println("Input your password:");
String password = sc.nextLine();
UserDaoImpl udi = new UserDaoImpl();
boolean flag = udi.isIogin(username, password);
if (flag) {
System.out.println("Play game!");
System.exit(0);
}
System.out.println("Wrong number!");
break;
case "2":
System.out.println("Welcome!");
System.out.println("Input your username:");
String newUsername = sc.nextLine();
System.out.println("Input your password:");
String newPassword = sc.nextLine();
System.out.println("Input your phone number");
String newPhonenum = sc.nextLine();
User users = new User();
users.setUsername(newUsername);
users.setPassword(newPassword);
users.setPhonenum(newPhonenum);
UserDaoImpl newUdi = new UserDaoImpl();
newUdi.register(users);
System.out.println("Done!");
break;
case "3":
default:
System.out.println("Welcome back! Thankyou!");
System.exit(0);
break;
}
}
}
}
|