第11講 考試系統之後臺服務03讀取題庫文件

  1. 創建 題目 實體
    題目格式如下:
@answer=1,score=5,level=1
下列屬於不合法Java標識符的是()。
_mem
12a
M12
$12

即:題目以@符號開始,answer爲答案,score爲題目得分,level爲難度
接下一行爲題幹,再下面4行爲選項。
因此,在包 com.zjipc.kaoshi.entity 下新建實體類 Question.java,代碼如下:

package com.zjipc.kaoshi.entity;

import java.util.ArrayList;
import java.util.List;

public class Question {
	public static final int LEVEL1 = 1;
	public static final int LEVEL2 = 2;
	public static final int LEVEL3 = 3;
	public static final int LEVEL4 = 4;
	public static final int LEVEL5 = 5;
	public static final int LEVEL6 = 6;
	public static final int LEVEL7 = 7;
	public static final int LEVEL8 = 8;
	public static final int LEVEL9 = 9;
	public static final int LEVEL10 = 10;

	public static final int SINGLE_SELECTION = 0;
	public static final int MULTI_SELECTION = 1;

	private int id;
	private String title;// 題幹
	private List<String> options = new ArrayList<String>();// 若干選項
	private List<Integer> answers = new ArrayList<Integer>();// 正確答案
	private int score;
	private int level;
	private int type; // 類型: 單選 SINGLE_SELECTION /多選 MULTI_SELECTION

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<String> getOptions() {
		return options;
	}

	public void setOptions(List<String> options) {
		this.options = options;
	}

	public List<Integer> getAnswers() {
		return answers;
	}

	public void setAnswers(List<Integer> answers) {
		this.answers = answers;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	// 打印題目
	public String toString() {
		StringBuffer sb = new StringBuffer(title + "\n");
		for (int i = 0; i < options.size(); i++) {
			sb.append((char) (i + 'A') + "." + options.get(i) + "\n");
		}
		sb.append("\n");
		return sb.toString();
	}
}

  1. 完善 EntityContext,以加載題庫
    代碼如下:
package com.zjipc.kaoshi.entity;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zjipc.kaoshi.util.Config;

public class EntityContext {

	public static void main(String[] args) {
		EntityContext context = new EntityContext();
	}

	public EntityContext() {
		config = new Config("client.properties");
		loadUsers(config.getString("UserFile"));
		printUsers();
		loadQuestions(config.getString("QuestionFile"));
		printQuestions();
	}

	private Config config; // 配置文件

	// 存儲用戶信息的HashMap
	private Map<Integer, User> users = new HashMap<>();
	private Map<Integer, List<Question>> questions = new HashMap<>();

	private void loadUsers(String file) {
		// 開始讀取file的內容
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
			String line;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				if ("".equals(line) || line.startsWith("#")) {
					continue;
				}
				User user = parseUser(line);
				users.put(user.getId(), user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public User findUserById(int id) {
		return users.get(id);
	}

	private User parseUser(String line) {
		String[] data = line.split(":");
		User user = new User();
		user.setId(Integer.parseInt(data[0]));
		user.setName(data[1]);
		user.setPwd(data[2]);
		user.setTel(data[3]);
		user.setEmail(data[4]);
		return user;
	}

	private void loadQuestions(String file) {
		// 開始讀取file的內容
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
			String line;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				if (line.startsWith("@")) {
					Question queston = parseQuestion(line, reader.readLine(), reader.readLine(), reader.readLine(),
							reader.readLine(), reader.readLine());
					addQuestion(queston);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Question parseQuestion(String fields, String title, String opt0, String opt1, String opt2, String opt3) {
		Question question = new Question();
		String[] data = fields.split("[@,][a-z]+=");
		question.setAnswers(parseAnswers(data[1]));
		question.setScore(Integer.parseInt(data[2]));
		question.setLevel(Integer.parseInt(data[3]));
		question.setTitle(title);
		List<String> options = new ArrayList<>();
		options.add(opt0);
		options.add(opt1);
		options.add(opt2);
		options.add(opt3);
		question.setOptions(options);
		question.setType(question.getAnswers().size() == 1 ? Question.SINGLE_SELECTION : Question.MULTI_SELECTION);
		return question;
	}

	private List<Integer> parseAnswers(String answers) {
		List<Integer> list = new ArrayList<>();
		String[] data = answers.split("/");
		for (String s : data) {
			list.add(Integer.parseInt(s));
		}
		return list;
	}

	private void addQuestion(Question queston) {
		List<Question> list = questions.get(queston.getLevel());
		if (list == null) {
			list = new ArrayList<>();
			questions.put(queston.getLevel(), list);
		}
		list.add(queston);
	}

	public int getTimeLimit() {
		return config.getInt("TimeLimit");
	}

	public String getTitle() {
		return config.getString("PaperTitle");
	}

	private void printUsers() {
		for (Map.Entry<Integer, User> user : users.entrySet()) {
			System.out.println(user.getValue());
		}
	}

	private void printQuestions() {
		for (Map.Entry<Integer, List<Question>> list : questions.entrySet()) {
			System.out.println("------- 難度 == " + list.getKey() + "---------");
			for (Question question : list.getValue()) {
				System.out.println(question);
			}
		}
	}
}

  1. 運行代碼,即可看到運行結果。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章