-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminService.java
More file actions
122 lines (104 loc) · 5.26 KB
/
Copy pathAdminService.java
File metadata and controls
122 lines (104 loc) · 5.26 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
import java.io.BufferedReader;
import java.sql.*;
public class AdminService {
public static void menuAdmin(Connection conn, BufferedReader input) {
try {
boolean isAdmin = true;
while (isAdmin) {
System.out.println("\n=== PANEL ADMIN GAMECENTRE ===");
System.out.println("1. Lihat Peminjaman Aktif (Semua User)");
System.out.println("2. Proses Pengembalian (Return & Restock)");
System.out.println("3. Batalkan Peminjaman (Force Cancel & Restock)");
System.out.println("4. Kirim Reminder ke User (Log Notifikasi)");
System.out.println("0. Logout");
System.out.print("Pilihan: ");
String choice = input.readLine();
switch (choice) {
case "1": showActiveLoans(conn); break;
case "2": updateStatusTrx(conn, input, "returned"); break;
case "3": updateStatusTrx(conn, input, "cancelled"); break;
case "4": sendReminder(conn, input); break;
case "0": isAdmin = false; break;
default: System.out.println("Menu tidak tersedia.");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Menampilkan semua peminjaman aktif
private static void showActiveLoans(Connection conn) throws SQLException {
String sql = "SELECT t.trx_id, u.username, g.title, t.borrow_date " +
"FROM transactions t " +
"JOIN users u ON t.user_id = u.user_id " +
"JOIN games g ON t.game_id = g.game_id " +
"WHERE t.status = 'active'";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
System.out.println("\n--- LIST SEMUA PEMINJAMAN AKTIF ---");
boolean adaData = false;
while (rs.next()) {
System.out.printf("ID Trx: %d | User: %s | Game: %s | Tgl: %s\n",
rs.getInt("trx_id"), rs.getString("username"),
rs.getString("title"), rs.getDate("borrow_date"));
adaData = true;
}
if (!adaData) System.out.println("Tidak ada peminjaman aktif.");
}
private static void updateStatusTrx(Connection conn, BufferedReader input, String newStatus) throws Exception {
System.out.print("Masukkan ID Transaksi: ");
int trxId = Integer.parseInt(input.readLine());
String checkSql = "SELECT game_id, status FROM transactions WHERE trx_id = ?";
PreparedStatement ps = conn.prepareStatement(checkSql);
ps.setInt(1, trxId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int gameId = rs.getInt("game_id");
String currentStatus = rs.getString("status");
if (currentStatus.equals("active")) {
String updateTrx = "UPDATE transactions SET status = ? WHERE trx_id = ?";
ps = conn.prepareStatement(updateTrx);
ps.setString(1, newStatus);
ps.setInt(2, trxId);
ps.executeUpdate();
String updateStock = "UPDATE games SET stock = stock + 1 WHERE game_id = ?";
ps = conn.prepareStatement(updateStock);
ps.setInt(1, gameId);
ps.executeUpdate();
System.out.println("SUKSES: Status '" + newStatus + "' & Stok dikembalikan.");
} else {
System.out.println("GAGAL: Transaksi sudah tidak aktif.");
}
} else {
System.out.println("ID Transaksi tidak ditemukan.");
}
}
private static void sendReminder(Connection conn, BufferedReader input) throws Exception {
System.out.print("Masukkan ID Peminjaman target: ");
int trxId = Integer.parseInt(input.readLine());
// 1. Cari tahu siapa pemilik transaksi ini dan apa gamenya
String sql = "SELECT t.user_id, u.username, g.title FROM transactions t " +
"JOIN users u ON t.user_id = u.user_id " +
"JOIN games g ON t.game_id = g.game_id " +
"WHERE t.trx_id = ? AND t.status = 'active'";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, trxId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int targetUserId = rs.getInt("user_id");
String username = rs.getString("username");
String gameTitle = rs.getString("title");
// 2. Buat Pesan Notifikasi
String message = "Halo " + username + ", Peminjaman anda akan segera berakhir, mohon kembalikan: " + gameTitle;
// 3. Masukkan ke tabel notifications
String insertNotif = "INSERT INTO notifications (user_id, message) VALUES (?, ?)";
ps = conn.prepareStatement(insertNotif);
ps.setInt(1, targetUserId);
ps.setString(2, message);
ps.executeUpdate();
System.out.println("SUKSES: Notifikasi telah dikirim ke Inbox User " + username);
} else {
System.out.println("Gagal: Transaksi tidak ditemukan atau sudah selesai.");
}
}
}