7.0 KiB
WS-10-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 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 the restaurant's menu, place orders, and store all transaction data permanently in a database.
Tech Stack:
-
Language: Java 23
-
Build Tool: Maven
-
Database: PostgreSQL
-
API: JDBC
✅ Prerequisites
Before starting, ensure you have the following installed on your machine:
-
Git
-
Java 23 (JDK)
-
Maven
-
PostgreSQL
-
A PostgreSQL Database GUI (e.g., pgAdmin, DBeaver, or DataGrip)
⚙️ 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.
Example pom.xml snippet:
XML
<properties>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<postgresql.version>42.7.8</postgresql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
</dependencies>
🎯 Objectives
By completing this assignment, you will be able to:
-
Design a structured relational database schema.
-
Understand and implement database relationships (One-to-Many).
-
Work effectively with Primary Keys (PK) and Foreign Keys (FK).
-
Write SQL scripts to create tables and insert initial data.
-
Connect a Java application to PostgreSQL using JDBC.
-
Implement CRUD (Create, Read, Update, Delete) operations via Java.
-
Build a modular and object-oriented 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.
Here are the required entities:
1. User (Customer)
Represents a customer using the system.
-
id:
SERIAL PRIMARY KEY -
username:
VARCHAR(Required, UNIQUE) -
password:
TEXT(Required, stored as a hashed value) -
email:
VARCHAR(Optional) -
Relationship: A User can have many Orders (1-to-N).
2. MenuItem
Represents the food/drink items available in the restaurant.
-
id:
SERIAL PRIMARY KEY -
name:
VARCHAR(Required) -
description:
TEXT(Optional) -
price:
NUMERICorDECIMAL(Required, must be strictly positive) -
category:
VARCHAR(Optional) -
Requirement: You must pre-populate this table with at least 3 items using
INSERTstatements in your SQL file.
3. Order
Represents a specific order placed by a customer.
-
id:
SERIAL PRIMARY KEY -
userId:
FOREIGN KEY(ReferencesUser.id) -
createdAt:
TIMESTAMP(Default to current time) -
totalPrice:
NUMERICorDECIMAL -
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(ReferencesOrder.id) -
menuItemId:
FOREIGN KEY(ReferencesMenuItem.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
MenuItemrecords 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
Orderrecord, and subsequently save the correspondingOrderDetailrecords.
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
=======================================
🍕 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
Your project will be graded based on the following:
1. Database Design:
-
Correct schema design with appropriately chosen data types.
-
Proper use of Primary Keys, Foreign Keys, and Constraints (e.g.,
NOT NULL,UNIQUE). -
A working
database.sqlinitialization script.
2. JDBC Integration:
-
Successful connection to PostgreSQL.
-
Use of
PreparedStatementto prevent SQL injection. -
Proper exception handling (
SQLException).
3. Code Quality & OOP:
-
Clean structure (e.g., separating database logic into DAO classes).
-
Avoidance of code duplication.
-
Application successfully covers all required functionalities (Auth, Menu, Ordering, Receipts).