-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaBeans_courses.java
More file actions
329 lines (299 loc) · 12.2 KB
/
Copy pathJavaBeans_courses.java
File metadata and controls
329 lines (299 loc) · 12.2 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
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.ArrayList;
public class JavaBeans_courses {
public static void main(String[] args) {
try (Connection con = DriverManager.getConnection("jdbc:sqlite:courses.db")) {
con.createStatement().execute("CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY, password TEXT)");
con.createStatement().execute("""
CREATE TABLE IF NOT EXISTS courses (
id TEXT,
program_id TEXT,
code TEXT PRIMARY KEY,
name TEXT,
category TEXT,
type TEXT,
prereq TEXT,
coreq TEXT,
progression TEXT,
course_plan TEXT
)
""");
} catch (Exception e) {
e.printStackTrace();
}
new ModernLogin();
}
}
class ModernLogin extends Frame {
TextField user, pass;
Label status;
ModernLogin() {
setTitle("Login");
setSize(300, 200);
setLayout(null);
setBackground(Color.WHITE);
Label title = new Label("Course Login", Label.CENTER);
title.setFont(new Font("Segoe UI", Font.BOLD, 15));
title.setBounds(70, 30, 160, 20);
add(title);
Label userLabel = new Label("Username:");
Label passLabel = new Label("Password:");
userLabel.setBounds(40, 60, 70, 20);
passLabel.setBounds(40, 90, 70, 20);
add(userLabel);
add(passLabel);
user = new TextField();
pass = new TextField();
pass.setEchoChar('*');
user.setBounds(120, 60, 120, 22);
pass.setBounds(120, 90, 120, 22);
add(user);
add(pass);
Button login = new Button("Login");
Button register = new Button("Register");
login.setBounds(40, 130, 80, 25);
register.setBounds(160, 130, 80, 25);
add(login);
add(register);
status = new Label("", Label.CENTER);
status.setBounds(40, 160, 200, 20);
status.setForeground(Color.RED);
add(status);
login.addActionListener(e -> {
if (CourseOperations.authenticate(user.getText(), pass.getText())) {
status.setText("Login Successful");
dispose();
new CourseDashboard(user.getText());
} else {
status.setText("Invalid Credentials");
}
});
register.addActionListener(e -> {
CourseOperations.register(user.getText(), pass.getText());
status.setText("User Registered");
});
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
}
class CourseDashboard extends Frame implements ActionListener {
TextField[] fields = new TextField[10];
TextArea display;
String[] labels = {"ID", "Program ID", "Code", "Name", "Category", "Type", "Pre-req", "Co-req", "Progression", "Course plan"};
String originalCode = "";
String loggedInUser;
CourseDashboard(String username) {
this.loggedInUser = username;
setTitle("Course Dashboard");
setSize(780, 600);
setLayout(null);
setBackground(Color.WHITE);
Label header = new Label("Course Management", Label.CENTER);
header.setFont(new Font("Segoe UI", Font.BOLD, 20));
header.setBounds(20, 40, 740, 30);
add(header);
Label userInfo = new Label("Logged in as: " + username);
userInfo.setBounds(40, 70, 300, 20);
add(userInfo);
Panel formPanel = new Panel();
formPanel.setLayout(new GridLayout(10, 2, 8, 6));
formPanel.setBounds(40, 100, 700, 280);
formPanel.setBackground(Color.LIGHT_GRAY);
for (int i = 0; i < labels.length; i++) {
Label l = new Label(labels[i]);
TextField tf = new TextField();
fields[i] = tf;
formPanel.add(l);
formPanel.add(tf);
}
add(formPanel);
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new GridLayout(1, 7, 10, 0));
buttonPanel.setBounds(40, 390, 700, 30);
String[] btns = {"Create", "Update", "Delete", "View All", "Retrive", "Get by Program", "Reset"};
for (String btn : btns) {
Button b = new Button(btn);
b.addActionListener(this);
buttonPanel.add(b);
}
add(buttonPanel);
display = new TextArea();
display.setBounds(40, 430, 700, 120);
display.setEditable(false);
display.setBackground(new Color(245, 245, 245));
display.setFont(new Font("Monospaced", Font.PLAIN, 12));
add(display);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String[] data = new String[10];
for (int i = 0; i < 10; i++) {
data[i] = fields[i].getText().trim(); // Trim input
System.out.println("Field " + i + ": " + data[i]); // Debug log
}
switch (cmd) {
case "Create" -> {
CourseOperations.JavaBeans_courses_create(data);
display.setText("✅ Course created!");
}
case "Update" -> {
CourseOperations.JavaBeans_courses_update(data, originalCode);
display.setText("✅ Course updated!");
}
case "Delete" -> {
CourseOperations.JavaBeans_courses_delete(data[2]);
display.setText("🗑 Course deleted: " + data[2]);
}
case "View All" -> {
var all = CourseOperations.viewAll();
display.setText("");
for (String[] row : all) display.append(String.join(" | ", row) + "\n");
}
case "Retrive" -> {
if (!data[0].isEmpty()) {
var row = CourseOperations.retrieveById(data[0]);
if (row != null) {
for (int i = 0; i < 10; i++) fields[i].setText(row[i]);
originalCode = row[2];
display.setText("📦 Retrieved by ID");
} else {
display.setText("❌ Not found by ID.");
}
} else if (!data[1].isEmpty()) {
var rows = CourseOperations.retrieveByProgramId(data[1]);
if (!rows.isEmpty()) {
String[] row = rows.get(0);
for (int i = 0; i < 10; i++) fields[i].setText(row[i]);
originalCode = row[2];
display.setText("📦 Courses for Program ID:\n");
for (String[] r : rows) display.append(String.join(" | ", r) + "\n");
} else {
display.setText("❌ No courses for Program ID.");
}
} else {
display.setText("⚠ Please enter ID or Program ID");
}
}
case "Get by Program" -> {
var rows = CourseOperations.retrieveByProgramId(data[1]);
display.setText("");
for (String[] row : rows) display.append(String.join(" | ", row) + "\n");
}
case "Reset" -> {
for (TextField field : fields) field.setText("");
display.setText("🔄 Reset");
originalCode = "";
}
}
}
}
class CourseOperations {
static void register(String u, String p) {
try (Connection con = DriverManager.getConnection("jdbc:sqlite:courses.db")) {
PreparedStatement ps = con.prepareStatement("INSERT OR IGNORE INTO users VALUES (?, ?)");
ps.setString(1, u);
ps.setString(2, p);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
static boolean authenticate(String u, String p) {
try (Connection con = DriverManager.getConnection("jdbc:sqlite:courses.db")) {
PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE username=? AND password=?");
ps.setString(1, u);
ps.setString(2, p);
return ps.executeQuery().next();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
static void JavaBeans_courses_create(String[] d) {
try (Connection con = DriverManager.getConnection("jdbc:sqlite:courses.db")) {
PreparedStatement ps = con.prepareStatement("INSERT OR REPLACE INTO courses VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
for (int i = 0; i < 10; i++) ps.setString(i + 1, d[i]);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
static void JavaBeans_courses_update(String[] d, String oldCode) {
try (Connection con = DriverManager.getConnection("jdbc:sqlite:courses.db")) {
PreparedStatement ps = con.prepareStatement(
"UPDATE courses SET id=?, program_id=?, code=?, name=?, category=?, type=?, prereq=?, coreq=?, progression=?, course_plan=? WHERE code=?"
);
for (int i = 0; i < 10; i++) ps.setString(i + 1, d[i]);
ps.setString(11, oldCode);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
static void JavaBeans_courses_delete(String code) {
try (Connection con = DriverManager.getConnection("jdbc:sqlite:courses.db")) {
PreparedStatement ps = con.prepareStatement("DELETE FROM courses WHERE code=?");
ps.setString(1, code);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
static ArrayList<String[]> viewAll() {
ArrayList<String[]> list = new ArrayList<>();
try (Connection con = DriverManager.getConnection("jdbc:sqlite:courses.db")) {
ResultSet rs = con.createStatement().executeQuery("SELECT * FROM courses");
while (rs.next()) {
String[] row = new String[10];
for (int i = 0; i < 10; i++) row[i] = rs.getString(i + 1);
list.add(row);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
static String[] retrieveById(String id) {
try (Connection con = DriverManager.getConnection("jdbc:sqlite:courses.db")) {
PreparedStatement ps = con.prepareStatement("SELECT * FROM courses WHERE id=?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String[] row = new String[10];
for (int i = 0; i < 10; i++) row[i] = rs.getString(i + 1);
return row;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
static ArrayList<String[]> retrieveByProgramId(String programId) {
ArrayList<String[]> list = new ArrayList<>();
try (Connection con = DriverManager.getConnection("jdbc:sqlite:courses.db")) {
PreparedStatement ps = con.prepareStatement("SELECT * FROM courses WHERE program_id=?");
ps.setString(1, programId);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String[] row = new String[10];
for (int i = 0; i < 10; i++) row[i] = rs.getString(i + 1);
list.add(row);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}