前言
我知道大家都很熟悉hashmap,并且有事没事都会new一个,但是hashmap的一些特性大家都是看了忘,忘了再记,今天这个例子可以帮助大家很好的记住。
场景
用户提交一张试卷答案到服务端,post报文可精简为
[{"question_id":"100001","answer":"A"},{"question_id":"100002","answer":"A"},{"question_id":"100003","answer":"A"},{"question_id":"100004","answer":"A"}]复制代码
提交地址采用restful风格
http://localhost:8080/exam/{试卷id}/answer复制代码
那么如何比对客户端传过来的题目就是这张试卷里的呢,假设用户伪造了试卷怎么办?
正常解决思路
- 得到试卷所有题目id的list
- 2层for循环比对题号和答案
- 判定分数
大概代码如下[他人代码]
//读取post题目for (MexamTestpaperQuestion mexamTestpaperQuestion : mexamTestpaperQuestions) { //通过考试试卷读取题目选项对象 MexamQuestionOption questionOption = mexamQuestionDao.findById(mexamTestpaperQuestion.getQuestionId()); map1.put("questionid", mexamTestpaperQuestion.getQuestionId()); map1.put("answer", mexamQuestionDao.findById(mexamTestpaperQuestion.getQuestionId()).getAnswer()); questionAnswerList.add(map1); //将每题分add到一个List}//遍历试卷内所有题目for (Map stringObjectMap : list) { //生成每题结果对象 mexamAnswerInfo = new MexamAnswerInfo(); mexamAnswerInfo.setAnswerId(answerId); mexamAnswerInfo.setId(id); mexamAnswerInfo.setQuestionId(questionid); mexamAnswerInfo.setResult(anwser); for (Map objectMap : questionAnswerList) { if (objectMap.get("questionid").equals(questionid)) { //比较答案 if (anwser.equals(objectMap.get("answer"))) { totalScore += questionOption.getScore(); mexamAnswerInfo.setIsfalse