?? examserviceimpl.java
字號:
package org.yeeku.service.impl;
import org.yeeku.dao.*;
import org.yeeku.model.*;
import java.util.List;
import org.yeeku.exception.ExamException;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class ExamServiceImpl implements org.yeeku.service.ExamService
{
private ExamUserDao examUserDao;
private StudentDao studentDao;
private ExamTypeDao examTypeDao;
private QuestionDao questionDao;
public void setExamUserDao(ExamUserDao examUserDao)
{
this.examUserDao = examUserDao;
}
public void setStudentDao(StudentDao studentDao)
{
this.studentDao = studentDao;
}
public void setExamTypeDao(ExamTypeDao examTypeDao)
{
this.examTypeDao = examTypeDao;
}
public void setQuestionDao(QuestionDao questionDao)
{
this.questionDao = questionDao;
}
/**
* 增加一個學生實例,對應為增加一條學生的記錄
* @param stuNumber 學生學號。
* @param name 學生學號。
* @param classname 學生學號。
* @param humanId 學生學號。
* @param email 學生學號。
* @param address 學生地址。
* @param phone 學生電話。
* @return 新增學生的主鍵
*/
public int addStudent(String stuNumber , String name ,
String classname , String humanId , String email ,
String address , String phone) throws ExamException
{
try
{
Student student = new Student(stuNumber, name, classname, humanId,
email, address, phone);
studentDao.save(student);
return student.getId();
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("添加學生時出現異常,請重試!");
}
}
/**
* 根據學生I刪除學生
* @param id 需要刪除的學生的主鍵。
* @return 刪除學生的記錄數
*/
public void deleteStudent(int id) throws ExamException
{
try
{
studentDao.delete(id);
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("刪除學生時出現異常,請重試!");
}
}
/**
* 根據頁碼列出學生列表
* @param pageNo 頁碼
* return 列出的學生列表
*/
public List<Student> listStudent(int pageNo) throws ExamException
{
try
{
return studentDao.findAllByPage(pageNo , STUDENT_PAGE_SIZE);
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("列出學生時出現異常,請重試!");
}
}
/**
* 增加一個試題,增加一條試題記錄
* @param quTitle 試題題目。
* @param quHard 試題難度。
* @param quScore 試題分數。
* @param quAnswer 試題答案。
* @param quType 試題類型。
* @param selectOption 試題選項。
* @param examTypeId 試題對應的考試類型
* @return 新增試題的主鍵
*/
public int addQuestion(String quTitle,String quHard,String quScore,
String quAnswer,String quType,String selectOption , int examTypeId) throws ExamException
{
try
{
ExamType examType = examTypeDao.get(examTypeId);
if (examType == null)
{
throw new ExamException("不存在該考試類型,請重新選擇");
}
Question question = new Question(quTitle,quHard, quScore,
quAnswer, quType, selectOption, examType);
questionDao.save(question);
return question.getId();
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("添加學生時出現異常,請重試!");
}
}
/**
* 根據試題ID刪除試題
* @param id 需要刪除的試題的主鍵。
*/
public void deleteQuestion(int id) throws ExamException
{
try
{
questionDao.delete(id);
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("刪除試題時出現異常,請重試!");
}
}
/**
* 新增考試類型
* @param testName 新增的考試名稱。
* @param testTime 新增的考試時間
* @return 新增的考試類型的ID
*/
public int addExamType(String testName , String testTime) throws ExamException
{
ExamType et = new ExamType(testName , testTime);
examTypeDao.save(et);
return et.getId();
}
/**
* 獲取所有考試類型
* @return 所有考試類型
*/
public List<ExamType> getAllExamType() throws ExamException
{
return examTypeDao.findAll();
}
/**
* 根據考試類型ID刪除考試類型
* @param id 需要刪除的考試類型的主鍵。
*/
public void deleteExamType(int id) throws ExamException
{
try
{
examTypeDao.delete(id);
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("刪除考試類型時出現異常,請重試!");
}
}
/**
* 根據頁碼列出試題列表
* @param pageNo 頁碼
* return 列出的試題列表
*/
public List<Question> listQuestion(int pageNo) throws ExamException
{
try
{
return questionDao.findAllByPage(pageNo , QUESTION_PAGE_SIZE);
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("列出考試試題時出現異常,請重試!");
}
}
/**
* 根據用戶名和密碼判斷用戶是否可以成功登錄
* @param user 登錄用的用戶名
* @param pass 登錄用的密碼
* @return 是否可以成功登錄
*/
public boolean adminLogin(String user , String pass) throws ExamException
{
try
{
List result = examUserDao.findExamUserByNameAndPass(user, pass);
if (result != null && result.size() > 0)
{
return true;
}
return false;
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("系統管理員登錄出現異常,請重試!");
}
}
public int getStudentCount()throws ExamException
{
try
{
return (int)studentDao.getStudentCount();
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("獲取學生數量時出現異常,請重試!");
}
}
/**
* 獲取試題數量
* @return 試題的個數
*/
public int getQuestionCount()throws ExamException
{
try
{
return (int)questionDao.getQuestionCount();
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("獲取試題數量時出現異常,請重試!");
}
}
/**
* 根據每頁記錄數,總記錄數獲取總頁數
* @param count 總記錄數
* @param pageSize 每頁顯示的記錄數
* @return 計算得到的總頁數
*/
public int getPageCount(int count , int pageSize)
{
return (count + pageSize - 1 ) / pageSize;
}
/**
* 判斷學生是否可以成功登錄。
* @param name 登錄用的學生姓名
* @param stuNumber 登錄用的學號
*/
public String studentLogin(String name , String stuNumber)throws ExamException
{
try
{
List result = studentDao.findStudentByNameAndStuNumber(name , stuNumber);
if (result != null && result.size() > 0)
{
return name;
}
return null;
}
catch (Exception e)
{
throw new ExamException("學生登錄出現異常,請重試!");
}
}
/**
* 根據考試類型ID獲取下一個試題
* @param alreadys 已經回答的試題ID
* @param examTypeId 考試類型ID
* return 該考試類型的下一個試題
*/
public Question getNextQuestion(List<Integer> alreadys , int examTypeId) throws ExamException
{
try
{
ExamType examType = examTypeDao.get(examTypeId);
if (examType == null)
{
throw new ExamException("不存在該考試類型,請重新選擇");
}
int maxId = questionDao.getMaxId();
REPEAT_TRY:
while(true)
{
int randomId = (int)Math.round(Math.random() * maxId) + 1;
//如果已答題的數組不為空,判斷獲取的題是否已在已答題數組中
if (alreadys != null)
{
for (int alreadyId :alreadys)
{
if (alreadyId == randomId)
{
continue REPEAT_TRY;
}
}
}
Question question = null;
//如果獲取題目出現異常,重新開始循環來獲取下一題.
try
{
question = questionDao.findQuestionByExamType(randomId , examType );
}
catch (Exception e)
{
continue;
}
return question;
}
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("獲取下一道試題時出現異常,請重試!");
}
}
/**
* 根據試題ID獲取實體
* @param id 試題ID
* return 該ID對應的試題
*/
public Question getQuestionById(int id)throws ExamException
{
try
{
return questionDao.get(id);
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("根據ID獲取試題出錯,請重試!");
}
}
/**
* 根據考試類型ID獲取考試類型
* @param typeId 考試類型ID
* return 該ID對應的考試類型名
*/
public String getExamTypeName(int typeId)throws ExamException
{
try
{
return examTypeDao.get(typeId).getExamName();
}
catch (Exception e)
{
e.printStackTrace();
throw new ExamException("獲取考試類型出現異常,請重試!");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -