-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.java
More file actions
146 lines (126 loc) · 6.16 KB
/
Copy pathUserService.java
File metadata and controls
146 lines (126 loc) · 6.16 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
import java.io.BufferedReader;
import java.sql.*;
public class UserService {
public static void menuUser(Connection conn, BufferedReader input, int userId, String username) {
try {
boolean isUser = true;
while (isUser) {
System.out.println("\n=== PANEL USER: " + username + " ===");
System.out.println("1. Lihat Daftar Game");
System.out.println("2. Pinjam Game");
System.out.println("3. Peminjaman Saya");
System.out.println("4. Batalkan Peminjaman");
System.out.println("5. Pusat Notifikasi (Inbox)");
System.out.println("0. Logout");
System.out.print("Pilihan: ");
String choice = input.readLine();
switch (choice) {
case "1": showGames(conn); break;
case "2": borrowGame(conn, input, userId); break;
case "3": myActiveLoans(conn, userId); break;
case "4": strictCancel(conn, input, userId); break;
case "5": showNotifications(conn, userId); break;
case "0": isUser = false; break;
default: System.out.println("Pilihan salah.");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void showGames(Connection conn) throws SQLException {
String sql = "SELECT * FROM games";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
System.out.println("\n--- KATALOG GAME ---");
while (rs.next()) {
System.out.printf("ID: %d | %s | Kategori: %s | Stok: %d\n",
rs.getInt("game_id"), rs.getString("title"),
rs.getString("category"), rs.getInt("stock"));
}
}
private static void borrowGame(Connection conn, BufferedReader input, int userId) throws Exception {
showGames(conn);
System.out.print("ID Game yang ingin dipinjam: ");
try {
int gameId = Integer.parseInt(input.readLine());
String checkSql = "SELECT stock FROM games WHERE game_id = ?";
PreparedStatement ps = conn.prepareStatement(checkSql);
ps.setInt(1, gameId);
ResultSet rs = ps.executeQuery();
if (rs.next() && rs.getInt("stock") > 0) {
String insert = "INSERT INTO transactions (user_id, game_id, borrow_date, status) VALUES (?, ?, CURRENT_DATE, 'active')";
ps = conn.prepareStatement(insert);
ps.setInt(1, userId);
ps.setInt(2, gameId);
ps.executeUpdate();
String update = "UPDATE games SET stock = stock - 1 WHERE game_id = ?";
ps = conn.prepareStatement(update);
ps.setInt(1, gameId);
ps.executeUpdate();
System.out.println("Berhasil meminjam!");
} else {
System.out.println("Stok habis atau ID salah.");
}
} catch (NumberFormatException e) {
System.out.println("Input harus angka.");
}
}
private static void myActiveLoans(Connection conn, int userId) throws SQLException {
// Query ini menggunakan 'WHERE t.user_id = ?' yang mengunci data ke user yang login
String sql = "SELECT t.trx_id, g.title, t.borrow_date FROM transactions t " +
"JOIN games g ON t.game_id = g.game_id " +
"WHERE t.user_id = ? AND t.status = 'active'";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, userId); // userId diambil dari parameter login
ResultSet rs = ps.executeQuery();
System.out.println("\n--- PEMINJAMAN SAYA ---");
boolean found = false;
while (rs.next()) {
System.out.printf("ID Trx: %d | Game: %s | Tgl: %s\n",
rs.getInt("trx_id"), rs.getString("title"), rs.getDate("borrow_date"));
found = true;
}
if (!found) System.out.println("Anda tidak memiliki peminjaman aktif.");
}
private static void strictCancel(Connection conn, BufferedReader input, int userId) throws Exception {
System.out.println("\n--- PEMBATALAN ---");
System.out.print("ID Peminjaman (Trx ID): ");
String trxIdStr = input.readLine();
System.out.print("Konfirmasi Nama Game (Persis): ");
String gameName = input.readLine();
String sql = "SELECT t.trx_id, t.game_id FROM transactions t " +
"JOIN games g ON t.game_id = g.game_id " +
"WHERE t.trx_id = ? AND t.user_id = ? AND g.title = ? AND t.status = 'active'";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, trxIdStr);
ps.setInt(2, userId);
ps.setString(3, gameName);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int gameId = rs.getInt("game_id");
ps = conn.prepareStatement("UPDATE transactions SET status = 'cancelled' WHERE trx_id = ?");
ps.setString(1, trxIdStr);
ps.executeUpdate();
ps = conn.prepareStatement("UPDATE games SET stock = stock + 1 WHERE game_id = ?");
ps.setInt(1, gameId);
ps.executeUpdate();
System.out.println("Sukses! Peminjaman dibatalkan.");
} else {
System.out.println("Validasi Gagal! Data tidak cocok.");
}
}
private static void showNotifications(Connection conn, int userId) throws SQLException {
String sql = "SELECT message, created_at FROM notifications WHERE user_id = ? ORDER BY created_at DESC";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, userId);
ResultSet rs = ps.executeQuery();
System.out.println("\n=== INBOX NOTIFIKASI ===");
boolean hasNotif = false;
while (rs.next()) {
System.out.printf("[%s] %s\n", rs.getTimestamp("created_at"), rs.getString("message"));
hasNotif = true;
}
if (!hasNotif) System.out.println("Tidak ada notifikasi baru.");
}
}