[英]Enum of enums [JAVA]
好的,所以我制作了一個 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);
}
}
任何幫助,將不勝感激。
首先,我會考慮將問題存儲在一個文件中(xml/json 似乎是自然的選擇)。
如果您想要一種在代碼中創建問題的好方法,為什么不創建一個帶有 fluent 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"...) )
最好使用 Hashmap 的 Hashmap。 這將為您提供所需的靈活性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.