forked from pnl4utw52/Math_Learning
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.0 KiB
116 lines
3.0 KiB
package Service;
|
|
|
|
import Base.Exam_result;
|
|
import Base.Question;
|
|
import Generator.Generate_paper;
|
|
|
|
import java.util.*;
|
|
|
|
public class Exam_service {
|
|
private String id;
|
|
private String type;
|
|
private ArrayList<Question> paper;
|
|
private int now_index;
|
|
private Date start_time;
|
|
private Date end_time;
|
|
private Map<Integer, Integer> user_answers;
|
|
|
|
public Exam_service(int num,String type,String id){
|
|
this.id=id;
|
|
this.type=type;
|
|
paper= Generate_paper.g_paper(num,type,id);
|
|
now_index=0;
|
|
this.start_time = new Date();
|
|
this.user_answers = new HashMap<>();
|
|
}
|
|
|
|
public ArrayList<Question> get_paper() { return paper; }
|
|
public int get_now_index() { return now_index; }
|
|
public Date get_start_time() { return start_time; }
|
|
public Date get_end_time() { return end_time; }
|
|
public Map<Integer, Integer> get_user_answers() {return user_answers;}
|
|
|
|
public void set_now_index(int i){
|
|
if (i==1 && now_index<paper.size()-1){
|
|
now_index+=i;
|
|
}
|
|
else if (i==-1 && now_index>0){
|
|
now_index+=i;
|
|
}
|
|
else if (i==0){
|
|
now_index=0;
|
|
}
|
|
}
|
|
|
|
public Question get_now_question() {
|
|
if (now_index < paper.size()) {
|
|
return paper.get(now_index);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public boolean next_one(int answer_index) {
|
|
if (now_index < paper.size()) {
|
|
user_answers.put(now_index, answer_index);
|
|
|
|
if (now_index < paper.size() - 1) {
|
|
now_index++;
|
|
return true;
|
|
} else {
|
|
end_time = new Date();
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean pre_one() {
|
|
if (now_index > 0) {
|
|
now_index--;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean change_answer(int index,int choice){
|
|
if (user_answers.containsKey(index)){
|
|
user_answers.put(index,choice);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public Integer get_user_answer(int question_index) {
|
|
return user_answers.get(question_index);
|
|
}
|
|
|
|
public boolean check_finished(){
|
|
for (int i=0;i< paper.size();i++){
|
|
if (user_answers.get(i)==-1){
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public Exam_result calculate_result(){
|
|
int correct = 0;
|
|
List<Integer> wrong = new ArrayList<>();
|
|
|
|
for (int i = 0; i < paper.size(); i++) {
|
|
Integer userAnswer = user_answers.get(i);
|
|
if (userAnswer != null && paper.get(i).getOptions(userAnswer).equals(paper.get(i).getAnswer())) {
|
|
correct++;
|
|
} else {
|
|
wrong.add(i);
|
|
}
|
|
}
|
|
|
|
double score = (double) correct / paper.size() * 100;
|
|
long duration = (end_time.getTime() - start_time.getTime()) / 1000; // 秒
|
|
|
|
return new Exam_result(type, paper.size(), correct,
|
|
score, duration, wrong);
|
|
}
|
|
}
|