2014-03-25 285 views
-1

好的,我制作了一个Java程序来测试我的冷战历史日期,并且我想用一个简单的框架来扩展它以涵盖所有主题。我正在考虑用不同的主题(本身就是枚举)来枚举枚举。我知道我可以制作一个带有JOptionPane的图形用户界面来进行选择,但我想尽可能使其动态变化(因为不必硬编码选项)。这是我到目前为止:枚举的枚举[JAVA]

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

你在找什么样的答案?我不知道你在问什么 – user3334690

+0

有很多代码要通过这里。您是否遇到特定的问题? –

+0

枚举的枚举?听起来有点奇怪,那么可序列化的列表'? – 2014-03-25 19:11:07

回答

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"...)) 
+0

这将是一个可行的选择,我会研究它,如果我不能想到别的。 – Injustice

0

你会更好使用散列图的哈希表。这会给你灵活性,你正在寻找。

+0

那么这将如何工作?地图<主题,地图<问题,答案>>? – Injustice

+0

你是对的@Injustice这正是它的工作原理,使用这样的地图(或字典)地图的美妙之处在于,它类似于Json结构,可以很容易地转换为一个。 – remudada