From acb1983ae34c286897093b125ed3eb82255cda14 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Tue, 30 Jun 2026 09:57:48 +0330 Subject: [PATCH] login®ister --- .idea/.gitignore | 10 + .idea/compiler.xml | 13 ++ .idea/encodings.xml | 7 + .idea/jarRepositories.xml | 20 ++ .idea/misc.xml | 12 ++ .idea/vcs.xml | 6 + database.sql | 180 +++++------------- pom.xml | 6 + src/main/java/dev/dao/UserDao.java | 56 +++++- .../java/dev/database/DatabaseConnection.java | 12 +- src/main/java/dev/model/User.java | 24 ++- src/main/java/dev/service/AuthService.java | 40 +++- src/main/java/dev/ui/ConsoleMenu.java | 68 ++++++- 13 files changed, 295 insertions(+), 159 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..bf2c501 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..4158879 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..eba6e1f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/database.sql b/database.sql index 2169346..5aefc62 100644 --- a/database.sql +++ b/database.sql @@ -1,141 +1,51 @@ --- Restaurant Database Management System --- --- Instructions: --- 1. Create all required tables. --- 2. Design appropriate PRIMARY KEY and FOREIGN KEY relationships. --- 3. Add suitable constraints based on the requirements. --- 4. Insert initial mock data. --- 5. Insert at least 3 menu items. --- 6. The script should be executable from start to finish without errors. +CREATE TABLE users( + id BIGSERIAL PRIMARY KEY, + username VARCHAR(20) NOT NULL UNIQUE, + password_hash VARCHAR(256) NOT NULL, + email VARCHAR(50) +); + +CREATE TABLE menu_items( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(50) NOT NULL, + description VARCHAR(100), + price DECIMAL NOT NULL CHECK (price > 0), + category VARCHAR(50) +); + +CREATE TABLE customer_orders( + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL, + time TIMESTAMP DEFAULT CURRENT_TIMESTAMP , + total_price DECIMAL(10 , 2), + CONSTRAINT fk_user_id + FOREIGN KEY (user_id) + REFERENCES users(id) +); +CREATE TABLE order_details ( + id BIGSERIAL PRIMARY KEY, + order_id BIGINT NOT NULL, + menu_item_id BIGINT NOT NULL, + quantity INTEGER NOT NULL CHECK (quantity > 0), + unit_price DECIMAL(10 , 2) NOT NULL, --- ======================================================= --- USER TABLE --- ======================================================= --- --- Represents customers using the system. --- --- Required information: --- - Unique identifier --- - Username --- - Password --- - Email (optional) --- --- Requirements: --- - Each user must have a unique identifier. --- - Usernames must be unique. --- - Username and password are required. --- - Passwords should not be stored in plain text. --- --- CREATE TABLE ... + CONSTRAINT fk_order + FOREIGN KEY (order_id) + REFERENCES customer_orders(id) , + + CONSTRAINT fk_menu_item + FOREIGN KEY (menu_item_id) + REFERENCES menu_items(id) + +); +INSERT INTO menu_items(name, description ,price, category) VALUES ('Margherita Pizza', 'Classic pizza with tomato and mozzarella', 12.99, 'Pizza'), + ('Pepperoni Pizza', 'Pizza with pepperoni and cheese', 14.99, 'Pizza'), + ('Cheeseburger', 'Beef burger with cheddar cheese', 10.99, 'Burger'), + ('Spaghetti Carbonara', 'Pasta with eggs, cheese, and pancetta', 13.99, 'Pasta'), + ('Coca-Cola', 'Classic carbonated soft drink', 3.99, 'Drink'), + ('Sparkling Water', 'Natural sparkling mineral water', 2.99, 'Drink'); --- ======================================================= --- MENU ITEM TABLE --- ======================================================= --- --- Represents available food and drink items. --- --- Required information: --- - Unique identifier --- - Name --- - Description (optional) --- - Price --- - Category (optional) --- --- Requirements: --- - Each menu item must have a unique identifier. --- - Name is required. --- - Price must always be positive. --- --- CREATE TABLE ... - - - --- ======================================================= --- ORDER TABLE --- ======================================================= --- --- Represents orders placed by customers. --- --- Required information: --- - Unique identifier --- - Reference to customer --- - Creation date and time --- - Total price --- --- Requirements: --- - Each order must belong to exactly one user. --- - A user can have multiple orders. --- - The relationship between User and Order must be implemented. --- --- Note: --- Avoid using reserved SQL keywords as table names. --- Consider using a name such as "orders" or "customer_orders". --- --- CREATE TABLE ... - - - --- ======================================================= --- ORDER DETAIL TABLE --- ======================================================= --- --- Represents items inside an order. --- --- Required information: --- - Unique identifier --- - Reference to an order --- - Reference to a menu item --- - Quantity --- - Item price at purchase time --- --- Requirements: --- - Each detail record must belong to one order. --- - Each detail record must reference one menu item. --- - Quantity must always be greater than zero. --- - Store the item's price at the moment of purchase. --- --- CREATE TABLE ... - - - --- ======================================================= --- INITIAL MENU DATA --- ======================================================= --- --- Insert at least 3 food or drink items. --- --- Example categories: --- - Pizza --- - Burger --- - Pasta --- - Drink --- --- INSERT INTO ... - - - --- ======================================================= --- OPTIONAL TEST DATA --- ======================================================= --- --- You may insert sample users and orders for testing. --- This section is optional. --- --- INSERT INTO ... - - - --- ======================================================= --- VERIFICATION QUERIES --- ======================================================= --- --- Uncomment these queries to verify your database. --- --- SELECT * FROM ...; --- SELECT * FROM ...; --- SELECT * FROM ...; --- SELECT * FROM ...; \ No newline at end of file diff --git a/pom.xml b/pom.xml index 028ca50..c12c681 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,12 @@ ${postgresql.version} + + org.mindrot + jbcrypt + 0.4 + + \ No newline at end of file diff --git a/src/main/java/dev/dao/UserDao.java b/src/main/java/dev/dao/UserDao.java index af08106..4da31a7 100644 --- a/src/main/java/dev/dao/UserDao.java +++ b/src/main/java/dev/dao/UserDao.java @@ -1,23 +1,69 @@ package dev.dao; +import dev.database.DatabaseConnection; import dev.model.User; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.mindrot.jbcrypt.BCrypt; + public class UserDao { public boolean save(User user) { - // TODO: - // Insert user into database + String hashedPassword = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()); + String query ="INSERT INTO users (username, password_hash, email) VALUES (?, ?, ?)"; + + try(Connection c = DatabaseConnection.getConnection(); + PreparedStatement pst = c.prepareStatement(query)) { + + pst.setString(1 , user.getUsername()); + pst.setString(2 , hashedPassword); + pst.setString(3 , user.getEmail()); + + + int result = pst.executeUpdate(); + if(result==1){ + return true; + } + + + } catch (SQLException e) { + System.out.println(e.getMessage()); + }; return false; } - public User findByUsername(String username) { + public User findByUsername(String userName) { - // TODO: - // Find a user by username + String query = "SELECT * FROM users WHERE username = ?"; + + try (Connection c = DatabaseConnection.getConnection(); + PreparedStatement pst = c.prepareStatement(query)) { + + pst.setString(1, userName); + + try (ResultSet rst = pst.executeQuery()) { + if (rst.next()) { + User user = new User(rst.getInt("id"), + rst.getString("username"), + rst.getString("password_hash"), + rst.getString("email")); + return user; + } + + } + + + } catch (SQLException e) { + System.out.println(e.getMessage()); + } return null; + } } \ No newline at end of file diff --git a/src/main/java/dev/database/DatabaseConnection.java b/src/main/java/dev/database/DatabaseConnection.java index da45535..2870f30 100644 --- a/src/main/java/dev/database/DatabaseConnection.java +++ b/src/main/java/dev/database/DatabaseConnection.java @@ -1,6 +1,7 @@ package dev.database; import java.sql.Connection; +import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseConnection { @@ -9,19 +10,18 @@ public class DatabaseConnection { private static final String USER = "postgres"; // Your Username - private static final String PASSWORD = "password"; // Your Password + private static final String PASSWORD = "12345678"; // Your Password private DatabaseConnection() { } - public static Connection getConnection() - throws SQLException { + public static Connection getConnection() throws SQLException { - // TODO: - // Return a valid PostgreSQL connection + Connection con = DriverManager.getConnection(URL , USER , PASSWORD); - return null; + + return con; } } \ No newline at end of file diff --git a/src/main/java/dev/model/User.java b/src/main/java/dev/model/User.java index 0ab60c6..0ea1c5d 100644 --- a/src/main/java/dev/model/User.java +++ b/src/main/java/dev/model/User.java @@ -3,11 +3,29 @@ package dev.model; public class User { private int id; - private String username; - private String password; - private String email; + + public User (int id, String username, String password, String email) { + this.id = id; + this.username = username; + this.password = password; + this.email = email; + } + + public User (String username, String password, String email) { + this.username = username; + this.password = password; + this.email = email; + } + + + public int getId() { return id; } + public String getUsername() { return username; } + public String getPassword() { return password; } + public String getEmail() { return email; } + + } \ No newline at end of file diff --git a/src/main/java/dev/service/AuthService.java b/src/main/java/dev/service/AuthService.java index bee866a..08cd41f 100644 --- a/src/main/java/dev/service/AuthService.java +++ b/src/main/java/dev/service/AuthService.java @@ -1,23 +1,51 @@ package dev.service; +import dev.dao.UserDao; import dev.model.User; +import org.mindrot.jbcrypt.BCrypt; public class AuthService { + private UserDao userDao = new UserDao(); + public boolean register(String username, String password, String email) { - // TODO: - // Validate and register user + if(userDao.findByUsername(username) == null){ + User user = new User (username , password , email); + if(userDao.save(user)){ + System.out.println("successfully register"); + return true; + }else { + System.out.println("register failed"); + return false; + } + } + else { + System.out.println("Username already exists."); + return false; + } - return false; } public User login(String username, String password) { - // TODO: - // Authenticate user + User user = userDao.findByUsername(username); + + if(user == null){ + System.out.println("Username not found."); + return null; + }else { + String storedHash = user.getPassword(); + + if (BCrypt.checkpw(password, storedHash)) { + return user; + + } else { + System.out.println("Incorrect password."); + return null; + } + } - return null; } } \ No newline at end of file diff --git a/src/main/java/dev/ui/ConsoleMenu.java b/src/main/java/dev/ui/ConsoleMenu.java index 663dc0c..8afaa26 100644 --- a/src/main/java/dev/ui/ConsoleMenu.java +++ b/src/main/java/dev/ui/ConsoleMenu.java @@ -1,11 +1,16 @@ package dev.ui; +import dev.model.User; +import dev.service.AuthService; + import java.util.Scanner; public class ConsoleMenu { - private final Scanner scanner = - new Scanner(System.in); + private final Scanner scanner = new Scanner(System.in); + private final AuthService authService = new AuthService(); + private User user; + public void start() { @@ -16,18 +21,46 @@ public class ConsoleMenu { System.out.println("1. Login"); System.out.println("2. Register"); System.out.println("3. Exit"); + System.out.println("======================================= \n" + + "Choose an option: "); int choice = scanner.nextInt(); switch (choice) { case 1: - // TODO + { + System.out.println("Enter username:"); + String username =scanner.next(); + System.out.println("Enter password:"); + String password = scanner.next(); + + user = authService.login(username,password); + + if(user == null){ + System.out.println("Try again"); + } + else { + System.out.println("Login success"); + + } + break; + } case 2: - // TODO + { + System.out.println("Enter username:"); + String username =scanner.next(); + System.out.println("Enter password:"); + String password = scanner.next(); + System.out.println("Enter email:"); + String email = scanner.next(); + + + authService.register(username,password,email); break; + } case 3: return; @@ -39,6 +72,33 @@ public class ConsoleMenu { } + } + + public void menu(){ + + while (true){ + System.out.println(); + System.out.println("========\uD83C\uDF7D\uFE0F MAIN MENU \uD83C\uDF7D\uFE0F========"); + System.out.println("1. View Menu"); + System.out.println("2. Place a New Order"); + System.out.println("3. View Order History "); + System.out.println("4. Logout"); + System.out.println("======================================= \n" + + "Choose an option: "); + + int choice = scanner.nextInt(); + + + + } + + + + + + + + } } \ No newline at end of file