Develop #1
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>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="SqlNoDataSourceInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
||||
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="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>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://mirror-maven.runflare.com/maven2" />
|
||||
</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>
|
||||
+43
-132
@@ -1,141 +1,52 @@
|
||||
-- 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 SERIAL PRIMARY KEY,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
password VARCHAR(100) NOT NULL,
|
||||
email VARCHAR(100)
|
||||
);
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
-- 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 menu_items (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
description TEXT,
|
||||
price NUMERIC(10, 2) NOT NULL CHECK (price > 0),
|
||||
category VARCHAR(50)
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE orders (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
total_price NUMERIC(10, 2) NOT NULL CHECK (total_price >= 0)
|
||||
);
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
-- 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 order_details (
|
||||
id SERIAL PRIMARY KEY,
|
||||
order_id INTEGER NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
|
||||
menu_item_id INTEGER NOT NULL REFERENCES menu_items(id),
|
||||
quantity INTEGER NOT NULL CHECK (quantity > 0),
|
||||
price NUMERIC(10, 2) NOT NULL CHECK (price > 0)
|
||||
);
|
||||
|
||||
|
||||
INSERT INTO menu_items (name, description, price, category)
|
||||
VALUES
|
||||
('Pizza', 'Cheese pizza with tomato sauce', 12.50, 'Pizza'),
|
||||
('Burger', 'Beef burger with fries', 9.00, 'Burger'),
|
||||
('Pasta', 'Pasta with Alfredo sauce', 11.00, 'Pasta'),
|
||||
('Cola', 'Cold soft drink', 2.50, 'Drink');
|
||||
|
||||
SELECT * FROM users;
|
||||
SELECT * FROM menu_items;
|
||||
SELECT * FROM orders;
|
||||
SELECT * FROM order_details;
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
-- 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 ...;
|
||||
@@ -1,23 +1,82 @@
|
||||
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.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MenuItemDao {
|
||||
|
||||
public List<MenuItem> findAll() {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all menu items
|
||||
String sql = "SELECT id, name, description, price, category FROM menu_items";
|
||||
|
||||
return null;
|
||||
List<MenuItem> menuItems = new ArrayList<>();
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql);
|
||||
ResultSet resultSet = statement.executeQuery()
|
||||
) {
|
||||
|
||||
while (resultSet.next()) {
|
||||
|
||||
MenuItem menuItem = new MenuItem();
|
||||
|
||||
menuItem.setId(resultSet.getInt("id"));
|
||||
menuItem.setName(resultSet.getString("name"));
|
||||
menuItem.setDescription(resultSet.getString("description"));
|
||||
menuItem.setPrice(resultSet.getDouble("price"));
|
||||
menuItem.setCategory(resultSet.getString("category"));
|
||||
|
||||
menuItems.add(menuItem);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
System.out.println("Error loading menu items: " + e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
return menuItems;
|
||||
}
|
||||
|
||||
public MenuItem findById(int id) {
|
||||
|
||||
// TODO:
|
||||
// Find menu item by id
|
||||
String sql = "SELECT id, name, description, price, category FROM menu_items WHERE id = ?";
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)
|
||||
) {
|
||||
|
||||
statement.setInt(1, id);
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
|
||||
MenuItem menuItem = new MenuItem();
|
||||
|
||||
menuItem.setId(resultSet.getInt("id"));
|
||||
menuItem.setName(resultSet.getString("name"));
|
||||
menuItem.setDescription(resultSet.getString("description"));
|
||||
menuItem.setPrice(resultSet.getDouble("price"));
|
||||
menuItem.setCategory(resultSet.getString("category"));
|
||||
|
||||
return menuItem;
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
System.out.println("Error finding menu item: " + e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,70 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.*;
|
||||
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 (?, ?) RETURNING id";
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)
|
||||
) {
|
||||
|
||||
statement.setInt(1, order.getUserId());
|
||||
statement.setDouble(2, order.getTotalPrice());
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
return resultSet.getInt("id");
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error saving order: " + e.getMessage());
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
String sql = "SELECT id, user_id, created_at, total_price FROM orders WHERE user_id = ?";
|
||||
|
||||
return null;
|
||||
List<Order> orders = new ArrayList<>();
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)
|
||||
) {
|
||||
|
||||
statement.setInt(1, userId);
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
|
||||
Order order = new Order();
|
||||
|
||||
order.setId(resultSet.getInt("id"));
|
||||
order.setUserId(resultSet.getInt("user_id"));
|
||||
order.setCreatedAt(resultSet.getTimestamp("created_at").toLocalDateTime());
|
||||
order.setTotalPrice(resultSet.getDouble("total_price"));
|
||||
|
||||
orders.add(order);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error loading orders: " + e.getMessage());
|
||||
}
|
||||
|
||||
return orders;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +1,74 @@
|
||||
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.sql.SQLException;
|
||||
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, price) VALUES (?, ?, ?, ?)";
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)
|
||||
) {
|
||||
|
||||
statement.setInt(1, detail.getOrderId());
|
||||
statement.setInt(2, detail.getMenuItemId());
|
||||
statement.setInt(3, detail.getQuantity());
|
||||
statement.setDouble(4, detail.getPrice());
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
System.out.println("Error saving order detail: " + e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
String sql = "SELECT id, order_id, menu_item_id, quantity, price FROM order_details WHERE order_id = ?";
|
||||
|
||||
return null;
|
||||
List<OrderDetail> details = new ArrayList<>();
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)
|
||||
) {
|
||||
|
||||
statement.setInt(1, orderId);
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
|
||||
OrderDetail detail = new OrderDetail();
|
||||
|
||||
detail.setId(resultSet.getInt("id"));
|
||||
detail.setOrderId(resultSet.getInt("order_id"));
|
||||
detail.setMenuItemId(resultSet.getInt("menu_item_id"));
|
||||
detail.setQuantity(resultSet.getInt("quantity"));
|
||||
detail.setPrice(resultSet.getDouble("price"));
|
||||
|
||||
details.add(detail);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
System.out.println("Error loading order details: " + e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
return details;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +1,71 @@
|
||||
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;
|
||||
|
||||
public class UserDao {
|
||||
|
||||
public boolean save(User user) {
|
||||
|
||||
// TODO:
|
||||
// Insert user into database
|
||||
String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)
|
||||
) {
|
||||
|
||||
statement.setString(1, user.getUsername());
|
||||
statement.setString(2, user.getPassword());
|
||||
statement.setString(3, user.getEmail());
|
||||
|
||||
int rows = statement.executeUpdate();
|
||||
|
||||
return rows > 0;
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
System.out.println("Error saving user: " + e.getMessage());
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
|
||||
// TODO:
|
||||
// Find a user by username
|
||||
String sql = "SELECT id, username, password, email FROM users WHERE username = ?";
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)
|
||||
) {
|
||||
|
||||
statement.setString(1, username);
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
|
||||
User user = new User();
|
||||
|
||||
user.setId(resultSet.getInt("id"));
|
||||
user.setUsername(resultSet.getString("username"));
|
||||
user.setPassword(resultSet.getString("password"));
|
||||
user.setEmail(resultSet.getString("email"));
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
System.out.println("Error finding user: " + e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
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/postgres"; // DB Server
|
||||
|
||||
private static final String USER = "postgres"; // Your Username
|
||||
|
||||
private static final String PASSWORD = "password"; // Your Password
|
||||
private static final String PASSWORD = "1234"; // Your Password
|
||||
|
||||
private DatabaseConnection() {
|
||||
|
||||
@@ -18,10 +19,9 @@ public class DatabaseConnection {
|
||||
public static Connection getConnection()
|
||||
throws SQLException {
|
||||
|
||||
// TODO:
|
||||
// Return a valid PostgreSQL connection
|
||||
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,13 +3,48 @@ package dev.model;
|
||||
public class MenuItem {
|
||||
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private double price;
|
||||
|
||||
private String category;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,35 @@ public class Order {
|
||||
|
||||
private double totalPrice;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,43 @@ public class OrderDetail {
|
||||
|
||||
private double price;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,35 @@ public class User {
|
||||
|
||||
private String email;
|
||||
|
||||
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,21 +1,48 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.UserDao;
|
||||
import dev.model.User;
|
||||
|
||||
public class AuthService {
|
||||
|
||||
private UserDao userDao = new UserDao();
|
||||
|
||||
public boolean register(String username, String password, String email) {
|
||||
|
||||
// TODO:
|
||||
// Validate and register user
|
||||
if (username == null || username.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
if (password == null || password.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
User existingUser = userDao.findByUsername(username);
|
||||
|
||||
if (existingUser != null) {
|
||||
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);
|
||||
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (user.getPassword().equals(password)) {
|
||||
return user;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,33 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MenuService {
|
||||
|
||||
private MenuItemDao menuItemDao = new MenuItemDao();
|
||||
|
||||
public void showMenu() {
|
||||
|
||||
// TODO:
|
||||
// Display menu items
|
||||
List<MenuItem> menuItems = menuItemDao.findAll();
|
||||
|
||||
if (menuItems.isEmpty()) {
|
||||
System.out.println("Menu is empty.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("===== MENU =====");
|
||||
|
||||
for (MenuItem item : menuItems) {
|
||||
System.out.println(
|
||||
item.getId() + ". " +
|
||||
item.getName() + " - " +
|
||||
item.getDescription() + " - $" +
|
||||
item.getPrice() + " - " +
|
||||
item.getCategory()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +1,141 @@
|
||||
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.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class OrderService {
|
||||
|
||||
private final Scanner scanner = new Scanner(System.in);
|
||||
|
||||
private final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
private final OrderDao orderDao = new OrderDao();
|
||||
private final OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
List<OrderDetail> cart = new ArrayList<>();
|
||||
double totalPrice = 0;
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.print("Enter item id or 0 to finish: ");
|
||||
int itemId = scanner.nextInt();
|
||||
|
||||
if (itemId == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
MenuItem menuItem = menuItemDao.findById(itemId);
|
||||
|
||||
if (menuItem == null) {
|
||||
System.out.println("Item not found.");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.print("Enter quantity: ");
|
||||
int quantity = scanner.nextInt();
|
||||
|
||||
if (quantity <= 0) {
|
||||
System.out.println("Quantity must be greater than 0.");
|
||||
continue;
|
||||
}
|
||||
|
||||
OrderDetail detail = new OrderDetail();
|
||||
detail.setMenuItemId(menuItem.getId());
|
||||
detail.setQuantity(quantity);
|
||||
detail.setPrice(menuItem.getPrice());
|
||||
|
||||
cart.add(detail);
|
||||
|
||||
totalPrice += menuItem.getPrice() * quantity;
|
||||
|
||||
System.out.println("Added " + quantity + "x " + menuItem.getName());
|
||||
}
|
||||
|
||||
if (cart.isEmpty()) {
|
||||
System.out.println("Order cancelled.");
|
||||
return;
|
||||
}
|
||||
|
||||
Order order = new Order();
|
||||
order.setUserId(userId);
|
||||
order.setTotalPrice(totalPrice);
|
||||
|
||||
int orderId = orderDao.save(order);
|
||||
|
||||
if (orderId == -1) {
|
||||
System.out.println("Order could not be saved.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (OrderDetail detail : cart) {
|
||||
detail.setOrderId(orderId);
|
||||
orderDetailDao.save(detail);
|
||||
}
|
||||
|
||||
System.out.println("Order saved successfully.");
|
||||
printReceipt(orderId);
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||
|
||||
if (details.isEmpty()) {
|
||||
System.out.println("No details found for this order.");
|
||||
return;
|
||||
}
|
||||
|
||||
double finalTotal = 0;
|
||||
|
||||
System.out.println();
|
||||
System.out.println("===== RECEIPT =====");
|
||||
|
||||
for (OrderDetail detail : details) {
|
||||
|
||||
MenuItem item = menuItemDao.findById(detail.getMenuItemId());
|
||||
|
||||
double subtotal = detail.getPrice() * detail.getQuantity();
|
||||
finalTotal += subtotal;
|
||||
|
||||
System.out.println(
|
||||
item.getName() +
|
||||
" | Qty: " + detail.getQuantity() +
|
||||
" | Unit: $" + detail.getPrice() +
|
||||
" | Total: $" + subtotal
|
||||
);
|
||||
}
|
||||
|
||||
System.out.println("-------------------");
|
||||
System.out.println("Final Total: $" + finalTotal);
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
List<Order> orders = orderDao.findByUserId(userId);
|
||||
|
||||
if (orders.isEmpty()) {
|
||||
System.out.println("No orders found.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("===== ORDER HISTORY =====");
|
||||
|
||||
for (Order order : orders) {
|
||||
System.out.println(
|
||||
"Order #" + order.getId() +
|
||||
" | Date: " + order.getCreatedAt() +
|
||||
" | Total: $" + order.getTotalPrice()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +1,117 @@
|
||||
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();
|
||||
|
||||
public void start() {
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.println();
|
||||
System.out.println("===== JAVA PIZZERIA =====");
|
||||
System.out.println("1. Login");
|
||||
System.out.println("2. Register");
|
||||
System.out.println("3. Exit");
|
||||
System.out.println("===== RESTAURANT SYSTEM =====");
|
||||
System.out.println("1. Register");
|
||||
System.out.println("2. Login");
|
||||
System.out.println("0. Exit");
|
||||
System.out.print("Choose: ");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1:
|
||||
// TODO
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// TODO
|
||||
break;
|
||||
|
||||
case 3:
|
||||
return;
|
||||
|
||||
default:
|
||||
System.out.println("Invalid choice");
|
||||
|
||||
if (choice == 1) {
|
||||
register();
|
||||
} else if (choice == 2) {
|
||||
login();
|
||||
} else if (choice == 0) {
|
||||
System.out.println("Goodbye!");
|
||||
return;
|
||||
} else {
|
||||
System.out.println("Invalid choice.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 login() {
|
||||
|
||||
System.out.print("Username: ");
|
||||
String username = scanner.nextLine();
|
||||
|
||||
System.out.print("Password: ");
|
||||
String password = scanner.nextLine();
|
||||
|
||||
User user = authService.login(username, password);
|
||||
|
||||
if (user == null) {
|
||||
System.out.println("Invalid username or password.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Login successful. Welcome " + user.getUsername());
|
||||
|
||||
userMenu(user);
|
||||
}
|
||||
|
||||
private void userMenu(User user) {
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.println();
|
||||
System.out.println("===== USER MENU =====");
|
||||
System.out.println("1. View Menu");
|
||||
System.out.println("2. Place Order");
|
||||
System.out.println("3. Order History");
|
||||
System.out.println("0. Logout");
|
||||
System.out.print("Choose: ");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
|
||||
if (choice == 1) {
|
||||
menuService.showMenu();
|
||||
} else if (choice == 2) {
|
||||
menuService.showMenu();
|
||||
orderService.placeOrder(user.getId());
|
||||
} else if (choice == 3) {
|
||||
orderService.showOrderHistory(user.getId());
|
||||
} else if (choice == 0) {
|
||||
System.out.println("Logged out.");
|
||||
return;
|
||||
} else {
|
||||
System.out.println("Invalid choice.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user