枚举枚举[JAVA]

时间:2014-03-25 19:05:07

标签: java list generics enums enumeration

好的,所以我制作了一个Java程序来测试我的冷战历史日期,我想用一个简单的框架来扩展它以涵盖所有主题。我正在考虑制作一个包含不同主题的枚举(它们本身就是枚举词)。我知道我可以用一个JOptionPane来制作一个GUI来选择或者其他东西但是我想让它尽可能地动态(因为不需要硬编码选项)。这就是我到目前为止所拥有的:

public class Main {
    public List<History> list = new ArrayList<>();
    public List<History> generated = new ArrayList<>();
    private final Random r = new Random();
    private final MainFrame frame = new MainFrame(nextQuestion(), Main.this);
    private int correctInFirstGo = History.values().length;

    public static void main(String[] args) {
        new Main();
    }
    public Main() {
        SwingUtilities.invokeLater(() ->frame.setVisible(true));
    }

    public void setCorrect(boolean b, int attempts) {
        if (list.size() == History.values().length) {
            JOptionPane.showMessageDialog(frame, "You got " + correctInFirstGo + "/" + History.values().length);
            frame.dispose();
            System.out.println(list);
            System.out.println(generated);
            return;
        }
        loop:
        if (b) {
            final History h = nextQuestion();
            if (generated.contains(h) && !list.contains(h)) {
                System.out.println(h);
                frame.setQuestion(h);
            } else {
                break loop;
            }
        } else if (attempts != 1) {
            correctInFirstGo--;
        }
    }

    private History nextQuestion() {
        History[] values = History.values();
        History h = values[r.nextInt(values.length)];
        if (list.contains(h)) {
            return nextQuestion();
        }
        generated.add(h);
        return h;
    }
}


public class MainFrame extends JFrame{

    private Main m;
    private History h;
    private JLabel questionLabel;
    private JLabel verdictLabel;
    private JTextField answerField;
    private JButton checkButton;
    private JLabel questionNumber;
    private JLabel attemptLabel;
    private int attempt = 1;

    public MainFrame(History h, Main m) {
        System.out.println(h);
        this.h = h;
        this.m = m;
        initComponents();
    }

    private void initComponents() {
        questionLabel = new JLabel("", SwingConstants.CENTER);
        answerField = new JTextField();
        checkButton = new JButton();
        verdictLabel = new JLabel("", SwingConstants.CENTER);
        questionNumber = new JLabel("");
        attemptLabel = new JLabel("" + attempt);

        setTitle("History Test");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        setQuestion(h);
        checkButton.setText("Check");
        verdictLabel.setText("");

        questionLabel.setBounds(5, 5, 345, 25);
        answerField.setBounds(148, 35, 55, 25);
        checkButton.setBounds(138, 65, 75, 25);
        verdictLabel.setBounds(5, 90, 345, 25);
        questionNumber.setBounds(5, 90, 50, 25);
        attemptLabel.setBounds(320, 90, 15, 25);

        answerField.addActionListener(ae -> checkSolution());
        checkButton.addActionListener(ae -> checkSolution());

        contentPane.add(questionLabel);
        contentPane.add(answerField);
        contentPane.add(checkButton);
        contentPane.add(verdictLabel);
        contentPane.add(questionNumber);
        contentPane.add(attemptLabel);

        setResizable(false);
        setLocationRelativeTo(null);
        pack();
        setSize(350, 140);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void setQuestion(History h) {
        this.h = h;
        String question = "What year was the " + h + "?";
        questionLabel.setText(question);
        answerField.setText("");
        m.list.add(h);
        questionNumber.setText("" + m.generated.size() + "/" + History.values().length);
    }

    private void checkSolution() {
        answerField.requestFocus();
        if (answerField.getText().equals(h.answer())) {
            verdictLabel.setText("Correct!");
            m.setCorrect(true, attempt);
            attemptLabel.setText((attempt = 1) + "");
        } else {
            verdictLabel.setText("Try again!");
            m.setCorrect(false, attempt);
            attempt++;
            attemptLabel.setText(attempt + "");
        }
    }
}

public enum History{
    // my constants here

    private String answer;
    private String toString;
    private final String[] ignoreCapitals = {"of", "the", "built"};

    History(String answer) {
        this.answer = answer;
    }

    public String answer() {
        return answer;
    }

    @Override
    public String toString() {
        if (toString != null) { return toString; }
        final String name = name();
        final String[] words = name.replace('$', '\'').split("_+");
        final StringBuilder result = new StringBuilder(name.length());
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            final String newWord = performCapitalization(word, i);
                if (newWord != null) {
                result.append(newWord);
            }
            if(i < words.length - 1){
                result.append(' ');
            }
        }

        return (toString = result.toString());
    }

    private String performCapitalization(final String oldWord, final int index) {
        if(oldWord == null || oldWord.length() == 0){
            return null;
        }
        boolean ignore = false;
        if (index != 0) {
            for (final String str : ignoreCapitals) {
                if(str.equalsIgnoreCase(oldWord)){
                    ignore = true;
                    break;
                }
            }
        }
        final String newWord = oldWord.toLowerCase();
        if(ignore){
            return oldWord.toLowerCase();
        }
        return Character.toUpperCase(newWord.charAt(0)) + newWord.substring(1);
    }
}

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

首先,我考虑将问题存储在一个文件中(xml / json似乎是自然选择)。

如果您想在代码中创建问题的好方法,为什么不创建一个流畅的api库?因此,您正在寻找以下内容:

QuestionSet = new QuestionSet("QS1")
                   .addTopic( new Topic("topic 1")
                                  .addQuestion( new Question("text"...)
                                  .addQuestion( new Question("text"...) )
                   .addTopic( new Topic("topic 2")
                                  .addQuestion( new Question("text"...) )

答案 1 :(得分:0)

最好使用Hashmap的Hashmap。这将为您提供所需的灵活性。

相关问题