⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 examserviceimpl.java

📁 基于J2EE的在线考试系统
💻 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 + -