Merge pull request 'Develop' (#1) from develop into main
Reviewed-on: #1
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>
|
||||||
+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://maven.myket.ir/" />
|
||||||
|
</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="23" project-jdk-type="JavaSDK" />
|
||||||
|
</project>
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
+52
-12
@@ -8,7 +8,10 @@
|
|||||||
-- 5. Insert at least 3 menu items.
|
-- 5. Insert at least 3 menu items.
|
||||||
-- 6. The script should be executable from start to finish without errors.
|
-- 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
|
-- USER TABLE
|
||||||
@@ -28,9 +31,12 @@
|
|||||||
-- - Username and password are required.
|
-- - Username and password are required.
|
||||||
-- - Passwords should not be stored in plain text.
|
-- - Passwords should not be stored in plain text.
|
||||||
--
|
--
|
||||||
-- CREATE TABLE ...
|
CREATE TABLE users (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
username VARCHAR(50) UNIQUE NOT NULL,
|
||||||
|
password VARCHAR(250) NOT NULL,
|
||||||
|
email VARCHAR(100)
|
||||||
|
);
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- MENU ITEM TABLE
|
-- MENU ITEM TABLE
|
||||||
@@ -50,9 +56,12 @@
|
|||||||
-- - Name is required.
|
-- - Name is required.
|
||||||
-- - Price must always be positive.
|
-- - Price must always be positive.
|
||||||
--
|
--
|
||||||
-- CREATE TABLE ...
|
CREATE TABLE menu_items (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(100),
|
||||||
|
price DECIMAL(10,2) NOT NULL CHECK (price > 0),
|
||||||
|
category VARCHAR(50)
|
||||||
|
);
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- ORDER TABLE
|
-- ORDER TABLE
|
||||||
@@ -75,9 +84,15 @@
|
|||||||
-- Avoid using reserved SQL keywords as table names.
|
-- Avoid using reserved SQL keywords as table names.
|
||||||
-- Consider using a name such as "orders" or "customer_orders".
|
-- Consider using a name such as "orders" or "customer_orders".
|
||||||
--
|
--
|
||||||
-- CREATE TABLE ...
|
CREATE TABLE orders (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
user_id INTEGER NOT NULL,
|
||||||
|
orderDate TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
total_price DECIMAL(10,2) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT fk_orders
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- ORDER DETAIL TABLE
|
-- ORDER DETAIL TABLE
|
||||||
@@ -98,7 +113,21 @@
|
|||||||
-- - Quantity must always be greater than zero.
|
-- - Quantity must always be greater than zero.
|
||||||
-- - Store the item's price at the moment of purchase.
|
-- - Store the item's price at the moment of purchase.
|
||||||
--
|
--
|
||||||
-- CREATE TABLE ...
|
CREATE TABLE order_details (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
order_id INTEGER NOT NULL,
|
||||||
|
menu_items_id INTEGER NOT NULL,
|
||||||
|
quantity INTEGER NOT NULL CHECK (quantity>0),
|
||||||
|
price_at_purchase DECIMAL(10,2) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT fk_order_detail_order
|
||||||
|
FOREIGN KEY (order_id) REFERENCES orders(id)
|
||||||
|
ON DELETE CASCADE,
|
||||||
|
|
||||||
|
CONSTRAINT fk_menu_items_id
|
||||||
|
FOREIGN KEY (menu_items_id) REFERENCES menu_items(id)
|
||||||
|
ON DELETE RESTRICT
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -114,7 +143,11 @@
|
|||||||
-- - Pasta
|
-- - Pasta
|
||||||
-- - Drink
|
-- - Drink
|
||||||
--
|
--
|
||||||
-- INSERT INTO ...
|
INSERT INTO menu_items (name, price, category) VALUES
|
||||||
|
('Margherita', 9.99, 'Pizza'),
|
||||||
|
('Cheeseburger', 8.50, 'Burger'),
|
||||||
|
('Alfredo pasta', 12.00, 'Pasta'),
|
||||||
|
('Coca Cola', 2.50, 'Drink');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -125,9 +158,16 @@
|
|||||||
-- You may insert sample users and orders for testing.
|
-- You may insert sample users and orders for testing.
|
||||||
-- This section is optional.
|
-- This section is optional.
|
||||||
--
|
--
|
||||||
-- INSERT INTO ...
|
INSERT INTO users (username, password, email) VALUES
|
||||||
|
('aga armin', MD5('123456'), 'ar@example.com'),
|
||||||
|
('mewsoume', MD5('mypass'), 'm@example.com');
|
||||||
|
|
||||||
|
INSERT INTO orders (user_id, total_price) VALUES
|
||||||
|
(1, 22.49);
|
||||||
|
|
||||||
|
INSERT INTO order_details (order_id, menu_items_id, quantity, price_at_purchase) VALUES
|
||||||
|
(1, 1, 2, 9.99),
|
||||||
|
(1, 3, 1, 12.00);
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- VERIFICATION QUERIES
|
-- VERIFICATION QUERIES
|
||||||
|
|||||||
@@ -1,23 +1,65 @@
|
|||||||
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:
|
String sql = "SELECT id, name, price, category FROM menu_items";
|
||||||
// Retrieve all menu items
|
List<MenuItem> items = new ArrayList<>();
|
||||||
|
|
||||||
return null;
|
try(Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||||
|
ResultSet rs = stmt.executeQuery())
|
||||||
|
{
|
||||||
|
while (rs.next())
|
||||||
|
{
|
||||||
|
MenuItem item = new MenuItem();
|
||||||
|
item.setId(rs.getInt("id"));
|
||||||
|
item.setName(rs.getString("name"));
|
||||||
|
item.setPrice(rs.getDouble("price"));
|
||||||
|
item.setCategory(rs.getString("category"));
|
||||||
|
items.add(item);
|
||||||
|
}
|
||||||
|
} catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
public MenuItem findById(int id) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT id, name, price, category FROM menu_items WHERE id = ?";
|
||||||
// Find menu item by id
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
stmt.setInt(1, id);
|
||||||
|
try (ResultSet rs = stmt.executeQuery())
|
||||||
|
{
|
||||||
|
if (rs.next())
|
||||||
|
{
|
||||||
|
MenuItem item = new MenuItem();
|
||||||
|
item.setId(rs.getInt("id"));
|
||||||
|
item.setName(rs.getString("name"));
|
||||||
|
item.setPrice(rs.getDouble("price"));
|
||||||
|
item.setCategory(rs.getString("category"));
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,76 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.Order;
|
import dev.model.Order;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
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:
|
String sql = "INSERT INTO orders (user_id, total_price) VALUES (?, ?)";
|
||||||
// Insert order and return generated id
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS))
|
||||||
|
{
|
||||||
|
stmt.setInt(1, order.getUserId());
|
||||||
|
stmt.setDouble(2, order.getTotalPrice());
|
||||||
|
|
||||||
|
int affectedRows = stmt.executeUpdate();
|
||||||
|
if (affectedRows == 0)
|
||||||
|
{
|
||||||
|
throw new SQLException("unsuccessful");
|
||||||
|
}
|
||||||
|
|
||||||
|
try (ResultSet generatedKeys = stmt.getGeneratedKeys())
|
||||||
|
{
|
||||||
|
if (generatedKeys.next())
|
||||||
|
{
|
||||||
|
return generatedKeys.getInt(1);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
throw new SQLException("unsuccessful");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Order> findByUserId(int userId) {
|
public List<Order> findByUserId(int userId) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT id, user_id, orderDate, total_price FROM orders WHERE user_id = ? ORDER BY orderDate DESC";
|
||||||
// Retrieve all orders of a user
|
List<Order> orders = new ArrayList<>();
|
||||||
|
|
||||||
return null;
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
|
||||||
|
stmt.setInt(1, userId);
|
||||||
|
try (ResultSet rs = stmt.executeQuery())
|
||||||
|
{
|
||||||
|
while (rs.next())
|
||||||
|
{
|
||||||
|
Order order = new Order();
|
||||||
|
order.setId(rs.getInt("id"));
|
||||||
|
order.setUserId(rs.getInt("user_id"));
|
||||||
|
order.setCreatedAt(rs.getTimestamp("orderDate").toLocalDateTime());
|
||||||
|
order.setTotalPrice(rs.getDouble("total_price"));
|
||||||
|
orders.add(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return orders;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,69 @@
|
|||||||
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:
|
String sql = "INSERT INTO order_details (order_id, menu_items_id, quantity, price_at_purchase) VALUES (? ,? ,? ,?)";
|
||||||
// Insert order detail
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
stmt.setInt(1, detail.getOrderId());
|
||||||
|
stmt.setInt(2, detail.getMenuItemId());
|
||||||
|
stmt.setInt(3, detail.getQuantity());
|
||||||
|
stmt.setDouble(4, detail.getPrice());
|
||||||
|
|
||||||
|
stmt.executeUpdate();
|
||||||
|
|
||||||
|
|
||||||
|
} catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<OrderDetail> findByOrderId(int orderId) {
|
public List<OrderDetail> findByOrderId(int orderId) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT id, order_id, menu_items_id, quantity, price_at_purchase FROM order_details WHERE order_id = ?";
|
||||||
// Retrieve order details
|
|
||||||
|
|
||||||
return null;
|
List<OrderDetail> orderDetails = new ArrayList<>();
|
||||||
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
|
||||||
|
stmt.setInt(1, orderId);
|
||||||
|
try (ResultSet rs = stmt.executeQuery())
|
||||||
|
{
|
||||||
|
while (rs.next())
|
||||||
|
{
|
||||||
|
OrderDetail orderDetail = new OrderDetail();
|
||||||
|
orderDetail.setId(rs.getInt("id"));
|
||||||
|
orderDetail.setOrderId(rs.getInt("order_id"));
|
||||||
|
orderDetail.setMenuItemId(rs.getInt("menu_items_id"));
|
||||||
|
orderDetail.setQuantity(rs.getInt("quantity"));
|
||||||
|
orderDetail.setPrice(rs.getDouble("price_at_purchase"));
|
||||||
|
|
||||||
|
orderDetails.add(orderDetail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return orderDetails;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,59 @@
|
|||||||
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:
|
String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
|
||||||
// Insert user into database
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
stmt.setString(1, user.getUsername());
|
||||||
|
stmt.setString(2, user.getPassword());
|
||||||
|
stmt.setString(3, user.getEmail());
|
||||||
|
|
||||||
return false;
|
int affectedRows = stmt.executeUpdate();
|
||||||
|
return affectedRows > 0;
|
||||||
|
} catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUsername(String username) {
|
public User findByUsername(String username) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT id, username, password, email FROM users WHERE username = ?";
|
||||||
// Find a user by username
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
stmt.setString(1, username);
|
||||||
|
try (ResultSet rs = stmt.executeQuery())
|
||||||
|
{
|
||||||
|
if (rs.next())
|
||||||
|
{
|
||||||
|
User user = new User();
|
||||||
|
user.setId(rs.getInt("id"));
|
||||||
|
user.setUsername(rs.getString("username"));
|
||||||
|
user.setPassword(rs.getString("password"));
|
||||||
|
user.setEmail(rs.getString("email"));
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
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/restaurant"; // 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 = "11"; // Your Password
|
||||||
|
|
||||||
private DatabaseConnection() {
|
private DatabaseConnection() {
|
||||||
|
|
||||||
@@ -17,11 +18,9 @@ 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,43 @@ public class MenuItem {
|
|||||||
|
|
||||||
private String category;
|
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;
|
private double totalPrice;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getTotalPrice() {
|
||||||
|
return totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalPrice(double totalPrice) {
|
||||||
|
this.totalPrice = totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getCreatedAt() {
|
||||||
|
return createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedAt(LocalDateTime createdAt) {
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(int userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,43 @@ public class OrderDetail {
|
|||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getQuantity() {
|
||||||
|
return quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQuantity(int quantity) {
|
||||||
|
this.quantity = quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMenuItemId() {
|
||||||
|
return menuItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMenuItemId(int menuItemId) {
|
||||||
|
this.menuItemId = menuItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderId(int orderId) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,4 +10,35 @@ public class User {
|
|||||||
|
|
||||||
private String email;
|
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,23 +1,106 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.UserDao;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
public class AuthService {
|
public class AuthService {
|
||||||
|
|
||||||
|
private final UserDao userDao;
|
||||||
|
|
||||||
|
public AuthService() {
|
||||||
|
this.userDao = new UserDao();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean register(String username, String password, String email) {
|
public boolean register(String username, String password, String email) {
|
||||||
|
|
||||||
// TODO:
|
if (username == null || username.trim().isEmpty())
|
||||||
// Validate and register user
|
{
|
||||||
|
System.err.println("username cannot be empty.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (password == null || password.trim().isEmpty())
|
||||||
|
{
|
||||||
|
System.err.println("password cannot be empty.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
User existingUser = userDao.findByUsername(username);
|
||||||
|
if (existingUser != null)
|
||||||
|
{
|
||||||
|
System.err.println("a user with this username is already registered");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
String hashedPassword = hashPassword(password);
|
||||||
|
if (hashedPassword == null)
|
||||||
|
{
|
||||||
|
System.err.println("error");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
User newUser = new User();
|
||||||
|
newUser.setUsername(username.trim());
|
||||||
|
newUser.setPassword(hashedPassword);
|
||||||
|
newUser.setEmail(email != null ? email.trim() : null);
|
||||||
|
|
||||||
|
return userDao.save(newUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User login(String username, String password) {
|
public User login(String username, String password) {
|
||||||
|
|
||||||
// TODO:
|
if (username == null || username.trim().isEmpty())
|
||||||
// Authenticate user
|
{
|
||||||
|
System.err.println("username cannot be empty.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (password == null || password.trim().isEmpty())
|
||||||
|
{
|
||||||
|
System.err.println("password cannot be empty.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
User user = userDao.findByUsername(username.trim());
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
System.err.println("not found");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String hashedPassword = hashPassword(password);
|
||||||
|
if (hashedPassword == null)
|
||||||
|
{
|
||||||
|
System.err.println("error");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hashedPassword.equals(user.getPassword()))
|
||||||
|
{
|
||||||
|
System.out.println("welcome " + user.getUsername());
|
||||||
|
return user;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
System.err.println("incorrect password");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private String hashPassword(String password)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
|
byte[] hashBytes = md.digest(password.getBytes());
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (byte b : hashBytes) {
|
||||||
|
sb.append(String.format("%02x", b));
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,35 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.MenuItemDao;
|
||||||
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class MenuService {
|
public class MenuService {
|
||||||
|
|
||||||
|
private final MenuItemDao menuItemDao;
|
||||||
|
|
||||||
|
public MenuService() {
|
||||||
|
this.menuItemDao = new MenuItemDao();
|
||||||
|
}
|
||||||
|
|
||||||
public void showMenu() {
|
public void showMenu() {
|
||||||
|
|
||||||
// TODO:
|
List<MenuItem> items = menuItemDao.findAll();
|
||||||
// Display menu items
|
if (items.isEmpty())
|
||||||
|
{
|
||||||
|
System.out.println("menu is empty");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nAvailable Items:");
|
||||||
|
for (MenuItem item : items)
|
||||||
|
{
|
||||||
|
System.out.printf("%d. %s - $%.2f%n",
|
||||||
|
item.getId(),
|
||||||
|
item.getName(),
|
||||||
|
item.getPrice());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +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.time.format.DateTimeFormatter;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class OrderService {
|
public class OrderService {
|
||||||
|
|
||||||
public void placeOrder(int userId) {
|
private final OrderDao orderDao;
|
||||||
|
private final OrderDetailDao orderDetailDao;
|
||||||
|
private final MenuItemDao menuItemDao;
|
||||||
|
|
||||||
// TODO:
|
public OrderService() {
|
||||||
// Create order
|
this.orderDao = new OrderDao();
|
||||||
|
this.orderDetailDao = new OrderDetailDao();
|
||||||
|
this.menuItemDao = new MenuItemDao();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int placeOrder(int userId, List<OrderDetail> cart) {
|
||||||
|
|
||||||
|
if (cart == null || cart.isEmpty()) {
|
||||||
|
System.out.println("cart is empty");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double totalPrice = 0;
|
||||||
|
for (OrderDetail o : cart) {
|
||||||
|
MenuItem item = menuItemDao.findById((o.getMenuItemId()));
|
||||||
|
if (item == null) {
|
||||||
|
System.out.println("menu item with id " + o.getMenuItemId() + "not found");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
o.setPrice(item.getPrice());
|
||||||
|
totalPrice += item.getPrice() * o.getQuantity();
|
||||||
|
}
|
||||||
|
|
||||||
|
Order order = new Order();
|
||||||
|
order.setUserId(userId);
|
||||||
|
order.setTotalPrice(totalPrice);
|
||||||
|
|
||||||
|
int orderId = orderDao.save(order);
|
||||||
|
if (orderId == -1) {
|
||||||
|
System.out.println("failed to save order.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (OrderDetail detail : cart) {
|
||||||
|
detail.setOrderId(orderId);
|
||||||
|
orderDetailDao.save(detail);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Order saved successfully!");
|
||||||
|
return orderId;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printReceipt(int orderId) {
|
public void printReceipt(int orderId) {
|
||||||
|
|
||||||
// TODO:
|
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||||
// Print order receipt
|
if (details.isEmpty()) {
|
||||||
|
System.out.println("order not found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
double total = 0;
|
||||||
|
|
||||||
|
System.out.println("\n[Order Summary / Receipt]");
|
||||||
|
System.out.println("---------------------------------------");
|
||||||
|
System.out.printf("%-12s %-6s %-8s %-10s%n", "Item", "Qty", "Unit", "Total");
|
||||||
|
System.out.println("---------------------------------------");
|
||||||
|
|
||||||
|
for (OrderDetail detail : details) {
|
||||||
|
MenuItem item = menuItemDao.findById(detail.getMenuItemId());
|
||||||
|
String itemName = (item != null) ? item.getName() : "Unknown";
|
||||||
|
double subtotal = detail.getPrice() * detail.getQuantity();
|
||||||
|
total += subtotal;
|
||||||
|
|
||||||
|
System.out.printf("%-12s %-6d $%-7.2f $%-9.2f%n",
|
||||||
|
itemName,
|
||||||
|
detail.getQuantity(),
|
||||||
|
detail.getPrice(),
|
||||||
|
subtotal);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("---------------------------------------");
|
||||||
|
System.out.printf("%-12s %-6s %-8s $%-9.2f%n", "", "", "Final Total:", total);
|
||||||
|
System.out.println("---------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showOrderHistory(int userId) {
|
public void showOrderHistory(int userId) {
|
||||||
|
|
||||||
// TODO:
|
List<Order> orders = orderDao.findByUserId(userId);
|
||||||
// Display user's order history
|
if (orders.isEmpty())
|
||||||
|
{
|
||||||
|
System.out.println("you have no orders.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nYour Order History:");
|
||||||
|
System.out.println("--------------------------------------------------");
|
||||||
|
System.out.printf("%-10s %-25s %-15s%n", "Order ID", "Date", "Total");
|
||||||
|
System.out.println("--------------------------------------------------");
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
||||||
|
for (Order order : orders) {
|
||||||
|
System.out.printf("%-10d %-25s $%-14.2f%n",
|
||||||
|
order.getId(),
|
||||||
|
order.getCreatedAt().format(formatter),
|
||||||
|
order.getTotalPrice());
|
||||||
|
}
|
||||||
|
System.out.println("--------------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
package dev.ui;
|
package dev.ui;
|
||||||
|
|
||||||
|
import dev.dao.MenuItemDao;
|
||||||
|
import dev.model.MenuItem;
|
||||||
|
import dev.model.OrderDetail;
|
||||||
|
import dev.model.User;
|
||||||
|
import dev.service.AuthService;
|
||||||
|
import dev.service.MenuService;
|
||||||
|
import dev.service.OrderService;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ConsoleMenu {
|
public class ConsoleMenu {
|
||||||
@@ -7,38 +17,164 @@ public class ConsoleMenu {
|
|||||||
private final Scanner scanner =
|
private final Scanner scanner =
|
||||||
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();
|
||||||
|
|
||||||
|
private User loggedInUser = null;
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (loggedInUser == null) {
|
||||||
System.out.println();
|
showMainMenu();
|
||||||
System.out.println("===== JAVA PIZZERIA =====");
|
} else {
|
||||||
System.out.println("1. Login");
|
showUserMenu();
|
||||||
System.out.println("2. Register");
|
|
||||||
System.out.println("3. Exit");
|
|
||||||
|
|
||||||
int choice = scanner.nextInt();
|
|
||||||
|
|
||||||
switch (choice) {
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
// TODO
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
// TODO
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 3:
|
|
||||||
return;
|
|
||||||
|
|
||||||
default:
|
|
||||||
System.out.println("Invalid choice");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void showMainMenu()
|
||||||
|
{
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("=======================================");
|
||||||
|
System.out.println(" 🍕 WELCOME TO JAVA PIZZERIA 🍕");
|
||||||
|
System.out.println("=======================================");
|
||||||
|
System.out.println("1. Login");
|
||||||
|
System.out.println("2. Register New Account");
|
||||||
|
System.out.println("3. Exit");
|
||||||
|
System.out.println("=======================================");
|
||||||
|
System.out.print("Choose an option: ");
|
||||||
|
|
||||||
|
int choice = getIntInput();
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1 -> login();
|
||||||
|
case 2 -> register();
|
||||||
|
case 3 -> {
|
||||||
|
System.out.println("goodbye");
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
default -> System.out.println("invalid choice.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showUserMenu()
|
||||||
|
{
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("=======================================");
|
||||||
|
System.out.println(" 🍽️ MAIN MENU 🍽️");
|
||||||
|
System.out.println("=======================================");
|
||||||
|
System.out.println("1. View Menu");
|
||||||
|
System.out.println("2. Place a New Order");
|
||||||
|
System.out.println("3. View Order History");
|
||||||
|
System.out.println("4. Logout");
|
||||||
|
System.out.println("=======================================");
|
||||||
|
System.out.print("Choose an option: ");
|
||||||
|
|
||||||
|
int choice = getIntInput();
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1 -> menuService.showMenu();
|
||||||
|
case 2 -> placeOrder();
|
||||||
|
case 3 -> orderService.showOrderHistory(loggedInUser.getId());
|
||||||
|
case 4 -> {
|
||||||
|
loggedInUser = null;
|
||||||
|
System.out.println("logged out.");
|
||||||
|
}
|
||||||
|
default -> System.out.println("invalid choice.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void login()
|
||||||
|
{
|
||||||
|
System.out.println("\n[Login]");
|
||||||
|
System.out.print("Enter username: ");
|
||||||
|
String username = scanner.nextLine().trim();
|
||||||
|
System.out.print("Enter password: ");
|
||||||
|
String password = scanner.nextLine().trim();
|
||||||
|
|
||||||
|
User user = authService.login(username, password);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
loggedInUser = user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void register()
|
||||||
|
{
|
||||||
|
System.out.println("\n[Register]");
|
||||||
|
System.out.print("Enter username: ");
|
||||||
|
String username = scanner.nextLine().trim();
|
||||||
|
System.out.print("Enter password: ");
|
||||||
|
String password = scanner.nextLine().trim();
|
||||||
|
System.out.print("Enter email (optional, press Enter to skip): ");
|
||||||
|
String email = scanner.nextLine().trim();
|
||||||
|
if (email.isEmpty()) email = null;
|
||||||
|
|
||||||
|
boolean success = authService.register(username, password, email);
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
System.out.println("registration successful. Please login.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void placeOrder()
|
||||||
|
{
|
||||||
|
System.out.println("\n[Placing Order]");
|
||||||
|
menuService.showMenu();
|
||||||
|
|
||||||
|
List<OrderDetail> cart = new ArrayList<>();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
System.out.print("\nEnter the ID of the item to add (or 0 to finish): ");
|
||||||
|
int itemId = getIntInput();
|
||||||
|
if (itemId == 0) break;
|
||||||
|
|
||||||
|
MenuItem item = new MenuItemDao().findById(itemId);
|
||||||
|
if (item == null)
|
||||||
|
{
|
||||||
|
System.out.println("item not found.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("Enter quantity: ");
|
||||||
|
int quantity = getIntInput();
|
||||||
|
if (quantity <= 0)
|
||||||
|
{
|
||||||
|
System.out.println("quantity must be greater than 0.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
OrderDetail detail = new OrderDetail();
|
||||||
|
detail.setMenuItemId(itemId);
|
||||||
|
detail.setQuantity(quantity);
|
||||||
|
cart.add(detail);
|
||||||
|
|
||||||
|
System.out.printf("Added %dx %s to your cart.%n", quantity, item.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cart.isEmpty())
|
||||||
|
{
|
||||||
|
System.out.println("cart is empty. Order cancelled.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int orderId = orderService.placeOrder(loggedInUser.getId(), cart);
|
||||||
|
if (orderId != -1)
|
||||||
|
{
|
||||||
|
orderService.printReceipt(orderId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getIntInput() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(scanner.nextLine().trim());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.out.print("please enter a number: ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user