diff --git a/README.md b/README.md index 7b39875..b3dcbd8 100644 --- a/README.md +++ b/README.md @@ -1,604 +1,268 @@ -# WS-11-Database +# WS-10-Database -# Restaurant Database Management System 🍕🗄️ +# 🍕🗄️ Restaurant Database Management System -## Overview +## 📖 Overview -In this workshop, you will build a **restaurant management system focused on database design, PostgreSQL, and JDBC integration**. +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. +The primary goal of this project is to practice working with relational databases. You will design a well-structured database schema, manage relationships between tables (Primary and Foreign Keys), and connect a Java application to a PostgreSQL database to perform CRUD operations. -You will implement a backend system where users can create accounts, view menu items, create orders, and store all information permanently inside a database. +You will implement a backend system where users can create accounts, view the restaurant's menu, place orders, and store all transaction data permanently in a database. -The project should be implemented using: +**Tech Stack:** -- Java 23 +- **Language:** Java 23 -- Maven +- **Build Tool:** Maven -- PostgreSQL +- **Database:** PostgreSQL -- JDBC +- **API:** JDBC ---- +## ✅ Prerequisites -# Prerequisites ✅ - -Make sure you have installed: +Before starting, ensure you have the following installed on your machine: - Git -- Java 23 +- Java 23 (JDK) - Maven - PostgreSQL -- PostgreSQL Client (pgAdmin is recommended) +- A PostgreSQL Database GUI (e.g., pgAdmin, DBeaver, or DataGrip) ---- +## ⚙️ Maven Configuration -# Maven Configuration +This project relies on **Maven** to manage dependencies. Your project must contain a `pom.xml` file configured for Java 23 and the PostgreSQL JDBC driver. -This project must use **Maven** as the build tool. +**Example `pom.xml` snippet:** -Your project should contain: +XML ``` -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} + org.postgresql + postgresql + ${postgresql.version} - - + ``` -This dependency allows your Java application to connect to PostgreSQL using JDBC. +## 🎯 Objectives +By completing this assignment, you will be able to: ---- - -# Objectives ✏️ - -By completing this assignment, you will: - -- Learn how to design a relational database +- Design a structured relational database schema. -- Understand database relationships +- Understand and implement database relationships (One-to-Many). -- Work with Primary Keys and Foreign Keys +- Work effectively with **Primary Keys (PK)** and **Foreign Keys (FK)**. -- Use SQL to create and manage tables +- Write SQL scripts to create tables and insert initial data. -- Connect Java applications to PostgreSQL using JDBC +- Connect a Java application to PostgreSQL using JDBC. -- Implement CRUD operations +- Implement **CRUD** (Create, Read, Update, Delete) operations via Java. -- Handle database transactions +- Build a modular and object-oriented backend application. -- Build a structured backend application +## 🏗️ Database Entities & Schema ---- +Your database must be created via a `database.sql` script. This file must include all `CREATE TABLE` statements, keys, constraints, and initial mock data. -# Important Concepts +Here are the required entities: -## CRUD Operations +### 1. User (Customer) -CRUD represents the four basic operations used to manage data in a database. +Represents a customer using the system. -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 +- **id:** `SERIAL PRIMARY KEY` - - Primary Key - -- username +- **username:** `VARCHAR` (Required, UNIQUE) - - Required - - - Unique - -- password +- **password:** `TEXT` (Required, stored as a hashed value) - - Stored as hashed value - -- email +- **email:** `VARCHAR` (Optional) + +- _Relationship:_ A User can have many Orders (1-to-N). - - Optional - -## Relationship: +### 2. MenuItem -One user can have many orders. +Represents the food/drink items available in the restaurant. + +- **id:** `SERIAL PRIMARY KEY` + +- **name:** `VARCHAR` (Required) + +- **description:** `TEXT` (Optional) + +- **price:** `NUMERIC` or `DECIMAL` (Required, must be strictly positive) + +- **category:** `VARCHAR` (Optional) + +- _Requirement:_ You must pre-populate this table with at least 3 items using `INSERT` statements in your SQL file. + + +### 3. Order + +Represents a specific order placed by a customer. + +- **id:** `SERIAL PRIMARY KEY` + +- **userId:** `FOREIGN KEY` (References `User.id`) + +- **createdAt:** `TIMESTAMP` (Default to current time) + +- **totalPrice:** `NUMERIC` or `DECIMAL` + +- _Relationship:_ An Order belongs to one User. An Order contains many OrderDetails (1-to-N). + + +### 4. OrderDetail + +Acts as a bridge table storing the specific items and quantities inside a single order. + +- **id:** `SERIAL PRIMARY KEY` + +- **orderId:** `FOREIGN KEY` (References `Order.id`) + +- **menuItemId:** `FOREIGN KEY` (References `MenuItem.id`) + +- **quantity:** `INTEGER` (Must be > 0) + +- **price:** `NUMERIC` (Snapshot of the item's price at the time of purchase) + + +## 💻 Required Features & App Flow + +You must create a Java application that communicates solely with PostgreSQL using JDBC. **Do not store any application data in local text files.** + +### Feature 1: User Management (Auth) + +- **Register:** Insert a new user. Verify the username is unique and hash the password before saving. + +- **Login:** Validate credentials against the database. Handle incorrect username or password scenarios gracefully. + + +### Feature 2: Menu Browsing + +- Fetch and display all available `MenuItem` records from the database. + + +### Feature 3: Order Creation + +- Allow the logged-in user to select items from the menu and specify a quantity. + +- Calculate the total price. + +- Save the `Order` record, and subsequently save the corresponding `OrderDetail` records. + + +### Feature 4: Receipt Generation + +- After an order is placed, query the database to print a detailed receipt. + +- **Must include:** Item names, quantities, unit prices, subtotal per item, and the final grand total. + + +### Feature 5: Order History (Bonus ⭐) + +- Allow a user to view all their past orders and the total amount spent on each. + + +## 📱 Sample Console Menu Template + +To give you an idea of how your application should flow, here is a recommended structure for your Command Line Interface : + +Plaintext ``` -User 1 -------- N Order +======================================= + 🍕 WELCOME TO JAVA PIZZERIA 🍕 +======================================= +1. Login +2. Register New Account +3. Exit +======================================= +Choose an option: 1 + +[Login] +Enter username: *** +Enter password: *** + +======================================= + 🍽️ MAIN MENU 🍽️ +======================================= +1. View Menu +2. Place a New Order +3. View Order History (Bonus) +4. Logout +======================================= +Choose an option: 2 + +[Placing Order] +Available Items: +1. Pizza - $10.00 +2. Burger - $8.00 +3. Pasta - $12.00 + +Enter the ID of the item to add (or 0 to finish): 1 +Enter quantity: 2 +Added 2x Pizza to your cart. + +Enter the ID of the item to add (or 0 to finish): 0 + +[Order Summary / Receipt] +--------------------------------------- +Item Qty Unit Total +--------------------------------------- +Pizza 2 $10.00 $20.00 +--------------------------------------- +Final Total: $20.00 +Order saved successfully! ``` ---- +## 📃 Evaluation Criteria -# 2. MenuItem +Your project will be graded based on the following: -Represents restaurant menu items. +**1. Database Design:** -## Attributes: - -- id +- Correct schema design with appropriately chosen data types. - - Primary Key - -- name +- Proper use of Primary Keys, Foreign Keys, and Constraints (e.g., `NOT NULL`, `UNIQUE`). -- 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 +- A working `database.sql` initialization script. -## Relationships: +**2. JDBC Integration:** -``` -User 1 -------- N Order -``` - -``` -Order 1 -------- N OrderDetail -``` - ---- - -# 4. OrderDetail - -Stores each ordered item. - -## Attributes: - -- id +- Successful connection to PostgreSQL. - - Primary Key - -- orderId +- Use of `PreparedStatement` to prevent SQL injection. - - Foreign Key - -- menuItemId - - - Foreign Key - -- quantity - -- price +- Proper exception handling (`SQLException`). -Example: +**3. Code Quality & OOP:** -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 +- Clean structure (e.g., separating database logic into DAO classes). -- Primary Keys +- Avoidance of code duplication. -- 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 - - ---- +- Application successfully covers all required functionalities (Auth, Menu, Ordering, Receipts). \ No newline at end of file