Repository navigation
Expand file tree
/
Copy pathGradeWiz.java
More file actions
331 lines (268 loc) · 11.6 KB
/
Copy pathGradeWiz.java
File metadata and controls
331 lines (268 loc) · 11.6 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
private JFrame frame;
private JTextField numComponentsField;
private JTextField[] weightingFields;
private JTextField[] scoreFields;
private int numComponents;
private double[] weightings;
private double[] scores;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Main().createAndShowGUI());
}
private void createAndShowGUI() {
frame = new JFrame("GradeWiz ✔");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300); // Adjusted size for a smaller window
frame.setLocationRelativeTo(null);
frame.setLayout(new CardLayout());
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
createMenuBar();
showNumComponentsScreen();
frame.setVisible(true);
}
private void createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem aboutItem = new JMenuItem("About");
aboutItem.addActionListener(e -> showAboutDialog());
JMenuItem resetItem = new JMenuItem("Reset");
resetItem.addActionListener(e -> showNumComponentsScreen());
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(e -> System.exit(0));
fileMenu.add(aboutItem);
fileMenu.add(resetItem);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
}
private void showAboutDialog() {
// Define the content of the About dialog with a clickable link
String message = "<html><h1><b>GradeWiz ✔</b></h1>" +
"Java SE Version 1.0<br><br>"+
"A simple app that calculates a final module mark<br>" +
"based on component weightings and component marks.<br><br>" +
"© 2024 Ricki Angel<br>" +
"<a href='https://github.com/TechAngelX/GradeWiz-JavaSE.git'>Visit the GitHub repository</a><br>" +
"</html>";
// Create the About dialog with the message and set it to be clickable
JOptionPane.showMessageDialog(frame,
new JLabel(message) { // Use JLabel to display the HTML content
@Override
public void setText(String text) {
super.setText(text);
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // Change cursor to hand on hover
addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
try {
java.awt.Desktop.getDesktop().browse(java.net.URI.create("https://github.com/TechAngelX/GradeWiz-JavaSE.git"));
} catch (java.io.IOException ioException) {
ioException.printStackTrace(); // Handle the exception if the URL cannot be opened
}
}
});
}
},
"About",
JOptionPane.INFORMATION_MESSAGE
);
}
private void showNumComponentsScreen() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel label = new JLabel("Number of components (1-5):");
numComponentsField = new JTextField(2);
numComponentsField.setPreferredSize(new Dimension(50, 30));
JButton nextButton = new JButton("Next");
nextButton.addActionListener(e -> {
try {
numComponents = Integer.parseInt(numComponentsField.getText());
if (numComponents < 1 || numComponents > 5) {
throw new NumberFormatException();
}
weightings = new double[numComponents];
scores = new double[numComponents];
showWeightingScreen();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter a number of components between 1 and 5.");
}
});
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(label, gbc);
gbc.gridx = 1;
panel.add(numComponentsField, gbc);
gbc.gridy = 1;
gbc.gridx = 0;
gbc.gridwidth = 1;
panel.add(nextButton, gbc);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.revalidate();
frame.repaint();
}
private void showWeightingScreen() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
weightingFields = new JTextField[numComponents];
JLabel label = new JLabel("Enter % weightings for each component:");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
panel.add(label, gbc);
gbc.gridwidth = 1;
for (int i = 0; i < numComponents; i++) {
JLabel componentLabel = new JLabel("Component " + (i + 1) + ":");
JTextField weightingField = new JTextField(2);
weightingField.setPreferredSize(new Dimension(5, 30));
weightingFields[i] = weightingField;
JPanel weightingPanel = new JPanel(new BorderLayout());
weightingPanel.add(weightingField, BorderLayout.CENTER);
weightingPanel.add(new JLabel("%"), BorderLayout.EAST);
gbc.gridy = i + 1;
gbc.gridx = 0;
panel.add(componentLabel, gbc);
gbc.gridx = 1;
panel.add(weightingPanel, gbc);
}
JButton backButton = new JButton("Back");
backButton.addActionListener(e -> showNumComponentsScreen());
JButton nextButton = new JButton("Next");
nextButton.addActionListener(e -> {
try {
double totalWeight = 0;
for (int i = 0; i < numComponents; i++) {
weightings[i] = Double.parseDouble(weightingFields[i].getText());
if (weightings[i] < 0) throw new NumberFormatException();
totalWeight += weightings[i];
}
if (totalWeight != 100) {
throw new NumberFormatException();
}
showScoreScreen();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter valid weightings that sum up to 100%.");
}
});
gbc.gridy = numComponents + 1;
gbc.gridx = 0;
gbc.gridwidth = 1;
panel.add(backButton, gbc);
gbc.gridx = 1;
panel.add(nextButton, gbc);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.revalidate();
frame.repaint();
}
private void showScoreScreen() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
scoreFields = new JTextField[numComponents];
JLabel label = new JLabel("Enter scores for each component:");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
panel.add(label, gbc);
gbc.gridwidth = 1;
for (int i = 0; i < numComponents; i++) {
JLabel componentLabel = new JLabel("Component " + (i + 1) + " score:");
JTextField scoreField = new JTextField(5);
scoreField.setPreferredSize(new Dimension(60, 30));
scoreFields[i] = scoreField;
// Creating a JPanel to hold the JTextField and % JLabel
JPanel scorePanel = new JPanel(new BorderLayout());
scorePanel.add(scoreField, BorderLayout.CENTER);
scorePanel.add(new JLabel("%"), BorderLayout.EAST);
gbc.gridy = i + 1;
gbc.gridx = 0;
panel.add(componentLabel, gbc);
gbc.gridx = 1;
panel.add(scorePanel, gbc);
}
JButton backButton = new JButton("Back");
backButton.addActionListener(e -> showWeightingScreen());
JButton calculateButton = new JButton("Calculate");
calculateButton.addActionListener(e -> {
try {
for (int i = 0; i < numComponents; i++) {
scores[i] = Double.parseDouble(scoreFields[i].getText());
}
showResultScreen();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter valid numbers for the scores.");
}
});
gbc.gridy = numComponents + 1;
gbc.gridx = 0;
gbc.gridwidth = 1;
panel.add(backButton, gbc);
gbc.gridx = 1;
panel.add(calculateButton, gbc);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.revalidate();
frame.repaint();
}
private void showResultScreen() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
double totalMark = calculateTotalMark();
JLabel resultLabel = new JLabel("Total module mark: " + String.format("%.2f", totalMark));
resultLabel.setFont(new Font("Arial", Font.BOLD, 18));
resultLabel.setForeground(Color.BLACK);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
panel.add(resultLabel, gbc);
for (int i = 0; i < numComponents; i++) {
JLabel componentLabel = new JLabel("Component " + (i + 1) + " mark:");
// Format the mark and weighting percentage for display
JLabel markLabel = new JLabel(String.format("%.2f (%.0f%%)", scores[i] * (weightings[i] / 100), weightings[i]));
// Create a panel for the mark label and percentage sign
JPanel endScorePanel = new JPanel(new BorderLayout());
endScorePanel.add(markLabel, BorderLayout.CENTER);
// No need to add another JLabel("%") since the percentage is already included in markLabel
gbc.gridy = i + 1;
gbc.gridx = 0;
gbc.gridwidth = 1;
panel.add(componentLabel, gbc);
gbc.gridx = 1;
panel.add(endScorePanel, gbc);
}
JButton backButton = new JButton("Back");
backButton.addActionListener(e -> showScoreScreen());
JButton restartButton = new JButton("Restart");
restartButton.addActionListener(e -> showNumComponentsScreen());
gbc.gridy = numComponents + 1;
gbc.gridx = 0;
gbc.gridwidth = 1;
gbc.weightx = 0; // Reset weightx for buttons
panel.add(backButton, gbc);
gbc.gridx = 1;
panel.add(restartButton, gbc);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.revalidate();
frame.repaint();
}
private double calculateTotalMark() {
double totalMark = 0;
for (int i = 0; i < numComponents; i++) {
totalMark += scores[i] * (weightings[i] / 100);
}
return totalMark;
}
}