package cn.itcast8.dao.daoimpl;
import java.util.ArrayList;
import cn.itcast8.pojo.Student;
import cn.itcast8.dao.StuDao;
public class StuDaoImpl implements StuDao {
private static ArrayList<Student> student = new ArrayList<Student>();
@Override
public boolean login(String name, String id) {
boolean flag = false;
// for循环
for (int i = 0; i < student.size(); i++) {
Student stu = student.get(i);
if (stu != null) {
if (stu.getName().equals(name) && stu.getId().equals(id)) {
return true;
}
}
}
return flag;
}
@Override
public void regist(Student stu) {
student.add(stu);
}
}
package cn.itcast8.dao;
import cn.itcast8.pojo.Student;
public interface StuDao {
public abstract boolean login(String name,String id);
public abstract void regist(Student stu);
}
package cn.itcast8.pojo;
/**
* 这是定义一个学生类
* @author wingzhe
*
*/
public class Student {
// 姓名
private String name;
// 年龄
private String age;
// 学号
private String id;
public Student() {
}
public Student(String name, String id) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* 这是重写equals方法
*/
@Override
public boolean equals(Object obj) {
boolean flag = false;
if (this == obj) {
return true;
}
if (!(obj instanceof Student)) {
return flag;
}
Student s = (Student) obj;
return this.name == s.name && this.id == s.id;
}
}
package cn.itcast8.test;
import java.util.Scanner;
import cn.itcast8.dao.daoimpl.StuDaoImpl;
import cn.itcast8.pojo.Student;
public class StuTest {
public static void main(String[] args) {
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();
switch (line) {
case "1":
System.out.println("请输入帐号");
String name = sc.nextLine();
System.out.println("请输入学号");
String id = sc.nextLine();
// 创建对象进行比较
StuDaoImpl sdi = new StuDaoImpl();
if (sdi.login(name, id)) {
System.out.println("登陆成功");
System.exit(0);
break;
} else {
System.out.println("登录失败,请重试");
break;
}
case "2":
System.out.println("欢迎注册");
System.out.println("请输入帐号");
String newName = sc.nextLine();
System.out.println("请输入学号");
String newId = sc.nextLine();
System.out.println("请输入年龄");
String newAge = sc.nextLine();
Student stu = new Student();
stu.setName(newName);
stu.setId(newId);
stu.setAge(newAge);
StuDaoImpl newSdi = new StuDaoImpl();
newSdi.regist(stu);
System.out.println("注册成功,请返回登录");
break;
default:
System.out.println("欢迎下次再来");
System.exit(0);
break;
}
}
}
} |
|