import java.io.IOException; import java.sql.*; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; public class DatabaseUtil { private static final String DB_URL = "jdbc:sqlite:globe_guru.db"; public static Connection getConnection() throws SQLException { return DriverManager.getConnection(DB_URL); } public static void initializeDatabase() throws SQLException { try (Connection conn = getConnection(); PreparedStatement stmt1 = conn.prepareStatement( "CREATE TABLE IF NOT EXISTS users (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "username TEXT NOT NULL UNIQUE, " + "email TEXT NOT NULL UNIQUE, " + "password TEXT, " + "isAdmin BOOLEAN NOT NULL DEFAULT FALSE)" ); PreparedStatement stmt2 = conn.prepareStatement( "CREATE TABLE IF NOT EXISTS options (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "link TEXT, " + "imgSrc TEXT, " + "hotelName TEXT, " + "country TEXT, " + "price REAL, " + "dateRange TEXT, " + "isPriceChanged BOOLEAN DEFAULT 0, " + "newPrice REAL DEFAULT 0)" ); PreparedStatement stmt3 = conn.prepareStatement( "CREATE TABLE IF NOT EXISTS savedOptions (" + "userId INTEGER, " + "optionId INTEGER, " + "FOREIGN KEY(userId) REFERENCES users(id), " + "FOREIGN KEY(optionId) REFERENCES options(id), " + "UNIQUE(userId, optionId))" )) { stmt1.executeUpdate(); stmt2.executeUpdate(); stmt3.executeUpdate(); } } public static boolean registerUser(String username, String email, String password) throws SQLException { String sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)"; try (Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, username); stmt.setString(2, email); stmt.setString(3, password); // Store hashed return stmt.executeUpdate() > 0; } } public static boolean authenticateUser(String email, String password) throws SQLException { String sql = "SELECT password FROM users WHERE email = ?"; try (Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, email); try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { String storedPassword = rs.getString("password"); if (password == null) { // Assume this is a Google login return storedPassword == null; } return password.equals(storedPassword); // Check hashed password } } } return false; } public static boolean deleteUser(int userId) throws SQLException { String selectSql = "SELECT userId FROM users WHERE userId = ?"; String deleteSql = "DELETE FROM users WHERE userId = ?"; String deleteFavoritesSql = "DELETE FROM savedOptions WHERE userId = ?"; try (Connection conn = getConnection(); PreparedStatement selectStmt = conn.prepareStatement(selectSql); PreparedStatement deleteStmt = conn.prepareStatement(deleteSql); PreparedStatement deleteFavoritesStmt = conn.prepareStatement(deleteFavoritesSql)) { selectStmt.setInt(1, userId); try (ResultSet rs = selectStmt.executeQuery()) { if (rs.next()) { // User exists, delete the user and their favourite options deleteStmt.setInt(1, userId); int rowsAffected = deleteStmt.executeUpdate(); deleteFavoritesStmt.setInt(1, userId); deleteFavoritesStmt.executeUpdate(); return rowsAffected > 0; } else { // User does not exist return false; } } } } public static boolean userExists(String email) throws SQLException { String query = "SELECT COUNT(*) FROM users WHERE email = ?"; try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(query)) { statement.setString(1, email); ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { return resultSet.getInt(1) > 0; } } return false; } public static boolean isAdmin(String email) throws SQLException { String selectSql = "SELECT isAdmin FROM users WHERE email = ?"; try (Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(selectSql)) { stmt.setString(1, email); try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { return rs.getBoolean("isAdmin"); } } } return false; } public static List