# WS-11-Database # Restaurant Database Management System 🍕🗄️ ## Overview In this workshop, you will build a **restaurant management system focused on database design, PostgreSQL, and JDBC integration**. The main goal of this project is to practice working with relational databases, designing a proper database schema, managing relationships between tables, and connecting a Java application to PostgreSQL. You will implement a backend system where users can create accounts, view menu items, create orders, and store all information permanently inside a database. The project should be implemented using: - Java 23 - Maven - PostgreSQL - JDBC --- # Prerequisites ✅ Make sure you have installed: - Git - Java 23 - Maven - PostgreSQL - PostgreSQL Client (pgAdmin is recommended) --- # Maven Configuration This project must use **Maven** as the build tool. Your project should contain: ``` pom.xml ``` The `pom.xml` file is responsible for managing project dependencies and configuring the Java version. Example: ```xml 23 23 UTF-8 42.7.8 org.postgresql postgresql ${postgresql.version} ``` This dependency allows your Java application to connect to PostgreSQL using JDBC. --- # Objectives ✏️ By completing this assignment, you will: - Learn how to design a relational database - Understand database relationships - Work with Primary Keys and Foreign Keys - Use SQL to create and manage tables - Connect Java applications to PostgreSQL using JDBC - Implement CRUD operations - Handle database transactions - Build a structured backend application --- # Important Concepts ## CRUD Operations CRUD represents the four basic operations used to manage data in a database. CRUD stands for: --- ## Create Adding new records to the database. Example: ```sql INSERT INTO users(username, password) VALUES ('ali', 'hashed_password'); ``` --- ## Read Retrieving information from the database. Example: ```sql SELECT * FROM menu_items; ``` --- ## Update Changing existing information. Example: ```sql UPDATE users SET email='new@email.com' WHERE id=1; ``` --- ## Delete Removing records. Example: ```sql DELETE FROM menu_items WHERE id=3; ``` Your application should support CRUD operations where appropriate. --- # Database Entities Your database must contain these entities: --- # 1. User Represents a customer. ## Attributes: - id - Primary Key - username - Required - Unique - password - Stored as hashed value - email - Optional ## Relationship: One user can have many orders. ``` User 1 -------- N Order ``` --- # 2. MenuItem Represents restaurant menu items. ## Attributes: - id - Primary Key - name - description - Optional - price - category - Optional ## Requirement: Database must contain at least 3 initial menu items. Example: |Name|Price|Category| |---|---|---| |Pizza|10|Fast Food| |Burger|8|Fast Food| |Pasta|12|Italian| --- # 3. Order Represents a customer's order. ## Attributes: - id - Primary Key - userId - Foreign Key - createdAt - totalPrice ## Relationships: ``` User 1 -------- N Order ``` ``` Order 1 -------- N OrderDetail ``` --- # 4. OrderDetail Stores each ordered item. ## Attributes: - id - Primary Key - orderId - Foreign Key - menuItemId - Foreign Key - quantity - price Example: Order: ``` Pizza x2 Burger x1 ``` Stored as: |Order ID|Item|Quantity| |---|---|---| |1|Pizza|2| |1|Burger|1| --- # Database Requirements Create: ``` database.sql ``` This file must include: - CREATE TABLE statements - Primary Keys - Foreign Keys - Constraints - Initial data Example: ```sql CREATE TABLE users( id SERIAL PRIMARY KEY, username VARCHAR(50) UNIQUE NOT NULL, password TEXT NOT NULL ); ``` --- # Java + JDBC Requirements Create a Java application that communicates with PostgreSQL using JDBC. All data must be stored inside PostgreSQL. Do not store application data in files. --- # Required Features ## User Management The system must support: ### Register User Insert a new user. Required: - Username - Password - Email (optional) Rules: - Username must be unique - Password must be hashed --- ### Login Validation Check credentials from database. Possible results: - Login successful - Username not found - Wrong password --- # Menu Management Load menu items from database. Example: ``` ID | Name | Price 1 | Pizza | 10 2 | Burger | 8 3 | Pasta | 12 ``` --- # Order Management Users should be able to create orders. Process: 1. Select user 2. Select food items 3. Set quantity 4. Save order Database example: Order: |ID|User ID|Total| |---|---|---| |1|5|28| OrderDetail: |Order ID|Item|Quantity| |---|---|---| |1|Pizza|2| |1|Burger|1| --- # Receipt Generation After creating an order, retrieve the order information from database. Receipt must include: - Food name - Quantity - Unit price - Item total price - Final order price Example: ``` Pizza Quantity: 2 Unit Price: 10 Total: 20 Burger Quantity: 1 Unit Price: 8 Total: 8 Final Price: 28 ``` --- # Order History (Bonus) Users can view previous orders. --- # Database Rules Required: - Every table must have Primary Key - Relationships must use Foreign Keys - Username must be UNIQUE - Required fields must be NOT NULL - Price must be positive --- # Transaction Requirement Creating an order must use a database transaction. Steps: 1. Insert Order 2. Insert OrderDetails If any step fails: Rollback the entire operation. Example: ``` BEGIN TRANSACTION Insert Order Insert OrderDetails COMMIT ``` --- # Evaluation Criteria 📃 ## Database Design - Correct schema design - Proper relationships - Correct constraints - SQL initialization file ## JDBC Integration - PostgreSQL connection - Correct SQL queries - PreparedStatement usage - Exception handling ## Functionality The application must support: - Creating users - Login verification - Reading menu items - Creating orders - Saving order details - Generating receipts - Viewing order history ## Code Quality The code should: - Follow Object-Oriented principles - Have clean structure - Avoid duplicated code - Separate database logic from application logic ---