Dao包
package com.gem.fruit.dao;
import com.gem.fruit.entity.User;
public class UserDaoImpl implements IUserDao {
User[] users = new User[10];
int index = 0;
@Override
public void register(User user) {
if((index++)==users.length){
User[] newArr = new User[users.length*2];
for(int i=0;i<=users.length;i++){
newArr[i] = users[i];
}
users = newArr;
}else{
users[index++] = user;
}
}
@Override
public boolean login(User user) {
if(user == null)
return false;
for(int i=0;i<users.length;i++){
if(user.equals(users[i]))
return true;
}
return false;
}
}
Main包
package com.gem.fruit.main;
import java.util.Scanner;
import com.gem.fruit.dao.IUserDao;
import com.gem.fruit.dao.UserDaoImpl;
import com.gem.fruit.entity.User;
public class MainApp {
static IUserDao ud = new UserDaoImpl();
static Scanner sc = new Scanner(System.in);
static MainApp ma = new MainApp();
int count = 0;
public static void main(String[] args) {
System.out.println("请选择操作:(登录(L),注册(R))");
String op = sc.next();
if("R".equals(op)){
ma.register();
System.out.println("注册成功!请登录:");
ma.LoginC1();
}else if("L".equals(op)){
ma.LoginC1();
}else{
System.out.println("输入错误,系统推出!");
System.exit(0);
}
}