2022首次更文挑战第25天 | 考试系统的开发03

157 阅读2分钟

「这是我参与2022首次更文挑战的第25天,活动详情查看:2022首次更文挑战

上一篇主要讲了question的列表功能,列出所有的题目,这次会对question类的功能进行拓展,增加新的功能。

判断执行的试卷下有无题目

paper里面包含了question,先有paper再有question,但是我们要怎么样才能知道paper里面有没有question,也就是试卷下面有没有题目?

先传入paperId,从question表查询->query.list(),如果查询出来有数据size()>0,那么就可以判断试卷下面是有题目的。

public boolean existQuestionByPaperId(String paperId)throws Exception{
	Session session=HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();
	Query query=session.createQuery("from Question as q where q.paper.id=:paperId");
	query.setString("paperId", paperId);
	@SuppressWarnings("unchecked")
	List<Student> studentList=(List<Student>)query.list();
	session.getTransaction().commit();
	if(studentList.size()>0){
		return true;
	}else{
		return false;
	}
}

在这里插入图片描述

查询试题记录数

题目的数量很重要,方便在出试卷的时候,从总数里面,抽取几道题目,做成试卷。 select count(*) from t_question直接查询出试题的数量。

public int questionCount(Question s_question)throws Exception{
	Session session=HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();
	StringBuffer sql=new StringBuffer("select count(*) from t_question");
	if(StringUtil.isNotEmpty(s_question.getSubject())){
		sql.append(" and subject like '%"+s_question.getSubject()+"%'");
	}
	Query query=session.createSQLQuery(sql.toString().replaceFirst("and", "where"));
	int count=((BigInteger)query.uniqueResult()).intValue();
	session.getTransaction().commit();
	return count;
}

在这里插入图片描述

计算/添加考试成绩

1、request.getParameterMap()获得考生id,试卷id,以及试题内容(答题以及答案),用Map<String, String[]>数据结构接收。

2、while (it2.hasNext())循环判断是否有值,有值进入判断体,没值跳出判断体。

3、entry.getKey();拿到考生id,试卷id entry.getValue()拿到试题内容(答题以及答案)

4、判断是否单选题if(keyStr.split("-")[1].equals("r")),如果是单选题,进行分数统计。

5、如果是多选题,拿到考生答题所做出来的答案,value=value.substring(0,value.length()-1);可能有多个。

6、exam.setSingleScore(singleScore); exam.setMoreScore(moreScore); exam.setScore(totalScore);设置单选题得分,多选题得分,以及总分。

public String add()throws Exception{
	Map<String, String[]> keyMap = new HashMap<String, String[]>();
       keyMap = request.getParameterMap();
       Iterator<Entry<String,String[]>> it2 = keyMap.entrySet().iterator();
       int totalScore=0;
       int singleScore=0;
       int moreScore=0;
       while (it2.hasNext()) {
           Entry<String, String[]> entry = it2.next();  
           String keyStr=entry.getKey();
           String values[]=entry.getValue();
           String key;
           String value="";
           if(keyStr.equals("exam.student.id")||keyStr.equals("exam.paper.id")){
           	continue;
           }
           if(keyStr.split("-")[1].equals("r")){  // 单选
           	key=keyStr.split("-")[2];
           	value=values[0];
           	singleScore+=this.calScore(key, value, "1");
           }else{  // 多选
           	key=keyStr.split("-")[2];
           	for(String s:values){
           		value+=s+",";
           	}
           	value=value.substring(0,value.length()-1);
           	moreScore+=this.calScore(key, value, "2");
           }
       }
       totalScore=singleScore+moreScore;
       exam.setSingleScore(singleScore);
       exam.setMoreScore(moreScore);
       exam.setScore(totalScore);
       exam.setExamDate(new Date());
       examDao.saveExam(exam);
	mainPage="exam/examResult.jsp";
	return SUCCESS;
}