习题10
1、
//User.java
1 import java.sql.SQLException; 2 3 /**
4 * 用户类,用于记录各类登录用户信息 5 * 6 */
7 public abstract class User { 8 9 //习题10-1 10 public abstract boolean login(String id,String password) throws SQLException; 11 12 protected String username; //用户名 13 protected String password; //口令 14 protected String UserType; //用户类型 15 16 17 public String getUsername() { 18 return username; 19 } 20 public void setUsername(String username) { 21 this.username = username; 22 } 23 public String getPassword() { 24 return password; 25 } 26 public void setPassword(String password) { 27 this.password = password; 28 } 29 30 public String getUserType() { 31 return UserType; 32 } 33 public void setUserType(String userType) { 34 UserType = userType; 35 } 36 37 public User() {} 38 public User(String username, String password, String userType) { 39 this.username = username; 40 this.password = password; 41 UserType = userType; 42 } 43 }
//Administrator2.java
1 import java.sql.SQLException; 2
3 import dao.AdministratorDao;
4 import dao.AdministratorDaoImpl; 5 import entity.User; 6 import util.Const; 7
8 public class Administrator2 extends User { 9 10 public Administrator2() { 11 this.userType=\管理员\12 }
13 14 @Override 15 public boolean login(String id, String password) throws SQLException { 16 AdministratorDao dao=new AdministratorDaoImpl(); 17 return dao.login(id, password); 18 } 19 }
//Student2.java
1 import java.sql.SQLException; 2
3 import dao.StudentDao;
4 import dao.StudentDaoImpl; 5 import entity.Student; 6 import entity.User; 7 import util.Const; 8 9 /**
10 * 学生实体类,与表student对应 11 * 12 */
13 public class Student2 extends User { 14 15 private String name; //姓名 16 private String phone; //手机号 17 private String sex; //性别 18 private String department; //所属院系 19 private String classname; //班级 20 private String major; //所学专业 21 22 public Student2() { 23 this.userType=\学生\24 } 25 26 @Override 27 public boolean login(String id, String password) throws SQLException { 28 StudentDao dao=new StudentDaoImpl(); 29 Student s=dao.findStudentById(id); 30 if(s!=null) { 31 if(s.getPassword().equals(password)) 32 return true; 33 else 34 return false; 35 }else 36 return false; 37 } 38 39 public String getName() { 40 return name; 41 } 42 public void setName(String name) { 43 this.name = name; 44 } 45 46 public String getPassword() { 47 return password; 48 } 49 public void setPassword(String password) { 50 this.password = password; 51 } 52 public String getPhone() { 53 return phone; 54 } 55 public void setPhone(String phone) { 56 this.phone = phone; 57 } 58 public String getSex() { 59 return sex;
60 } 61 public void setSex(String sex) { 62 this.sex = sex; 63 } 64 public String getDepartment() { 65 return department; 66 } 67 public void setDepartment(String department) { 68 this.department = department; 69 } 70 public String getClassname() { 71 return classname; 72 } 73 public void setClassname(String classname) { 74 this.classname = classname; 75 } 76 public String getMajor() { 77 return major; 78 } 79 public void setMajor(String major) { 80 this.major = major; 81 } 82 }
//Teacher2.java
1 import java.sql.SQLException; 2 import dao.TeacherDao;
3 import dao.TeacherDaoImpl; 4 import entity.Teacher; 5 import entity.User; 6 import util.Const; 7 8 /**
9 * 教师实体类,与表teacher对应 10 * 11 */
12 public class Teacher2 extends User { 13 14 private String name; //姓名 15 private String sex; //性别 16 private int age; //年龄 17 18 public Teacher2() { 19 this.userType=\教师\20 } 21 22 @Override 23 public boolean login(String id, String password) throws SQLException { 24 TeacherDao dao=new TeacherDaoImpl(); 25 Teacher t=dao.findTeacherById(id); 26 if(t!=null) { 27 if(t.getPassword().equals(password)) 28 return true; 29 else 30 return false; 31 }else 32 return false; 33 } 34 35 36 public String getName() { 37 return name; 38 } 39 40 public void setName(String name) { 41 this.name = name; 42 } 43
相关推荐: