Merge pull request 'complete' (#1) from develop into main
Reviewed-on: #1 100 / 100 with leniency consider : hashing passwords before storing them in the database
This commit was merged in pull request #1.
This commit is contained in:
Generated
+10
@@ -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/
|
||||
Generated
+13
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="WS-10-Database" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+20
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://maven.devneeds.ir/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+12
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK" />
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
+61
-109
@@ -1,141 +1,93 @@
|
||||
-- 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.
|
||||
|
||||
|
||||
DROP TABLE IF EXISTS order_details CASCADE;
|
||||
DROP TABLE IF EXISTS orders CASCADE;
|
||||
DROP TABLE IF EXISTS menu_items CASCADE;
|
||||
DROP TABLE IF EXISTS users CASCADE;
|
||||
|
||||
-- =======================================================
|
||||
-- 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 ...
|
||||
|
||||
|
||||
CREATE TABLE users (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(100) UNIQUE
|
||||
);
|
||||
|
||||
-- =======================================================
|
||||
-- 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 ...
|
||||
|
||||
|
||||
CREATE TABLE menu_items (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
description TEXT,
|
||||
price NUMERIC(10,2) NOT NULL CHECK (price > 0),
|
||||
category VARCHAR(50)
|
||||
);
|
||||
|
||||
-- =======================================================
|
||||
-- ORDER TABLE
|
||||
-- ORDERS 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 ...
|
||||
|
||||
CREATE TABLE orders (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
total_price NUMERIC(10,2) NOT NULL CHECK (total_price >= 0),
|
||||
|
||||
CONSTRAINT fk_orders_user
|
||||
FOREIGN KEY (user_id)
|
||||
REFERENCES users(id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- =======================================================
|
||||
-- ORDER DETAIL TABLE
|
||||
-- ORDER DETAILS 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 ...
|
||||
|
||||
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),
|
||||
item_price NUMERIC(10,2) NOT NULL CHECK (item_price > 0),
|
||||
|
||||
CONSTRAINT fk_order_details_order
|
||||
FOREIGN KEY (order_id)
|
||||
REFERENCES orders(id)
|
||||
ON DELETE CASCADE,
|
||||
|
||||
CONSTRAINT fk_order_details_menu_item
|
||||
FOREIGN KEY (menu_item_id)
|
||||
REFERENCES menu_items(id)
|
||||
);
|
||||
|
||||
-- =======================================================
|
||||
-- INITIAL MENU DATA
|
||||
-- =======================================================
|
||||
--
|
||||
-- Insert at least 3 food or drink items.
|
||||
--
|
||||
-- Example categories:
|
||||
-- - Pizza
|
||||
-- - Burger
|
||||
-- - Pasta
|
||||
-- - Drink
|
||||
--
|
||||
-- INSERT INTO ...
|
||||
|
||||
|
||||
INSERT INTO menu_items (name, description, price, category)
|
||||
VALUES
|
||||
('Pepperoni Pizza', 'Classic pepperoni pizza', 10.99, 'Pizza'),
|
||||
('Cheese Burger', 'Beef burger with cheese', 8.50, 'Burger'),
|
||||
('Chicken Pasta', 'Creamy chicken pasta', 12.75, 'Pasta'),
|
||||
('Coca Cola', 'Soft drink', 2.50, 'Drink');
|
||||
|
||||
-- =======================================================
|
||||
-- OPTIONAL TEST DATA
|
||||
-- =======================================================
|
||||
--
|
||||
-- You may insert sample users and orders for testing.
|
||||
-- This section is optional.
|
||||
--
|
||||
-- INSERT INTO ...
|
||||
|
||||
INSERT INTO users (username, password_hash, email)
|
||||
VALUES
|
||||
('ali', 'hashed_password_1', 'ali@example.com'),
|
||||
('sara', 'hashed_password_2', 'sara@example.com');
|
||||
|
||||
INSERT INTO orders (user_id, total_price)
|
||||
VALUES
|
||||
(1, 24.48);
|
||||
|
||||
-- =======================================================
|
||||
-- VERIFICATION QUERIES
|
||||
-- =======================================================
|
||||
--
|
||||
-- Uncomment these queries to verify your database.
|
||||
--
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
INSERT INTO order_details (order_id, menu_item_id, quantity, item_price)
|
||||
VALUES
|
||||
(1, 1, 2, 10.99),
|
||||
(1, 4, 1, 2.50);
|
||||
@@ -1,25 +1,72 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MenuItemDao {
|
||||
|
||||
public List<MenuItem> findAll() {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all menu items
|
||||
List<MenuItem> items = new ArrayList<>();
|
||||
|
||||
return null;
|
||||
String sql = "SELECT * FROM menu_items";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
MenuItem item = new MenuItem();
|
||||
item.setId(rs.getInt("id"));
|
||||
item.setName(rs.getString("name"));
|
||||
item.setDescription(rs.getString("description"));
|
||||
item.setPrice(rs.getDouble("price"));
|
||||
item.setCategory(rs.getString("category"));
|
||||
|
||||
items.add(item);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
public MenuItem findById(int id) {
|
||||
|
||||
// TODO:
|
||||
// Find menu item by id
|
||||
String sql = "SELECT * FROM menu_items WHERE id = ?";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setInt(1, id);
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
|
||||
MenuItem item = new MenuItem();
|
||||
item.setId(rs.getInt("id"));
|
||||
item.setName(rs.getString("name"));
|
||||
item.setDescription(rs.getString("description"));
|
||||
item.setPrice(rs.getDouble("price"));
|
||||
item.setCategory(rs.getString("category"));
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +1,91 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDao {
|
||||
|
||||
public int save(Order order) {
|
||||
|
||||
// TODO:
|
||||
// Insert order and return generated id
|
||||
String sql = "INSERT INTO orders (user_id, total_price) VALUES (?, ?)";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
|
||||
ps.setInt(1, order.getUserId());
|
||||
ps.setDouble(2, order.getTotalPrice());
|
||||
|
||||
int affectedRows = ps.executeUpdate();
|
||||
|
||||
if (affectedRows == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ResultSet keys = ps.getGeneratedKeys();
|
||||
|
||||
if (keys.next()) {
|
||||
return keys.getInt(1);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
public void updateTotalPrice(int orderId, double totalPrice) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
String sql = "UPDATE orders SET total_price = ? WHERE id = ?";
|
||||
|
||||
return null;
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setDouble(1, totalPrice);
|
||||
ps.setInt(2, orderId);
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
|
||||
List<Order> orders = new ArrayList<>();
|
||||
|
||||
String sql = "SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setInt(1, userId);
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
Order order = new Order();
|
||||
order.setId(rs.getInt("id"));
|
||||
order.setUserId(rs.getInt("user_id"));
|
||||
order.setCreatedAt(rs.getTimestamp("created_at").toLocalDateTime());
|
||||
order.setTotalPrice(rs.getDouble("total_price"));
|
||||
|
||||
orders.add(order);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return orders;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,64 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDetailDao {
|
||||
|
||||
public void save(OrderDetail detail) {
|
||||
|
||||
// TODO:
|
||||
// Insert order detail
|
||||
String sql = "INSERT INTO order_details (order_id, menu_item_id, quantity, item_price) VALUES (?, ?, ?, ?)";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setLong(1, detail.getOrderId());
|
||||
ps.setLong(2, detail.getMenuItemId());
|
||||
ps.setInt(3, detail.getQuantity());
|
||||
ps.setDouble(4, detail.getPrice());
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
List<OrderDetail> list = new ArrayList<>();
|
||||
|
||||
return null;
|
||||
String sql = "SELECT * FROM order_details WHERE order_id = ?";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setInt(1, orderId);
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
OrderDetail detail = new OrderDetail();
|
||||
detail.setId(rs.getInt("id"));
|
||||
detail.setOrderId(rs.getInt("order_id"));
|
||||
detail.setMenuItemId(rs.getInt("menu_item_id"));
|
||||
detail.setQuantity(rs.getInt("quantity"));
|
||||
detail.setPrice(rs.getDouble("item_price"));
|
||||
|
||||
list.add(detail);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +1,60 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.User;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class UserDao {
|
||||
|
||||
public boolean save(User user) {
|
||||
|
||||
// TODO:
|
||||
// Insert user into database
|
||||
String sql = "INSERT INTO users (username, password_hash, email) VALUES (?, ?, ?)";
|
||||
|
||||
return false;
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setString(1, user.getUsername());
|
||||
ps.setString(2, user.getPassword());
|
||||
ps.setString(3, user.getEmail());
|
||||
|
||||
ps.executeUpdate();
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
|
||||
// TODO:
|
||||
// Find a user by username
|
||||
String sql = "SELECT * FROM users WHERE username = ?";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
|
||||
ps.setString(1, username);
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
if (rs.next()) {
|
||||
|
||||
User user = new User();
|
||||
user.setId(rs.getInt("id"));
|
||||
user.setUsername(rs.getString("username"));
|
||||
user.setPassword(rs.getString("password_hash"));
|
||||
user.setEmail(rs.getString("email"));
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +1,23 @@
|
||||
package dev.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatabaseConnection {
|
||||
|
||||
private static final String URL = "jdbc:postgresql://localhost:5432/restaurant_db"; // DB Server
|
||||
private static final String URL =
|
||||
"jdbc:postgresql://localhost:5432/restaurant_db";
|
||||
|
||||
private static final String USER = "postgres"; // Your Username
|
||||
private static final String USER = "postgres";
|
||||
|
||||
private static final String PASSWORD = "password"; // Your Password
|
||||
private static final String PASSWORD = "1234";
|
||||
|
||||
private DatabaseConnection() {
|
||||
|
||||
}
|
||||
|
||||
public static Connection getConnection()
|
||||
throws SQLException {
|
||||
public static Connection getConnection() throws SQLException {
|
||||
|
||||
// TODO:
|
||||
// Return a valid PostgreSQL connection
|
||||
|
||||
return null;
|
||||
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,13 +3,51 @@ package dev.model;
|
||||
public class MenuItem {
|
||||
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private double price;
|
||||
|
||||
private String category;
|
||||
|
||||
public MenuItem() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,42 @@ import java.time.LocalDateTime;
|
||||
public class Order {
|
||||
|
||||
private int id;
|
||||
|
||||
private int userId;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private double totalPrice;
|
||||
|
||||
public Order() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(LocalDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public double getTotalPrice() {
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
public void setTotalPrice(double totalPrice) {
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,51 @@ package dev.model;
|
||||
public class OrderDetail {
|
||||
|
||||
private int id;
|
||||
|
||||
private int orderId;
|
||||
|
||||
private int menuItemId;
|
||||
|
||||
private int quantity;
|
||||
|
||||
private double price;
|
||||
|
||||
public OrderDetail() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(int orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public int getMenuItemId() {
|
||||
return menuItemId;
|
||||
}
|
||||
|
||||
public void setMenuItemId(int menuItemId) {
|
||||
this.menuItemId = menuItemId;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,42 @@ package dev.model;
|
||||
public class User {
|
||||
|
||||
private int id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String email;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,46 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.UserDao;
|
||||
import dev.model.User;
|
||||
|
||||
public class AuthService {
|
||||
|
||||
private final UserDao userDao = new UserDao();
|
||||
|
||||
public boolean register(String username, String password, String email) {
|
||||
|
||||
// TODO:
|
||||
// Validate and register user
|
||||
if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
|
||||
System.out.println("Username or password cannot be empty!");
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
if (userDao.findByUsername(username) != null) {
|
||||
System.out.println("Username already exists!");
|
||||
return false;
|
||||
}
|
||||
|
||||
User user = new User();
|
||||
user.setUsername(username);
|
||||
user.setPassword(password);
|
||||
user.setEmail(email);
|
||||
|
||||
return userDao.save(user);
|
||||
}
|
||||
|
||||
public User login(String username, String password) {
|
||||
|
||||
// TODO:
|
||||
// Authenticate user
|
||||
User user = userDao.findByUsername(username);
|
||||
|
||||
return null;
|
||||
if (user == null) {
|
||||
System.out.println("User not found!");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!user.getPassword().equals(password)) {
|
||||
System.out.println("Wrong password!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,34 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MenuService {
|
||||
|
||||
private final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
|
||||
public void showMenu() {
|
||||
|
||||
// TODO:
|
||||
// Display menu items
|
||||
List<MenuItem> items = menuItemDao.findAll();
|
||||
|
||||
System.out.println("========== MENU ==========");
|
||||
|
||||
if (items.isEmpty()) {
|
||||
System.out.println("No items available.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (MenuItem item : items) {
|
||||
System.out.println(
|
||||
item.getId() + " - " +
|
||||
item.getName() + " | " +
|
||||
item.getPrice() + " | " +
|
||||
item.getCategory()
|
||||
);
|
||||
}
|
||||
|
||||
System.out.println("==========================");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +1,117 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.dao.OrderDao;
|
||||
import dev.dao.OrderDetailDao;
|
||||
import dev.model.MenuItem;
|
||||
import dev.model.Order;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class OrderService {
|
||||
|
||||
private final OrderDao orderDao = new OrderDao();
|
||||
private final OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||
private final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
|
||||
private final Scanner scanner = new Scanner(System.in);
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
Order order = new Order();
|
||||
order.setUserId(userId);
|
||||
order.setCreatedAt(LocalDateTime.now());
|
||||
|
||||
double total = 0;
|
||||
|
||||
int orderId = orderDao.save(order);
|
||||
|
||||
if (orderId == -1) {
|
||||
System.out.println("Failed to create order!");
|
||||
return;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.println("Enter menu item id (0 to finish): ");
|
||||
int itemId = scanner.nextInt();
|
||||
|
||||
if (itemId == 0) break;
|
||||
|
||||
MenuItem item = menuItemDao.findById(itemId);
|
||||
|
||||
if (item == null) {
|
||||
System.out.println("Item not found!");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println("Enter quantity: ");
|
||||
int qty = scanner.nextInt();
|
||||
|
||||
double itemTotal = item.getPrice() * qty;
|
||||
total += itemTotal;
|
||||
|
||||
OrderDetail detail = new OrderDetail();
|
||||
detail.setOrderId(orderId);
|
||||
detail.setMenuItemId(itemId);
|
||||
detail.setQuantity(qty);
|
||||
detail.setPrice(item.getPrice());
|
||||
|
||||
orderDetailDao.save(detail);
|
||||
|
||||
System.out.println("Added: " + item.getName());
|
||||
}
|
||||
|
||||
order.setTotalPrice(total);
|
||||
orderDao.updateTotalPrice(orderId, total);
|
||||
|
||||
System.out.println("Order placed successfully! Total: " + total);
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||
|
||||
System.out.println("===== RECEIPT =====");
|
||||
|
||||
double total = 0;
|
||||
|
||||
for (OrderDetail d : details) {
|
||||
|
||||
MenuItem item = menuItemDao.findById((int) d.getMenuItemId());
|
||||
|
||||
double lineTotal = d.getPrice() * d.getQuantity();
|
||||
total += lineTotal;
|
||||
|
||||
System.out.println(
|
||||
item.getName() + " x " +
|
||||
d.getQuantity() + " = " +
|
||||
lineTotal
|
||||
);
|
||||
}
|
||||
|
||||
System.out.println("-------------------");
|
||||
System.out.println("TOTAL: " + total);
|
||||
System.out.println("===================");
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
List<Order> orders = orderDao.findByUserId(userId);
|
||||
|
||||
System.out.println("===== ORDER HISTORY =====");
|
||||
|
||||
for (Order o : orders) {
|
||||
System.out.println(
|
||||
"Order ID: " + o.getId() +
|
||||
" | Date: " + o.getCreatedAt() +
|
||||
" | Total: " + o.getTotalPrice()
|
||||
);
|
||||
}
|
||||
|
||||
System.out.println("=========================");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,21 @@
|
||||
package dev.ui;
|
||||
|
||||
import dev.model.User;
|
||||
import dev.service.AuthService;
|
||||
import dev.service.MenuService;
|
||||
import dev.service.OrderService;
|
||||
|
||||
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 final MenuService menuService = new MenuService();
|
||||
private final OrderService orderService = new OrderService();
|
||||
|
||||
private User loggedInUser;
|
||||
|
||||
public void start() {
|
||||
|
||||
@@ -18,15 +28,16 @@ public class ConsoleMenu {
|
||||
System.out.println("3. Exit");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine(); // consume newline
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1:
|
||||
// TODO
|
||||
login();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// TODO
|
||||
register();
|
||||
break;
|
||||
|
||||
case 3:
|
||||
@@ -34,11 +45,84 @@ public class ConsoleMenu {
|
||||
|
||||
default:
|
||||
System.out.println("Invalid choice");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void login() {
|
||||
|
||||
System.out.print("Username: ");
|
||||
String username = scanner.nextLine();
|
||||
|
||||
System.out.print("Password: ");
|
||||
String password = scanner.nextLine();
|
||||
|
||||
loggedInUser = authService.login(username, password);
|
||||
|
||||
if (loggedInUser == null) {
|
||||
System.out.println("Login failed!");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Login successful!");
|
||||
mainMenu();
|
||||
}
|
||||
|
||||
private void register() {
|
||||
|
||||
System.out.print("Username: ");
|
||||
String username = scanner.nextLine();
|
||||
|
||||
System.out.print("Password: ");
|
||||
String password = scanner.nextLine();
|
||||
|
||||
System.out.print("Email: ");
|
||||
String email = scanner.nextLine();
|
||||
|
||||
boolean success = authService.register(username, password, email);
|
||||
|
||||
if (success) {
|
||||
System.out.println("Registration successful!");
|
||||
} else {
|
||||
System.out.println("Registration failed!");
|
||||
}
|
||||
}
|
||||
|
||||
private void mainMenu() {
|
||||
|
||||
while (loggedInUser != null) {
|
||||
|
||||
System.out.println();
|
||||
System.out.println("===== MAIN MENU =====");
|
||||
System.out.println("1. View Menu");
|
||||
System.out.println("2. Place Order");
|
||||
System.out.println("3. View Order History");
|
||||
System.out.println("4. Logout");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1:
|
||||
menuService.showMenu();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
orderService.placeOrder(loggedInUser.getId());
|
||||
break;
|
||||
|
||||
case 3:
|
||||
orderService.showOrderHistory(loggedInUser.getId());
|
||||
break;
|
||||
|
||||
case 4:
|
||||
loggedInUser = null;
|
||||
System.out.println("Logged out!");
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Invalid choice");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user