Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd6e81bcd5 | ||
|
|
3573dc1f58 | ||
|
|
de2c9530c7 | ||
|
|
d26975c816 | ||
|
|
878ebcdc6c | ||
|
|
884e95957d | ||
|
|
d5f3f4f38f | ||
|
|
62de8172ed | ||
|
|
0bb90c8c62 | ||
|
|
8f1cf660db | ||
|
|
7ed7206372 | ||
|
|
17ae2dfeaf | ||
|
|
1564167420 | ||
|
|
55763a1c46 | ||
|
|
a58476f109 |
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
+17
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||||
|
<data-source source="LOCAL" name="postgres@localhost" uuid="f6d07e17-51cf-4ac6-b639-e1093cb323b7">
|
||||||
|
<driver-ref>postgresql</driver-ref>
|
||||||
|
<synchronize>true</synchronize>
|
||||||
|
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||||
|
<jdbc-url>jdbc:postgresql://localhost:5432/postgres</jdbc-url>
|
||||||
|
<jdbc-additional-properties>
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||||
|
</jdbc-additional-properties>
|
||||||
|
<working-dir>$ProjectFileDir$</working-dir>
|
||||||
|
</data-source>
|
||||||
|
</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="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="21" project-jdk-type="JavaSDK" />
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="SqlDialectMappings">
|
||||||
|
<file url="file://$PROJECT_DIR$/database.sql" dialect="GenericSQL" />
|
||||||
|
</component>
|
||||||
|
</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>
|
||||||
+37
-132
@@ -1,141 +1,46 @@
|
|||||||
-- Restaurant Database Management System
|
CREATE TABLE users (
|
||||||
--
|
id SERIAL PRIMARY KEY,
|
||||||
-- Instructions:
|
username VARCHAR(20) NOT NULL UNIQUE,
|
||||||
-- 1. Create all required tables.
|
password VARCHAR(20) NOT NULL,
|
||||||
-- 2. Design appropriate PRIMARY KEY and FOREIGN KEY relationships.
|
email VARCHAR(100) UNIQUE
|
||||||
-- 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 menu_items(
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(50) NOT NULL ,
|
||||||
|
description TEXT,
|
||||||
|
price NUMERIC(10, 2) NOT NULL CHECK ( price>0 ),
|
||||||
|
category VARCHAR(50)
|
||||||
|
);
|
||||||
|
|
||||||
-- =======================================================
|
CREATE TABLE orders(
|
||||||
-- USER TABLE
|
id SERIAL PRIMARY KEY,
|
||||||
-- =======================================================
|
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
--
|
created_at TIMESTAMP NOT NULL,
|
||||||
-- Represents customers using the system.
|
total_price NUMERIC(10, 2) NOT NULL CHECK (total_price >= 0)
|
||||||
--
|
|
||||||
-- 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 order_detail(
|
||||||
-- =======================================================
|
id SERIAL PRIMARY KEY,
|
||||||
-- MENU ITEM TABLE
|
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),
|
||||||
-- Represents available food and drink items.
|
price NUMERIC(10, 2) NOT NULL CHECK (price > 0)
|
||||||
--
|
);
|
||||||
-- 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 ...
|
|
||||||
|
|
||||||
|
|
||||||
|
INSERT INTO menu_items (name, description, price, category)
|
||||||
|
VALUES
|
||||||
|
('Margherita Classica', 'Tomato sauce, fresh mozzarella, and basil on a crispy thin crust', 16.00, 'Pizza'),
|
||||||
|
('Smoked BBQ Bacon Burger', 'Angus beef with cheddar, bacon, onion rings, and BBQ sauce on a brioche bun.', 14.50, 'Burger'),
|
||||||
|
('Truffle Mushroom Fettuccine', 'Creamy fettuccine with mushrooms, Parmesan, and a touch of truffle oil.', 18.00, 'Pasta'),
|
||||||
|
('Cola', 'Cold soft drink', 2.50, 'Drink');
|
||||||
|
|
||||||
-- =======================================================
|
|
||||||
-- ORDER TABLE
|
|
||||||
-- =======================================================
|
|
||||||
--
|
--
|
||||||
-- Represents orders placed by customers.
|
SELECT * FROM users;
|
||||||
--
|
SELECT * FROM menu_items;
|
||||||
-- Required information:
|
SELECT * FROM orders;
|
||||||
-- - Unique identifier
|
SELECT * FROM order_detail;
|
||||||
-- - 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,74 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.MenuItem;
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
public class MenuItemDao {
|
public class MenuItemDao {
|
||||||
|
|
||||||
public List<MenuItem> findAll() {
|
public List<MenuItem> findAll() {
|
||||||
|
|
||||||
// TODO:
|
List<MenuItem> items = new ArrayList<>();
|
||||||
// Retrieve all menu items
|
|
||||||
|
|
||||||
return null;
|
// Retrieve all menu items
|
||||||
|
try ( Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM menu_items");
|
||||||
|
ResultSet resultSet = preparedStatement.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"));
|
||||||
|
|
||||||
|
items.add(menuItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
public MenuItem findById(int id) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Find menu item by id
|
// Find menu item by id
|
||||||
|
String sql = "SELECT * FROM menu_items WHERE id=?";
|
||||||
|
|
||||||
|
try (Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, id);
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.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) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,67 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.Order;
|
import dev.model.Order;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OrderDao {
|
public class OrderDao {
|
||||||
|
|
||||||
public int save(Order order) {
|
public int save(Order order) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Insert order and return generated id
|
// Insert order and return generated id
|
||||||
|
String sql = "INSERT INTO orders (user_id, total_price, created_at) VALUES(?, ?, ?) RETURNING id";
|
||||||
|
|
||||||
|
try (Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(sql)){
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, order.getUserId());
|
||||||
|
preparedStatement.setDouble(2, order.getTotalPrice());
|
||||||
|
preparedStatement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()));
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
if(resultSet.next()) return resultSet.getInt("id");
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Order> findByUserId(int userId) {
|
public List<Order> findByUserId(int userId) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Retrieve all orders of a user
|
// Retrieve all orders of a user
|
||||||
|
String sql = "SELECT * FROM orders WHERE user_id=?";
|
||||||
|
List<Order> orders = new ArrayList<>();
|
||||||
|
|
||||||
return null;
|
try(Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(sql) ) {
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, userId);
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.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) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return orders;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,74 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.OrderDetail;
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
public class OrderDetailDao {
|
public class OrderDetailDao {
|
||||||
|
|
||||||
public void save(OrderDetail detail) {
|
public void save(OrderDetail detail) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Insert order detail
|
// Insert order detail
|
||||||
|
|
||||||
|
String sql = "INSERT INTO order_detail (order_id, menu_item_id, quantity, price) VALUES(?, ?, ?, ?)";
|
||||||
|
|
||||||
|
try(Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, detail.getOrderId());
|
||||||
|
preparedStatement.setInt(2, detail.getMenuItemId());
|
||||||
|
preparedStatement.setInt(3, detail.getQuantity());
|
||||||
|
preparedStatement.setDouble(4, detail.getPrice());
|
||||||
|
|
||||||
|
preparedStatement.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<OrderDetail> findByOrderId(int orderId) {
|
public List<OrderDetail> findByOrderId(int orderId) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Retrieve order details
|
// Retrieve order details
|
||||||
|
|
||||||
return null;
|
String sql = "SELECT * FROM order_detail WHERE order_id=?";
|
||||||
|
|
||||||
|
List<OrderDetail> orderDetails = new ArrayList<>();
|
||||||
|
|
||||||
|
try(Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, orderId);
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
|
||||||
|
OrderDetail orderDetail = new OrderDetail();
|
||||||
|
|
||||||
|
orderDetail.setId(resultSet.getInt("id"));
|
||||||
|
orderDetail.setOrderId(resultSet.getInt("order_id"));
|
||||||
|
orderDetail.setMenuItemId(resultSet.getInt("menu_item_id"));
|
||||||
|
orderDetail.setQuantity(resultSet.getInt("quantity"));
|
||||||
|
orderDetail.setPrice(resultSet.getDouble("price"));
|
||||||
|
|
||||||
|
orderDetails.add(orderDetail);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return orderDetails;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,63 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class UserDao {
|
public class UserDao {
|
||||||
|
|
||||||
public boolean save(User user) {
|
public boolean save(User user) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Insert user into database
|
// Insert user into database
|
||||||
|
|
||||||
return false;
|
String sql = "INSERT INTO users (username, password, email) VALUES(?, ?, ?)";
|
||||||
|
|
||||||
|
try(Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
preparedStatement.setString(1, user.getUsername());
|
||||||
|
preparedStatement.setString(2, user.getPassword());
|
||||||
|
preparedStatement.setString(3, user.getEmail());
|
||||||
|
|
||||||
|
return preparedStatement.executeUpdate() > 0;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUsername(String username) {
|
public User findByUsername(String username) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Find a user by username
|
// Find a user by username
|
||||||
|
// Retrieve all orders of a user
|
||||||
|
String sql = "SELECT * FROM users WHERE username=?";
|
||||||
|
|
||||||
|
try (Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
preparedStatement.setString(1, username);
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.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) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,16 @@
|
|||||||
package dev.database;
|
package dev.database;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class DatabaseConnection {
|
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 USER = "postgres"; // Your Username
|
||||||
|
|
||||||
private static final String PASSWORD = "password"; // Your Password
|
private static final String PASSWORD = "1234"; // Your Password
|
||||||
|
|
||||||
private DatabaseConnection() {
|
private DatabaseConnection() {
|
||||||
|
|
||||||
@@ -18,10 +19,8 @@ public class DatabaseConnection {
|
|||||||
public static Connection getConnection()
|
public static Connection getConnection()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Return a valid PostgreSQL connection
|
// Return a valid PostgreSQL connection
|
||||||
|
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,15 @@ public class MenuItem {
|
|||||||
|
|
||||||
private String category;
|
private String category;
|
||||||
|
|
||||||
|
public void setId(int id) { this.id = id; }
|
||||||
|
public void setName(String name) { this.name = name; }
|
||||||
|
public void setDescription(String description) { this.description = description; }
|
||||||
|
public void setPrice(double price) { this.price = price; }
|
||||||
|
public void setCategory(String category) { this.category = category; }
|
||||||
|
|
||||||
|
public int getId() { return id; }
|
||||||
|
public String getName() { return name; }
|
||||||
|
public String getDescription() { return description; }
|
||||||
|
public double getPrice() { return price; }
|
||||||
|
public String getCategory() { return category; }
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,13 @@ public class Order {
|
|||||||
|
|
||||||
private double totalPrice;
|
private double totalPrice;
|
||||||
|
|
||||||
|
public void setId(int id) { this.id = id; }
|
||||||
|
public void setUserId(int userId) { this.userId = userId; }
|
||||||
|
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
|
||||||
|
public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; }
|
||||||
|
|
||||||
|
public int getId() { return id; }
|
||||||
|
public double getTotalPrice() { return totalPrice; }
|
||||||
|
public int getUserId() { return userId; }
|
||||||
|
public LocalDateTime getCreatedAt() { return createdAt; }
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,17 @@ public class OrderDetail {
|
|||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
|
public void setId(int id) { this.id = id; }
|
||||||
|
public void setOrderId(int orderId) { this.orderId = orderId; }
|
||||||
|
public void setMenuItemId(int menuItemId) { this.menuItemId = menuItemId; }
|
||||||
|
public void setQuantity(int quantity) { this.quantity = quantity; }
|
||||||
|
public void setPrice(double price) { this.price = price; }
|
||||||
|
|
||||||
|
|
||||||
|
public int getId() { return id; }
|
||||||
|
public int getOrderId() { return orderId; }
|
||||||
|
public int getMenuItemId() { return menuItemId; }
|
||||||
|
public int getQuantity() { return quantity; }
|
||||||
|
public double getPrice() { return price; }
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,4 +10,15 @@ public class User {
|
|||||||
|
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
|
||||||
|
public void setId(int id) { this.id = id; }
|
||||||
|
public void setUsername(String username) { this.username= username; }
|
||||||
|
public void setPassword(String password) { this.password = password; }
|
||||||
|
public void setEmail(String email) { this.email = email; }
|
||||||
|
|
||||||
|
|
||||||
|
public int getId() { return id; }
|
||||||
|
public String getUsername() { return username; }
|
||||||
|
public String getEmail() { return email; }
|
||||||
|
public String getPassword() { return password; }
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,39 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.UserDao;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
public class AuthService {
|
public class AuthService {
|
||||||
|
|
||||||
|
UserDao userDao = new UserDao();
|
||||||
|
|
||||||
public boolean register(String username, String password, String email) {
|
public boolean register(String username, String password, String email) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Validate and register user
|
// Validate and register user
|
||||||
|
if( username == null || username.isBlank() || password == null || password.isBlank())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (userDao.findByUsername(username) == null) {
|
||||||
|
|
||||||
|
User user = new User();
|
||||||
|
|
||||||
|
user.setEmail(email);
|
||||||
|
user.setPassword(password);
|
||||||
|
user.setUsername(username);
|
||||||
|
|
||||||
|
return userDao.save(user);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User login(String username, String password) {
|
public User login(String username, String password) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Authenticate user
|
// Authenticate user
|
||||||
|
User user = userDao.findByUsername(username);
|
||||||
|
|
||||||
|
if (user != null && user.getPassword().equals(password)) {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,25 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.MenuItemDao;
|
||||||
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class MenuService {
|
public class MenuService {
|
||||||
|
|
||||||
public void showMenu() {
|
public void showMenu() {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Display menu items
|
// Display menu items
|
||||||
|
MenuItemDao menuItemDao = new MenuItemDao();
|
||||||
|
|
||||||
|
List<MenuItem> items = menuItemDao.findAll();
|
||||||
|
|
||||||
|
for ( MenuItem menuItem : items) {
|
||||||
|
|
||||||
|
System.out.println(menuItem.getId() + ". " + menuItem.getName() +
|
||||||
|
" " + menuItem.getPrice() + "$ \n" +
|
||||||
|
menuItem.getCategory() + " | " + menuItem.getDescription());
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,25 +1,119 @@
|
|||||||
package dev.service;
|
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 {
|
public class OrderService {
|
||||||
|
|
||||||
|
MenuItemDao menuItemDao = new MenuItemDao();
|
||||||
|
OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||||
|
OrderDao orderDao = new OrderDao();
|
||||||
|
|
||||||
|
Scanner in = new Scanner(System.in);
|
||||||
|
|
||||||
public void placeOrder(int userId) {
|
public void placeOrder(int userId) {
|
||||||
|
|
||||||
// TODO:
|
List<OrderDetail> details = new ArrayList<>();
|
||||||
// Create order
|
int totalPrice = 0;
|
||||||
|
|
||||||
|
// Create order
|
||||||
|
while (true) {
|
||||||
|
System.out.println("Enter the id of your order (-1 to exit) : ");
|
||||||
|
int id = in.nextInt();
|
||||||
|
|
||||||
|
if(id==-1) break;
|
||||||
|
|
||||||
|
MenuItem menuItem = menuItemDao.findById(id);
|
||||||
|
|
||||||
|
if(menuItem != null) {
|
||||||
|
|
||||||
|
System.out.println("Enter quantity: ");
|
||||||
|
int quantity = in.nextInt();
|
||||||
|
|
||||||
|
if(quantity<=0) {
|
||||||
|
System.out.println("Quantity can't be negative");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
OrderDetail orderDetail = new OrderDetail();
|
||||||
|
|
||||||
|
orderDetail.setPrice(menuItem.getPrice());
|
||||||
|
orderDetail.setQuantity(quantity);
|
||||||
|
orderDetail.setMenuItemId(id);
|
||||||
|
|
||||||
|
details.add(orderDetail);
|
||||||
|
totalPrice += menuItem.getPrice()*quantity;
|
||||||
|
|
||||||
|
System.out.println(quantity+ " " + menuItem.getName() + "(s) got added to your order.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (totalPrice<=0) return;
|
||||||
|
|
||||||
|
Order order = new Order();
|
||||||
|
order.setTotalPrice(totalPrice);
|
||||||
|
order.setUserId(userId);
|
||||||
|
int orderId = orderDao.save(order);
|
||||||
|
|
||||||
|
if (orderId == -1) {
|
||||||
|
System.out.println("Order could not be saved");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(OrderDetail orderDetail : details) {
|
||||||
|
orderDetail.setOrderId(orderId);
|
||||||
|
orderDetailDao.save(orderDetail);
|
||||||
|
}
|
||||||
|
|
||||||
|
printReceipt(orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printReceipt(int orderId) {
|
public void printReceipt(int orderId) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Print order receipt
|
// Print order receipt
|
||||||
|
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||||
|
|
||||||
|
if (details.isEmpty()) return;
|
||||||
|
|
||||||
|
double totalPrice = 0;
|
||||||
|
|
||||||
|
System.out.println("\n============ RECEIPT ============");
|
||||||
|
|
||||||
|
for (OrderDetail detail : details) {
|
||||||
|
|
||||||
|
MenuItem item = menuItemDao.findById(detail.getMenuItemId());
|
||||||
|
|
||||||
|
double subtotal = detail.getPrice() * detail.getQuantity();
|
||||||
|
totalPrice += subtotal;
|
||||||
|
|
||||||
|
System.out.println( item.getName() + " | Quantity: " + detail.getQuantity() +
|
||||||
|
" | Price: " + detail.getPrice() + "$ | Total: " + subtotal + "$");
|
||||||
|
}
|
||||||
|
System.out.println("==============================");
|
||||||
|
System.out.println("Final Total: $" + totalPrice);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showOrderHistory(int userId) {
|
public void showOrderHistory(int userId) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Display user's order history
|
// Display user's order history
|
||||||
|
List<Order> orders = orderDao.findByUserId(userId);
|
||||||
|
|
||||||
|
if (orders.isEmpty()) return;
|
||||||
|
|
||||||
|
System.out.println("\n===== ORDER HISTORY =====");
|
||||||
|
|
||||||
|
for (Order order : orders) {
|
||||||
|
System.out.println( "Order #" + order.getId() + " | Date: " + order.getCreatedAt() +
|
||||||
|
" | Total: " + order.getTotalPrice() + "$");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,21 @@
|
|||||||
package dev.ui;
|
package dev.ui;
|
||||||
|
|
||||||
|
import dev.model.User;
|
||||||
|
import dev.service.AuthService;
|
||||||
|
import dev.service.MenuService;
|
||||||
|
import dev.service.OrderService;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ConsoleMenu {
|
public class ConsoleMenu {
|
||||||
|
|
||||||
private final Scanner scanner =
|
private final Scanner in =
|
||||||
new Scanner(System.in);
|
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() {
|
public void start() {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -17,16 +26,17 @@ public class ConsoleMenu {
|
|||||||
System.out.println("2. Register");
|
System.out.println("2. Register");
|
||||||
System.out.println("3. Exit");
|
System.out.println("3. Exit");
|
||||||
|
|
||||||
int choice = scanner.nextInt();
|
int choice = in.nextInt();
|
||||||
|
in.nextLine();
|
||||||
|
|
||||||
switch (choice) {
|
switch (choice) {
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
// TODO
|
login();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
// TODO
|
register();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
@@ -41,4 +51,78 @@ public class ConsoleMenu {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void login() {
|
||||||
|
|
||||||
|
System.out.println("Enter your username: ");
|
||||||
|
String username = in.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Enter your password: ");
|
||||||
|
String password = in.nextLine();
|
||||||
|
|
||||||
|
User user = authService.login(username, password);
|
||||||
|
|
||||||
|
if(user != null) {
|
||||||
|
System.out.println("Welcome back, " + username);
|
||||||
|
showOptions(user);
|
||||||
|
}
|
||||||
|
else System.out.println("Wrong username or password.");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void register() {
|
||||||
|
|
||||||
|
System.out.println("Enter your username: ");
|
||||||
|
String username = in.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Enter password: ");
|
||||||
|
String password = in.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Enter your email: ");
|
||||||
|
String email = in.nextLine();
|
||||||
|
|
||||||
|
if (authService.register(username, password, email)) {
|
||||||
|
System.out.println("Welcome!");
|
||||||
|
}
|
||||||
|
else System.out.println("Registration failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showOptions(User user) {
|
||||||
|
|
||||||
|
System.out.println("""
|
||||||
|
=======================================
|
||||||
|
\uD83C\uDF7D️ MAIN MENU \uD83C\uDF7D️
|
||||||
|
=======================================""");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
System.out.println("1. View Menu\n" +
|
||||||
|
"2. Place a New Order\n" +
|
||||||
|
"3. View Order History \n" +
|
||||||
|
"4. Logout");
|
||||||
|
|
||||||
|
int choice = in.nextInt();
|
||||||
|
|
||||||
|
switch (choice) {
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
menuService.showMenu();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
orderService.placeOrder(user.getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
orderService.showOrderHistory(user.getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user