-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameCentre.java
More file actions
107 lines (93 loc) · 3.86 KB
/
Copy pathGameCentre.java
File metadata and controls
107 lines (93 loc) · 3.86 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;
import java.util.regex.Pattern;
public class GameCentre {
static Connection conn;
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) {
try {
conn = KoneksiDB.getConnection();
if (conn == null) System.exit(0);
while (true) {
System.out.println("\n=== SELAMAT DATANG DI GAMECENTRE ===");
System.out.println("1. Login");
System.out.println("2. Registrasi");
System.out.println("0. Keluar");
System.out.print("Pilihan: ");
String choice = input.readLine();
switch (choice) {
case "1": login(); break;
case "2": register(); break;
case "0":
System.out.println("Bye!");
System.exit(0);
break;
default: System.out.println("Pilihan salah.");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
static void register() {
try {
System.out.println("\n--- REGISTRASI ---");
System.out.print("Username: ");
String username = input.readLine();
System.out.print("Email: ");
String emailRaw = input.readLine();
System.out.print("Password: ");
String password = input.readLine();
String finalEmail = emailRaw;
String role = "user";
// Fitur Regex & Domain Swap
if (Pattern.matches(".*@game\\.adminregist\\.com", emailRaw)) { //Cek registrasi admin dan ganti ke email khusus admin
System.out.println("[SYSTEM] Admin Terdeteksi.");
finalEmail = emailRaw.replace("@game.adminregist.com", "@gamecentre.com");
role = "admin";
System.out.println("[SYSTEM] Email Admin diubah ke: " + finalEmail);
}
String sql = "INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, finalEmail);
ps.setString(3, password);
ps.setString(4, role);
ps.executeUpdate();
System.out.println("Registrasi Berhasil!");
} catch (Exception e) {
System.out.println("Gagal: " + e.getMessage());
}
}
static void login() {
try {
System.out.println("\n--- LOGIN ---");
System.out.print("Email: ");
String email = input.readLine();
System.out.print("Password: ");
String pass = input.readLine();
String sql = "SELECT * FROM users WHERE email = ? AND password = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, email);
ps.setString(2, pass);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String role = rs.getString("role");
String username = rs.getString("username");
int userId = rs.getInt("user_id");
System.out.println("Login Sukses! Halo " + username);
// Routing ke Class yang sesuai
if (role.equals("admin")) {
AdminService.menuAdmin(conn, input); //Call class Admin
} else {
UserService.menuUser(conn, input, userId, username); //Call class User
}
} else {
System.out.println("Login Gagal!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}