java代码在线测试_Java实现在线测评考试系统代码

该代码段展示了如何在服务实现类中利用EntityContext查询用户并进行登录验证,同时结合Md5Utils进行密码加密比对。此外,还详细描述了如何构建随机试卷,包括设置时间限制、题目数量以及获取试题信息的方法。
摘要由CSDN通过智能技术生成

package service;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import util.Config;

import util.Md5Utils;

import entity.EntityContext;

import entity.ExamInfo;

import entity.Question;

import entity.QuestionInfo;

import entity.User;

import exception.IdOrPasswordException;

public class ExamServiceImpl implements ExamService {

private EntityContext entityContext;

private List paper = new ArrayList();

private Config config;

private User loginUser;

public List getPaper() {return paper;

}

public void setPaper(List paper) {

this.paper = paper;

}

public ExamServiceImpl(EntityContext entityContext, Config config) {

super();

this.entityContext = entityContext;

this.config = config;

}

public ExamServiceImpl(EntityContext entityContext) {

super();

this.entityContext = entityContext;

}

@Override

public User login(int id, String password) throws IdOrPasswordException {

loginUser = entityContext.findUserById(id);

if (loginUser == null) {

throw new IdOrPasswordException("鏃犳鐢ㄦ埛!");

}

if (loginUser.getPassword().equals(Md5Utils.md5(password))) {

return loginUser;

}

throw new IdOrPasswordException("瀵嗙爜閿欒!");

}

@Override

public ExamInfo start() {

buildPaper();

ExamInfo examInfo = new ExamInfo();

examInfo.setUser(loginUser);

examInfo.setTimeLimit(config.getInt("TimeLimit"));

examInfo.setExamTitle(config.getString("PaperTitle"));

examInfo.setQuestionNumber(config.getInt("QuestionNumber"));

return examInfo;

}

private void buildPaper() {

int i = 0;

Random random = new Random();

for (int level = Question.LEVEL1; level <= Question.LEVEL10; level++) {

List list = entityContext.getQuestions(level);

Question q1 = list.remove(random.nextInt(list.size()));

Question q2 = list.remove(random.nextInt(list.size()));

paper.add(new QuestionInfo(++i, q1));

paper.add(new QuestionInfo(++i, q2));

}

}

@Override

public QuestionInfo getQuestionInfo(int index) {

return paper.get(index - 1);

}

@Override

public void sendUserAnswers(int questionIndex, List answers) {

QuestionInfo questionInfo = paper.get(questionIndex - 1);

questionInfo.setUserAnswers(new ArrayList(answers));

}

@Override

public int getTotalSocre() {

int score = 0;

for (QuestionInfo questionInfo : paper) {

if (questionInfo.getUserAnswers().equals(

questionInfo.getQuestion().getAnswers())) {

score += questionInfo.getQuestion().getScore();

}

}

return score;

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值