JavaWeb项目开发案例精粹-第3章在线考试系统-004Service层
1.
1 package com.sanqing.service; 2 3 import java.util.List; 4 5 import com.sanqing.po.Student; 6 7 public interface StudentService { 8 //判断是否为合法学生,从而决定是否允许登录 9 public boolean allowLogin(String studentID,String password); 10 //获得学生信息 11 public Student getStudentInfo(String studentID); 12 //设置学生成绩 13 public void setStudentResult(String studentID,int result); 14 //根据学生姓名查找学生 15 public List<Student> getStudentByName(String studentName); 16 //根据班级查找学生 17 public List<Student> getStudentByClass(String sclass); 18 }
2.
1 package com.sanqing.service; 2 3 import java.util.List; 4 5 import com.sanqing.dao.StudentDAO; 6 import com.sanqing.dao.StudentDAOImpl; 7 import com.sanqing.po.Student; 8 9 public class StudentServiceImpl implements StudentService{ 10 private StudentDAO studentDAO = new StudentDAOImpl(); 11 12 public boolean allowLogin(String studentID, String password) { 13 14 Student student = studentDAO.findByStudentID(studentID); 15 if(student == null) {//判断是否存在该ID的学生 16 return false; 17 }else { 18 if(password.equals(student.getPassword())) {//判断密码是否相同 19 return true; 20 }else{ 21 return false; 22 } 23 24 } 25 } 26 27 public Student getStudentInfo(String studentID) { 28 return studentDAO.findByStudentID(studentID); 29 } 30 31 public void setStudentResult(String studentID, int result) { 32 Student student = studentDAO.findByStudentID(studentID);//根据ID查找到该学生 33 student.setResult(result);//设置其成绩 34 studentDAO.updateStudent(student);//更新学生信息 35 } 36 37 public List<Student> getStudentByName(String studentName) { 38 return studentDAO.findByStudentName(studentName); 39 } 40 41 public List<Student> getStudentByClass(String sclass) { 42 return studentDAO.findByStudentClass(sclass); 43 } 44 }
3.
1 package com.sanqing.service; 2 3 import java.util.List; 4 5 import com.sanqing.po.Subject; 6 import com.sanqing.util.Page; 7 import com.sanqing.util.PageResult; 8 9 public interface SubjectService { 10 // 添加试题,首先判断该试题标题是否已经存在,如果已经则不能添加 11 public boolean saveSubject(Subject subject); 12 // 按分页信息查询试题 13 public PageResult querySubjectByPage(Page page); 14 // 查看试题详细信息 15 public Subject showSubjectParticular(int subjectID); 16 // 更新试题信息 17 public void updateSubject(Subject subject); 18 // 删除试题信息 19 public void deleteSubject(int subjectID); 20 //模糊查询试题信息 21 public PageResult likeQueryBySubjectTitle(String subjectTitle,Page page); 22 //随机查询试题记录 23 public List<Subject> randomFindSubject(int number); 24 //计算学生得分 25 public int accountResult(List<Integer> subjectIDs,List<String> studentAnswers); 26 }
4.
1 package com.sanqing.service; 2 3 import java.util.List; 4 5 import com.sanqing.dao.SubjectDAO; 6 import com.sanqing.dao.SubjectDAOImpl; 7 import com.sanqing.po.Subject; 8 import com.sanqing.util.Page; 9 import com.sanqing.util.PageResult; 10 import com.sanqing.util.PageUtil; 11 12 public class SubjectServiceImpl implements SubjectService{ 13 private SubjectDAO subjectDAO = new SubjectDAOImpl(); 14 15 public boolean saveSubject(Subject subject) { 16 String subjectTile = subject.getSubjectTitle(); 17 if(subjectDAO.findSubjectByTitle(subjectTile) == null){ //如果该试题标题不存在,允许添加 18 subjectDAO.addSubject(subject); 19 return true; 20 }else { 21 return false; 22 } 23 } 24 25 public PageResult querySubjectByPage(Page page) { 26 page = PageUtil.createPage(page.getEveryPage(), 27 subjectDAO.findSubjectCount(),page.getCurrentPage());//根据总记录数创建分页信息 28 List<Subject> list = subjectDAO.findSubjectByPage(page);//通过分页信息取得试题 29 PageResult result = new PageResult(page,list);//封装分页信息和记录信息,返回给调用处 30 return result; 31 } 32 33 public Subject showSubjectParticular(int subjectID) { 34 return subjectDAO.findSubjectByID(subjectID); 35 } 36 37 public void updateSubject(Subject subject) { 38 subjectDAO.updateSubject(subject); 39 } 40 41 public void deleteSubject(int subjectID) { 42 subjectDAO.deleteSubject(subjectID); 43 } 44 45 public PageResult likeQueryBySubjectTitle(String subjectTitle, Page page) { 46 page = PageUtil.createPage(page.getEveryPage(), 47 subjectDAO.findLinkQueryCount(subjectTitle),page.getCurrentPage());//根据总记录数创建分页信息 48 List<Subject> list = subjectDAO.likeQueryByTitle(subjectTitle, page);//通过分页信息模糊查询试题 49 PageResult result = new PageResult(page,list);//封装分页信息和记录信息,返回给调用处 50 return result; 51 } 52 53 public List<Subject> randomFindSubject(int number) { 54 return subjectDAO.randomFindSubject(number); 55 } 56 57 public int accountResult(List<Integer> subjectIDs, 58 List<String> studentAnswers) { 59 int GeneralPoint = 0;//总分 60 for(int i = 0; i < subjectIDs.size(); i++) { 61 String rightAnswer = subjectDAO. 62 findSubjectByID(subjectIDs.get(i)).getSubjectAnswer();//得到正确答案,通过试题ID 63 if(rightAnswer.equals(studentAnswers.get(i))) { 64 GeneralPoint += 5;//加5分 65 } 66 } 67 return GeneralPoint; 68 } 69 }
5.
1 package com.sanqing.service; 2 3 public interface TeacherService { 4 //判断是否为合法老师,从而决定是否允许登录 5 public boolean allowLogin(String teacherID,String password); 6 }
6.
1 package com.sanqing.service; 2 3 import com.sanqing.dao.TeacherDAO; 4 import com.sanqing.dao.TeacherDAOImpl; 5 import com.sanqing.po.Teacher; 6 7 public class TeacherServiceImpl implements TeacherService{ 8 private TeacherDAO teacherDAO = new TeacherDAOImpl(); 9 10 public boolean allowLogin(String teacherID, String password) { 11 Teacher teacher = teacherDAO.findByTeacherID(teacherID); 12 if(teacher == null) {//判断是否存在该ID的教师 13 return false; 14 }else { 15 if(password.equals(teacher.getPassword())) {//判断密码是否相同 16 return true; 17 }else{ 18 return false; 19 } 20 } 21 } 22 }
You can do anything you set your mind to, man!