This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslationMaster.java
258 lines (221 loc) · 10.5 KB
/
TranslationMaster.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TranslationMaster extends JFrame {
private static final int GAME_ITERATIONS = 10;
private int currentRound = 0;
private int score = 0;
private JLabel wordLabel;
private JButton[] optionButtons;
private JLabel scoreLabel;
private JButton restartButton;
String url, username, password;
public TranslationMaster() {
super("Pick the appropriate translation");
setExtendedState(JFrame.MAXIMIZED_BOTH);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
getContentPane().setBackground(new Color(230, 230, 230));
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.setBackground(new Color(50, 50, 50));
topPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 10, 20));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
wordLabel = new JLabel("", SwingConstants.CENTER);
wordLabel.setForeground(Color.WHITE);
wordLabel.setFont(new Font("Arial", Font.BOLD, 28));
topPanel.add(wordLabel, BorderLayout.CENTER);
restartButton = new JButton("Restart");
restartButton.setFont(new Font("Arial", Font.PLAIN, 18));
restartButton.setVisible(false);
restartButton.addActionListener(new RestartButtonListener());
topPanel.add(restartButton, BorderLayout.SOUTH);
add(topPanel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(2, 2, 10, 10));
buttonPanel.setBackground(new Color(230, 230, 230));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 20, 20));
optionButtons = new JButton[4];
for (int i = 0; i < optionButtons.length; i++) {
optionButtons[i] = new JButton();
optionButtons[i].setFont(new Font("Arial", Font.PLAIN, 18));
optionButtons[i].setBackground(new Color(100, 100, 100));
optionButtons[i].setForeground(Color.WHITE);
optionButtons[i].addActionListener(new OptionButtonListener());
buttonPanel.add(optionButtons[i]);
}
add(buttonPanel, BorderLayout.CENTER);
scoreLabel = new JLabel("", SwingConstants.CENTER);
scoreLabel.setFont(new Font("Arial", Font.BOLD, 20));
scoreLabel.setForeground(Color.BLACK);
scoreLabel.setVisible(false);
add(scoreLabel, BorderLayout.SOUTH);
url = Main.getUrl();
username = Main.getUsername();
password = Main.getPassword();
createTableAndDataIfNeeded();
loadNextRound();
setVisible(true);
}
private void createTableAndDataIfNeeded() {
try (Connection c = DriverManager.getConnection(url, username, password)) {
if (!tableExists(c, "GERMAN_WORDS")) {
createGermanWordsTable(c);
addGermanWords(c);
} else {
if (getTotalRows(c) < GAME_ITERATIONS) {
addGermanWords(c);
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private boolean tableExists(Connection connection, String tableName) throws SQLException {
DatabaseMetaData meta = connection.getMetaData();
ResultSet resultSet = meta.getTables(null, null, tableName, new String[]{"TABLE"});
return resultSet.next();
}
private void createGermanWordsTable(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(
"CREATE TABLE GERMAN_WORDS (" +
"WORD VARCHAR2(50), " +
"MEANING VARCHAR2(50))"
);
}
}
private void addGermanWords(Connection connection) throws SQLException {
String[] words = {
"Haus", "Auto", "Baum", "Hund", "Katze",
"Tisch", "Stuhl", "Blume", "Wasser", "Apfel"
};
String[] meanings = {
"house", "car", "tree", "dog", "cat",
"table", "chair","flower", "water", "apple"
};
String query = "INSERT INTO GERMAN_WORDS (WORD, MEANING) VALUES (?, ?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
for (int i = 0; i < words.length; i++) {
preparedStatement.setString(1, words[i]);
preparedStatement.setString(2, meanings[i]);
preparedStatement.executeUpdate();
}
}
}
private int getTotalRows(Connection connection) throws SQLException {
String query = "SELECT COUNT(*) FROM GERMAN_WORDS";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
if (resultSet.next()) {
return resultSet.getInt(1);
}
return 0;
}
}
private void loadNextRound() {
if (currentRound < GAME_ITERATIONS) {
currentRound++;
try (Connection c = DriverManager.getConnection(url, username, password)) {
String germanWord = getRandomGermanWord(c);
wordLabel.setText("<html><div style='text-align: center;'>" + germanWord + "</div></html>");
List<String> meanings = getMeaningOptions(c, germanWord);
Collections.shuffle(meanings);
for (int i = 0; i < optionButtons.length; i++) {
optionButtons[i].setText(meanings.get(i));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
endGame();
}
}
private String getRandomGermanWord(Connection connection) throws SQLException {
String query = "SELECT WORD FROM GERMAN_WORDS ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 1 ROWS ONLY";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return resultSet.getString("WORD");
} else {
throw new SQLException("No German words found in the table.");
}
}
}
private List<String> getMeaningOptions(Connection connection, String germanWord) throws SQLException {
String query = "SELECT MEANING FROM GERMAN_WORDS WHERE WORD != ? ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 3 ROWS ONLY";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, germanWord);
ResultSet resultSet = preparedStatement.executeQuery();
List<String> meanings = new ArrayList<>();
meanings.add(getMeaningForWord(connection, germanWord));
while (resultSet.next()) {
meanings.add(resultSet.getString("MEANING"));
}
return meanings;
}
}
private String getMeaningForWord(Connection connection, String germanWord) throws SQLException {
String query = "SELECT MEANING FROM GERMAN_WORDS WHERE WORD = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, germanWord);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return resultSet.getString("MEANING");
} else {
throw new SQLException("Meaning not found for word: " + germanWord);
}
}
}
private class OptionButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String selectedMeaning = button.getText();
try (Connection c = DriverManager.getConnection(url, username, password)) {
String germanWord = wordLabel.getText().replaceAll("<.*?>", "");
String correctMeaning = getMeaningForWord(c, germanWord);
if (selectedMeaning.equals(correctMeaning)) {
score++;
JOptionPane.showMessageDialog(TranslationMaster.this, "Correct!", "Result", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(TranslationMaster.this, "Incorrect!", "Result", JOptionPane.ERROR_MESSAGE);
}
loadNextRound();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(TranslationMaster.this, "Error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private class RestartButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
restartGame();
}
}
private void restartGame() {
currentRound = 0;
score = 0;
scoreLabel.setVisible(false);
restartButton.setVisible(false);
for (JButton button : optionButtons) {
button.setEnabled(true);
}
loadNextRound();
}
private void endGame() {
JOptionPane.showMessageDialog(this, "Game Over! Your final score: " + score, "Game Over", JOptionPane.INFORMATION_MESSAGE);
scoreLabel.setText("Final Score: " + score);
scoreLabel.setVisible(true);
for (JButton button : optionButtons) {
button.setEnabled(false);
}
restartButton.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TranslationMaster::new);
}
}