-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartQuiz.java
More file actions
222 lines (181 loc) · 7.95 KB
/
StartQuiz.java
File metadata and controls
222 lines (181 loc) · 7.95 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
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class StartQuiz extends JFrame {
private static final String SETTINGS_FILE = "settings.txt";
private static final String ATTEMPTS_FILE = "attempts.txt";
public StartQuiz(ArrayList<Questions> questions) {
init(questions, null);
}
public StartQuiz(ArrayList<Questions> questions, String studentName, String studentId) {
init(questions, new StudentIdentity(studentName, studentId));
}
private void init(ArrayList<Questions> questions, StudentIdentity presetIdentity) {
setTitle("Start Your Quiz");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(720, 520);
setLocationRelativeTo(null);
setResizable(false);
JPanel mainPanel = new JPanel();
mainPanel.setBackground(new Color(245, 247, 250));
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.setBorder(new EmptyBorder(40, 50, 40, 50));
JLabel titleLabel = new JLabel("Quiz Application");
titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 32));
titleLabel.setForeground(new Color(30, 41, 59));
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
mainPanel.add(titleLabel);
mainPanel.add(Box.createVerticalStrut(18));
JLabel descLabel = new JLabel("<html><center>Ready to test your knowledge?<br>You are about to answer " +
questions.size() + " questions.<br>Good luck!</center></html>");
descLabel.setFont(new Font("Segoe UI", Font.PLAIN, 14));
descLabel.setForeground(new Color(51, 65, 85));
descLabel.setHorizontalAlignment(SwingConstants.CENTER);
descLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
mainPanel.add(descLabel);
mainPanel.add(Box.createVerticalStrut(26));
JPanel detailsPanel = new JPanel();
detailsPanel.setOpaque(false);
detailsPanel.setLayout(new BoxLayout(detailsPanel, BoxLayout.Y_AXIS));
detailsPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel detailLabel = new JLabel("Total Questions: " + questions.size());
detailLabel.setFont(new Font("Segoe UI", Font.PLAIN, 14));
detailLabel.setForeground(new Color(51, 65, 85));
detailLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
detailsPanel.add(detailLabel);
detailsPanel.add(Box.createVerticalStrut(6));
JLabel typeLabel = new JLabel("Multiple Choice (4 Options)");
typeLabel.setFont(new Font("Segoe UI", Font.PLAIN, 14));
typeLabel.setForeground(new Color(51, 65, 85));
typeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
detailsPanel.add(typeLabel);
mainPanel.add(detailsPanel);
mainPanel.add(Box.createVerticalStrut(26));
JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(false);
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
buttonPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
JButton startButton = new JButton("Start Quiz");
startButton.setFont(new Font("Segoe UI", Font.BOLD, 14));
startButton.setBackground(new Color(37, 99, 235));
startButton.setForeground(Color.WHITE);
startButton.setBorder(BorderFactory.createEmptyBorder(12, 30, 12, 30));
startButton.setFocusPainted(false);
startButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
startButton.setPreferredSize(new Dimension(150, 44));
startButton.addActionListener(e -> {
StudentIdentity identity = presetIdentity != null ? presetIdentity : promptForIdentity();
if (identity == null) {
return;
}
Settings settings = loadSettings();
int maxTrials = settings.maxTrials;
if (maxTrials == 0) {
JOptionPane.showMessageDialog(this,
"No trials are allowed for this quiz.\nPlease contact your teacher.",
"Trials Disabled", JOptionPane.WARNING_MESSAGE);
return;
}
int attempts = getAttempts(identity.studentId);
if (attempts >= maxTrials) {
JOptionPane.showMessageDialog(this,
"No attempts remaining for this student ID.\nPlease contact your teacher.",
"Attempts Limit Reached", JOptionPane.WARNING_MESSAGE);
return;
}
new MainWindow(questions, 0, identity.name, identity.studentId);
dispose();
});
buttonPanel.add(startButton);
buttonPanel.add(Box.createHorizontalStrut(20));
JButton cancelButton = new JButton("Cancel");
cancelButton.setFont(new Font("Segoe UI", Font.BOLD, 14));
cancelButton.setBackground(new Color(100, 116, 139));
cancelButton.setForeground(Color.WHITE);
cancelButton.setBorder(BorderFactory.createEmptyBorder(12, 30, 12, 30));
cancelButton.setFocusPainted(false);
cancelButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
cancelButton.setPreferredSize(new Dimension(150, 44));
cancelButton.addActionListener(e -> {
new Dashboard(questions);
dispose();
});
buttonPanel.add(cancelButton);
mainPanel.add(buttonPanel);
add(mainPanel);
setVisible(true);
}
private StudentIdentity promptForIdentity() {
JTextField nameField = new JTextField();
JTextField idField = new JTextField();
JPanel panel = new JPanel(new GridLayout(0, 1, 6, 6));
panel.setBorder(new EmptyBorder(8, 8, 8, 8));
panel.add(new JLabel("Student Name:"));
panel.add(nameField);
panel.add(new JLabel("Student ID:"));
panel.add(idField);
while (true) {
int option = JOptionPane.showConfirmDialog(this, panel, "Student Information",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (option != JOptionPane.OK_OPTION) {
return null;
}
String name = nameField.getText().trim();
String id = idField.getText().trim();
if (name.isEmpty() || id.isEmpty()) {
JOptionPane.showMessageDialog(this, "Name and ID are required.",
"Invalid Input", JOptionPane.WARNING_MESSAGE);
continue;
}
return new StudentIdentity(name, id);
}
}
private Settings loadSettings() {
File file = new File(SETTINGS_FILE);
Settings settings = new Settings(); settings.maxTrials = 1;
if (!file.exists()) {
return settings;
}
try (Scanner sc = new Scanner(file)) {
if (sc.hasNextLine()) { }
if (sc.hasNextLine()) {
settings.maxTrials = Integer.parseInt(sc.nextLine().trim());
}
} catch (Exception e) {
return settings;
}
return settings;
}
private int getAttempts(String studentId) {
File file = new File(ATTEMPTS_FILE);
if (!file.exists()) {
return 0;
}
try (Scanner sc = new Scanner(file)) {
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] parts = line.split("\\|", 2);
if (parts.length == 2 && parts[0].trim().equals(studentId)) {
return Integer.parseInt(parts[1].trim());
}
}
} catch (Exception e) {
return 0;
}
return 0;
}
private static class Settings {
int maxTrials;
}
private static class StudentIdentity {
String name;
String studentId;
StudentIdentity(String name, String studentId) {
this.name = name;
this.studentId = studentId;
}
}
}