单机考试管理软件(7)

在这里插入图片描述
Exam.java


public class Exam {
    public static void main(String[] args) {

        ExamView EV = new ExamView() ;
        EV.testExam();
    }
}

ExamView.java

import java.util.Scanner;

public class ExamView {

    private ItemService IS = new ItemService() ;
    private char[] answer ;

    public ExamView(){
        answer = new char[IS.TOTAL_ITEMS] ;
        for (int i=0; i < answer.length; i++){
            answer[i] = ' ' ;
        }
    }

    public char getUserAction(){
        char[] validKey = {'1', '2', '3', 'A', 'B', 'C', 'D', 'F', 'N', 'P', 'Y'} ;
        char key = 0 ;

        Scanner scanner = new Scanner(System.in) ;

        while (scanner.hasNext()){
            String str = scanner.next() ;
            if (str.length() != 1){
                continue;
            }
            str = str.toUpperCase() ;
            key = str.charAt(0) ;
            for (char k : validKey){
                if (k == key){
                    return key ;
                }
            }
        }
        return key ;
    }

    public void displayItem(int no){
        System.out.println(IS.getItem(no).getQuestion());
        String[] options = IS.getItem(no).getOptions() ;
        for (String option : options){
            System.out.println(option);
        }
    }

    public void testExam(){
        int curItem = 1 ;

        while (true){
            displayItem(curItem);
            System.out.println("请选择正确答案(p-上一题 n-下一题)");

            char key = getUserAction() ;
            switch (key){
                case 'A':
                case 'B':
                case 'C':
                case 'D':
                    answer[curItem - 1] = key ;
                case 'N':
                    if (curItem < IS.TOTAL_ITEMS){
                        ++curItem ;
                    } else {
                        System.out.println("已经到达最后一题了");
                    }
                    break;
                case 'P':
                    if (curItem > 1){
                        --curItem ;
                    } else {
                        System.out.println("已经到达第一题");
                    }
                    break;
                case 'F':
                    break;
                default:
                        System.out.println("输入无效");
            }
            IS.saveAnswer(answer);
        }
    }
}

Item.java

import java.util.Arrays;

public class Item {
    private String question ;
    private String[] options ;
    private char answer ;

    public Item(String question, String[] options, char answer){
        this.question = question ;
        this.options = options ;
        this.answer = answer ;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String[] getOptions() {
        return options;
    }

    public void setOptions(String[] options) {
        this.options = options;
    }

    public char getAnswer() {
        return answer;
    }

    public void setAnswer(char answer) {
        this.answer = answer;
    }

    @Override
    public String toString() {
        return "Item{" +
                "question='" + question + '\'' +
                ", options=" + Arrays.toString(options) +
                ", answer=" + answer +
                '}';
    }
}

ItemService.java

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ItemService {
    private Item[] items ;  //题目数组

    private final String ITEM_FILENAME = "D:\\Project\\Springboot\\Bank\\src\\main\\java\\Items.txt" ;
    private final String ANSWER_FILENAME = "./answer.dat" ;   //文件输出位置
    private final int LINES_PER_ITEM = 6 ;
    public  int TOTAL_ITEMS ;  //题目数

    public ItemService(){
        List<String> list = readTextFile(ITEM_FILENAME) ;
        //计算得到题目的数量
        TOTAL_ITEMS = list.size() / LINES_PER_ITEM ;

        items = new Item[TOTAL_ITEMS] ;
        //  初始化题目数组
        for (int i=0; i< TOTAL_ITEMS; i++){
            String question = list.get(i * LINES_PER_ITEM) ;
            String[] options = {
                    list.get(i * LINES_PER_ITEM + 1) ,
                    list.get(i * LINES_PER_ITEM + 2) ,
                    list.get(i * LINES_PER_ITEM + 3) ,
                    list.get(i * LINES_PER_ITEM + 4)
            } ;
            char answer = list.get(i * LINES_PER_ITEM + 5).charAt(0) ;
            items[i] = new Item(question, options, answer) ;
        }

    }

    private List<String> readTextFile(String filename){
        FileReader fr = null ;
        BufferedReader br = null ;
        List<String> content = new ArrayList<>() ;

        try {
            fr = new FileReader(filename) ;
            br = new BufferedReader(fr) ;

            String line ;
            while ((line = br.readLine()) != null){
                if (!line.trim().equals("")) content.add(line) ;
            }

        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
            if (fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
        return content;
    }

    public Item getItem(int no){
        if (no <= 0 || no > TOTAL_ITEMS){
            return null ;
        }
        return items[no - 1] ;
    }

    public void saveAnswer(char[] answer){
        FileOutputStream fos = null ;
        ObjectOutputStream oos = null ;

        try {
            fos = new FileOutputStream(ANSWER_FILENAME) ;
            oos = new ObjectOutputStream(fos) ;

            oos.writeObject(answer);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值