forked from AdvancedProgramming1404/WS-10-Database
database
This commit is contained in:
Generated
+10
@@ -0,0 +1,10 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Ignored default folder with query files
|
||||||
|
/queries/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
Generated
+13
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="WS-10-Database" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+20
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="RemoteRepositoriesConfiguration">
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="https://maven.devneeds.ir/" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Maven Central repository" />
|
||||||
|
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="jboss.community" />
|
||||||
|
<option name="name" value="JBoss Community repository" />
|
||||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+12
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="26" 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>
|
||||||
+102
-120
@@ -1,141 +1,123 @@
|
|||||||
-- Restaurant Database Management System
|
|
||||||
--
|
|
||||||
-- Instructions:
|
DROP TABLE IF EXISTS order_details;
|
||||||
-- 1. Create all required tables.
|
DROP TABLE IF EXISTS orders;
|
||||||
-- 2. Design appropriate PRIMARY KEY and FOREIGN KEY relationships.
|
DROP TABLE IF EXISTS menu_items;
|
||||||
-- 3. Add suitable constraints based on the requirements.
|
DROP TABLE IF EXISTS users;
|
||||||
-- 4. Insert initial mock data.
|
|
||||||
-- 5. Insert at least 3 menu items.
|
|
||||||
-- 6. The script should be executable from start to finish without errors.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
|
||||||
-- USER TABLE
|
CREATE TABLE users (
|
||||||
-- =======================================================
|
|
||||||
--
|
id SERIAL PRIMARY KEY,
|
||||||
-- Represents customers using the system.
|
|
||||||
--
|
username VARCHAR(50) UNIQUE NOT NULL,
|
||||||
-- Required information:
|
|
||||||
-- - Unique identifier
|
password VARCHAR(255) NOT NULL,
|
||||||
-- - Username
|
|
||||||
-- - Password
|
email VARCHAR(100)
|
||||||
-- - 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 ...
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
|
||||||
-- MENU ITEM TABLE
|
|
||||||
-- =======================================================
|
CREATE TABLE menu_items (
|
||||||
--
|
|
||||||
-- Represents available food and drink items.
|
id SERIAL PRIMARY KEY,
|
||||||
--
|
|
||||||
-- Required information:
|
name VARCHAR(100) NOT NULL,
|
||||||
-- - Unique identifier
|
|
||||||
-- - Name
|
description TEXT,
|
||||||
-- - Description (optional)
|
|
||||||
-- - Price
|
price NUMERIC(10,2) NOT NULL CHECK(price > 0),
|
||||||
-- - Category (optional)
|
|
||||||
--
|
category VARCHAR(50)
|
||||||
-- Requirements:
|
|
||||||
-- - Each menu item must have a unique identifier.
|
);
|
||||||
-- - Name is required.
|
|
||||||
-- - Price must always be positive.
|
|
||||||
--
|
|
||||||
-- CREATE TABLE ...
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
|
||||||
-- ORDER TABLE
|
|
||||||
-- =======================================================
|
CREATE TABLE orders (
|
||||||
--
|
|
||||||
-- Represents orders placed by customers.
|
id SERIAL PRIMARY KEY,
|
||||||
--
|
|
||||||
-- Required information:
|
user_id INTEGER NOT NULL,
|
||||||
-- - Unique identifier
|
|
||||||
-- - Reference to customer
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
-- - Creation date and time
|
|
||||||
-- - Total price
|
total_price NUMERIC(10,2) NOT NULL,
|
||||||
--
|
|
||||||
-- Requirements:
|
|
||||||
-- - Each order must belong to exactly one user.
|
CONSTRAINT fk_order_user
|
||||||
-- - A user can have multiple orders.
|
|
||||||
-- - The relationship between User and Order must be implemented.
|
FOREIGN KEY(user_id)
|
||||||
--
|
|
||||||
-- Note:
|
REFERENCES users(id)
|
||||||
-- Avoid using reserved SQL keywords as table names.
|
|
||||||
-- Consider using a name such as "orders" or "customer_orders".
|
ON DELETE CASCADE
|
||||||
--
|
|
||||||
-- CREATE TABLE ...
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
|
||||||
-- ORDER DETAIL TABLE
|
|
||||||
-- =======================================================
|
CREATE TABLE order_details (
|
||||||
--
|
|
||||||
-- Represents items inside an order.
|
id SERIAL PRIMARY KEY,
|
||||||
--
|
|
||||||
-- Required information:
|
|
||||||
-- - Unique identifier
|
order_id INTEGER NOT NULL,
|
||||||
-- - Reference to an order
|
|
||||||
-- - Reference to a menu item
|
|
||||||
-- - Quantity
|
menu_item_id INTEGER NOT NULL,
|
||||||
-- - Item price at purchase time
|
|
||||||
--
|
|
||||||
-- Requirements:
|
quantity INTEGER NOT NULL CHECK(quantity > 0),
|
||||||
-- - Each detail record must belong to one order.
|
|
||||||
-- - Each detail record must reference one menu item.
|
|
||||||
-- - Quantity must always be greater than zero.
|
price NUMERIC(10,2) NOT NULL CHECK(price > 0),
|
||||||
-- - Store the item's price at the moment of purchase.
|
|
||||||
--
|
|
||||||
-- CREATE TABLE ...
|
CONSTRAINT fk_detail_order
|
||||||
|
|
||||||
|
FOREIGN KEY(order_id)
|
||||||
|
|
||||||
|
REFERENCES orders(id)
|
||||||
|
|
||||||
|
ON DELETE CASCADE,
|
||||||
|
|
||||||
|
|
||||||
|
CONSTRAINT fk_detail_menu
|
||||||
|
|
||||||
|
FOREIGN KEY(menu_item_id)
|
||||||
|
|
||||||
|
REFERENCES menu_items(id)
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
|
||||||
-- INITIAL MENU DATA
|
|
||||||
-- =======================================================
|
|
||||||
--
|
|
||||||
-- Insert at least 3 food or drink items.
|
|
||||||
--
|
|
||||||
-- Example categories:
|
|
||||||
-- - Pizza
|
|
||||||
-- - Burger
|
|
||||||
-- - Pasta
|
|
||||||
-- - Drink
|
|
||||||
--
|
|
||||||
-- INSERT INTO ...
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
INSERT INTO menu_items
|
||||||
-- OPTIONAL TEST DATA
|
(name, description, price, category)
|
||||||
-- =======================================================
|
|
||||||
--
|
VALUES
|
||||||
-- You may insert sample users and orders for testing.
|
|
||||||
-- This section is optional.
|
('Pizza','Cheese pizza',10.00,'Pizza'),
|
||||||
--
|
|
||||||
-- INSERT INTO ...
|
('Burger','Beef burger',8.00,'Burger'),
|
||||||
|
|
||||||
|
('Pasta','Italian pasta',12.00,'Pasta'),
|
||||||
|
|
||||||
|
('Cola','Cold drink',2.50,'Drink');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
|
||||||
-- VERIFICATION QUERIES
|
|
||||||
-- =======================================================
|
|
||||||
--
|
|
||||||
-- Uncomment these queries to verify your database.
|
|
||||||
--
|
|
||||||
-- SELECT * FROM ...;
|
|
||||||
-- SELECT * FROM ...;
|
|
||||||
-- SELECT * FROM ...;
|
|
||||||
-- SELECT * FROM ...;
|
|
||||||
@@ -1,25 +1,133 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.MenuItem;
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class MenuItemDao {
|
public class MenuItemDao {
|
||||||
|
|
||||||
public List<MenuItem> findAll() {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Retrieve all menu items
|
|
||||||
|
|
||||||
return null;
|
public List<MenuItem> findAll(){
|
||||||
|
|
||||||
|
|
||||||
|
List<MenuItem> list=new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
|
String sql="SELECT * FROM menu_items";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try(Connection con =
|
||||||
|
DatabaseConnection.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement ps =
|
||||||
|
con.prepareStatement(sql)){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ResultSet rs=ps.executeQuery();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(rs.next()){
|
||||||
|
|
||||||
|
|
||||||
|
list.add(new MenuItem(
|
||||||
|
|
||||||
|
rs.getInt("id"),
|
||||||
|
|
||||||
|
rs.getString("name"),
|
||||||
|
|
||||||
|
rs.getString("description"),
|
||||||
|
|
||||||
|
rs.getDouble("price"),
|
||||||
|
|
||||||
|
rs.getString("category")
|
||||||
|
|
||||||
|
));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}catch(SQLException e){
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return list;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Find menu item by id
|
|
||||||
|
|
||||||
|
|
||||||
|
public MenuItem findById(int id){
|
||||||
|
|
||||||
|
|
||||||
|
String sql =
|
||||||
|
"SELECT * FROM menu_items WHERE id=?";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try(Connection con =
|
||||||
|
DatabaseConnection.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement ps =
|
||||||
|
con.prepareStatement(sql)){
|
||||||
|
|
||||||
|
|
||||||
|
ps.setInt(1,id);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ResultSet rs=ps.executeQuery();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(rs.next()){
|
||||||
|
|
||||||
|
|
||||||
|
return new MenuItem(
|
||||||
|
|
||||||
|
rs.getInt("id"),
|
||||||
|
|
||||||
|
rs.getString("name"),
|
||||||
|
|
||||||
|
rs.getString("description"),
|
||||||
|
|
||||||
|
rs.getDouble("price"),
|
||||||
|
|
||||||
|
rs.getString("category")
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}catch(SQLException e){
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,25 +1,130 @@
|
|||||||
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:
|
|
||||||
// Insert order and return generated id
|
String sql =
|
||||||
|
"INSERT INTO orders(user_id,total_price) VALUES(?,?)";
|
||||||
|
|
||||||
|
|
||||||
|
try(Connection con = DatabaseConnection.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement ps =
|
||||||
|
con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)){
|
||||||
|
|
||||||
|
|
||||||
|
ps.setInt(1, order.getUserId());
|
||||||
|
|
||||||
|
ps.setDouble(2, order.getTotalPrice());
|
||||||
|
|
||||||
|
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ResultSet rs = ps.getGeneratedKeys();
|
||||||
|
|
||||||
|
|
||||||
|
if(rs.next()){
|
||||||
|
|
||||||
|
return rs.getInt(1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}catch(SQLException e){
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Order> findByUserId(int userId) {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Retrieve all orders of a user
|
|
||||||
|
|
||||||
return null;
|
|
||||||
|
public List<Order> findByUserId(int userId){
|
||||||
|
|
||||||
|
|
||||||
|
List<Order> orders = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
|
String sql =
|
||||||
|
"SELECT * FROM orders WHERE user_id=?";
|
||||||
|
|
||||||
|
|
||||||
|
try(Connection con = DatabaseConnection.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement ps =
|
||||||
|
con.prepareStatement(sql)){
|
||||||
|
|
||||||
|
|
||||||
|
ps.setInt(1,userId);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ResultSet rs = ps.executeQuery();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(rs.next()){
|
||||||
|
|
||||||
|
|
||||||
|
Order order = new Order();
|
||||||
|
|
||||||
|
|
||||||
|
order.setId(rs.getInt("id"));
|
||||||
|
|
||||||
|
order.setUserId(rs.getInt("user_id"));
|
||||||
|
|
||||||
|
order.setTotalPrice(
|
||||||
|
rs.getDouble("total_price")
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
Timestamp timestamp =
|
||||||
|
rs.getTimestamp("created_at");
|
||||||
|
|
||||||
|
|
||||||
|
if(timestamp != null){
|
||||||
|
|
||||||
|
order.setCreatedAt(
|
||||||
|
timestamp.toLocalDateTime()
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
orders.add(order);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}catch(SQLException e){
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return orders;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,144 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.OrderDetail;
|
import dev.model.OrderDetail;
|
||||||
|
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class OrderDetailDao {
|
public class OrderDetailDao {
|
||||||
|
|
||||||
public void save(OrderDetail detail) {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Insert order detail
|
public void save(OrderDetail detail){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
String sql =
|
||||||
|
"""
|
||||||
|
INSERT INTO order_details
|
||||||
|
(order_id,menu_item_id,quantity,price)
|
||||||
|
VALUES(?,?,?,?)
|
||||||
|
""";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try(Connection con =
|
||||||
|
DatabaseConnection.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement ps =
|
||||||
|
con.prepareStatement(sql)){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ps.setInt(1,detail.getOrderId());
|
||||||
|
|
||||||
|
ps.setInt(2,detail.getMenuItemId());
|
||||||
|
|
||||||
|
ps.setInt(3,detail.getQuantity());
|
||||||
|
|
||||||
|
ps.setDouble(4,detail.getPrice());
|
||||||
|
|
||||||
|
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}catch(SQLException e){
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<OrderDetail> findByOrderId(int orderId) {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Retrieve order details
|
|
||||||
|
|
||||||
return null;
|
|
||||||
|
|
||||||
|
public List<OrderDetail> findByOrderId(int orderId){
|
||||||
|
|
||||||
|
|
||||||
|
List<OrderDetail> list =
|
||||||
|
new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
|
String sql =
|
||||||
|
"SELECT * FROM order_details WHERE order_id=?";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try(Connection con =
|
||||||
|
DatabaseConnection.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement ps =
|
||||||
|
con.prepareStatement(sql)){
|
||||||
|
|
||||||
|
|
||||||
|
ps.setInt(1,orderId);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ResultSet rs =
|
||||||
|
ps.executeQuery();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(rs.next()){
|
||||||
|
|
||||||
|
|
||||||
|
OrderDetail detail =
|
||||||
|
new OrderDetail();
|
||||||
|
|
||||||
|
|
||||||
|
detail.setId(
|
||||||
|
rs.getInt("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
detail.setOrderId(
|
||||||
|
rs.getInt("order_id")
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
detail.setMenuItemId(
|
||||||
|
rs.getInt("menu_item_id")
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
detail.setQuantity(
|
||||||
|
rs.getInt("quantity")
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
detail.setPrice(
|
||||||
|
rs.getDouble("price")
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
list.add(detail);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}catch(SQLException e){
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return list;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,109 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class UserDao {
|
public class UserDao {
|
||||||
|
|
||||||
public boolean save(User user) {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Insert user into database
|
public boolean save(User user){
|
||||||
|
|
||||||
|
|
||||||
|
String sql =
|
||||||
|
"INSERT INTO users(username,password,email) VALUES(?,?,?)";
|
||||||
|
|
||||||
|
|
||||||
|
try(Connection con =
|
||||||
|
DatabaseConnection.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement ps =
|
||||||
|
con.prepareStatement(sql)){
|
||||||
|
|
||||||
|
|
||||||
|
ps.setString(1,user.getUsername());
|
||||||
|
|
||||||
|
ps.setString(2,user.getPassword());
|
||||||
|
|
||||||
|
ps.setString(3,user.getEmail());
|
||||||
|
|
||||||
|
|
||||||
|
ps.executeUpdate();
|
||||||
|
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
|
||||||
|
}catch(SQLException e){
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUsername(String username) {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Find a user by username
|
|
||||||
|
|
||||||
|
public User findByUsername(String username){
|
||||||
|
|
||||||
|
|
||||||
|
String sql =
|
||||||
|
"SELECT * FROM users WHERE username=?";
|
||||||
|
|
||||||
|
|
||||||
|
try(Connection con =
|
||||||
|
DatabaseConnection.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement ps =
|
||||||
|
con.prepareStatement(sql)){
|
||||||
|
|
||||||
|
|
||||||
|
ps.setString(1,username);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ResultSet rs=ps.executeQuery();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(rs.next()){
|
||||||
|
|
||||||
|
|
||||||
|
return new User(
|
||||||
|
|
||||||
|
rs.getInt("id"),
|
||||||
|
|
||||||
|
rs.getString("username"),
|
||||||
|
|
||||||
|
rs.getString("password"),
|
||||||
|
|
||||||
|
rs.getString("email")
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}catch(SQLException e){
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,43 @@
|
|||||||
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 USER = "postgres"; // Your Username
|
private static final String URL =
|
||||||
|
"jdbc:postgresql://localhost:5432/restaurant_db";
|
||||||
|
|
||||||
private static final String PASSWORD = "password"; // Your Password
|
|
||||||
|
|
||||||
private DatabaseConnection() {
|
private static final String USER =
|
||||||
|
"postgres";
|
||||||
|
|
||||||
|
|
||||||
|
private static final String PASSWORD =
|
||||||
|
"neda3113";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private DatabaseConnection(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static Connection getConnection()
|
public static Connection getConnection()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Return a valid PostgreSQL connection
|
|
||||||
|
|
||||||
return null;
|
return DriverManager.getConnection(
|
||||||
|
URL,
|
||||||
|
USER,
|
||||||
|
PASSWORD
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
package dev.model;
|
package dev.model;
|
||||||
|
|
||||||
|
|
||||||
public class MenuItem {
|
public class MenuItem {
|
||||||
|
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
@@ -12,4 +14,74 @@ public class MenuItem {
|
|||||||
|
|
||||||
private String category;
|
private String category;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public MenuItem(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public MenuItem(int id,String name,String description,
|
||||||
|
double price,String category){
|
||||||
|
|
||||||
|
this.id=id;
|
||||||
|
this.name=name;
|
||||||
|
this.description=description;
|
||||||
|
this.price=price;
|
||||||
|
this.category=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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
package dev.model;
|
package dev.model;
|
||||||
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
|
||||||
public class Order {
|
public class Order {
|
||||||
|
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private int userId;
|
private int userId;
|
||||||
@@ -12,4 +15,61 @@ public class Order {
|
|||||||
|
|
||||||
private double totalPrice;
|
private double totalPrice;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Order(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Order(int userId,double totalPrice){
|
||||||
|
|
||||||
|
this.userId=userId;
|
||||||
|
this.totalPrice=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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
package dev.model;
|
package dev.model;
|
||||||
|
|
||||||
|
|
||||||
public class OrderDetail {
|
public class OrderDetail {
|
||||||
|
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private int orderId;
|
private int orderId;
|
||||||
@@ -12,4 +14,75 @@ public class OrderDetail {
|
|||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public OrderDetail(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public OrderDetail(int orderId,
|
||||||
|
int menuItemId,
|
||||||
|
int quantity,
|
||||||
|
double price){
|
||||||
|
|
||||||
|
this.orderId=orderId;
|
||||||
|
this.menuItemId=menuItemId;
|
||||||
|
this.quantity=quantity;
|
||||||
|
this.price=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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -3,11 +3,67 @@ package dev.model;
|
|||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public User(String username, String password, String email) {
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public User(int id, String username, String password, String email) {
|
||||||
|
this.id = id;
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.email = 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,136 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
|
||||||
|
import dev.dao.UserDao;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
|
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public class AuthService {
|
public class AuthService {
|
||||||
|
|
||||||
public boolean register(String username, String password, String email) {
|
|
||||||
|
|
||||||
// TODO:
|
private final UserDao userDao =
|
||||||
// Validate and register user
|
new UserDao();
|
||||||
|
|
||||||
return false;
|
|
||||||
|
|
||||||
|
|
||||||
|
public boolean register(String username,
|
||||||
|
String password,
|
||||||
|
String email){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
User existingUser = userDao.findByUsername(username);
|
||||||
|
|
||||||
|
if (existingUser != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
String hashedPassword = hashPassword(password);
|
||||||
|
|
||||||
|
|
||||||
|
User user = new User(username, hashedPassword, email);
|
||||||
|
|
||||||
|
|
||||||
|
return userDao.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User login(String username, String password) {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Authenticate user
|
|
||||||
|
|
||||||
|
|
||||||
|
public User login(String username,
|
||||||
|
String password){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
User user =
|
||||||
|
userDao.findByUsername(username);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(user==null){
|
||||||
|
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
String hashed =
|
||||||
|
hashPassword(password);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(user.getPassword().equals(hashed)){
|
||||||
|
|
||||||
|
|
||||||
|
return user;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private String hashPassword(String password){
|
||||||
|
|
||||||
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
|
||||||
|
MessageDigest md =
|
||||||
|
MessageDigest.getInstance("SHA-256");
|
||||||
|
|
||||||
|
|
||||||
|
byte[] bytes =
|
||||||
|
md.digest(
|
||||||
|
password.getBytes()
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
StringBuilder sb =
|
||||||
|
new StringBuilder();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for(byte b:bytes){
|
||||||
|
|
||||||
|
|
||||||
|
sb.append(
|
||||||
|
String.format("%02x",b)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}catch(Exception e){
|
||||||
|
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,54 @@
|
|||||||
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() {
|
|
||||||
|
|
||||||
// TODO:
|
private final MenuItemDao dao =
|
||||||
// Display menu items
|
new MenuItemDao();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void showMenu(){
|
||||||
|
|
||||||
|
|
||||||
|
List<MenuItem> items =
|
||||||
|
dao.findAll();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"========== MENU =========="
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for(MenuItem item:items){
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
|
||||||
|
item.getId()
|
||||||
|
+" - "
|
||||||
|
+item.getName()
|
||||||
|
+" $"
|
||||||
|
+item.getPrice()
|
||||||
|
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,26 +1,300 @@
|
|||||||
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 {
|
||||||
|
|
||||||
public void placeOrder(int userId) {
|
|
||||||
|
|
||||||
// TODO:
|
private final MenuItemDao menuDao =
|
||||||
// Create order
|
new MenuItemDao();
|
||||||
|
|
||||||
|
|
||||||
|
private final OrderDao orderDao =
|
||||||
|
new OrderDao();
|
||||||
|
|
||||||
|
|
||||||
|
private final OrderDetailDao detailDao =
|
||||||
|
new OrderDetailDao();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public int placeOrder(int userId){
|
||||||
|
|
||||||
|
|
||||||
|
Scanner scanner =
|
||||||
|
new Scanner(System.in);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
List<OrderDetail> cart =
|
||||||
|
new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
|
double total = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(true){
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Enter menu item id (0 finish): "
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
int itemId =
|
||||||
|
scanner.nextInt();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(itemId == 0){
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
MenuItem item =
|
||||||
|
menuDao.findById(itemId);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(item == null){
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Item not found!"
|
||||||
|
);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Enter quantity:"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
int quantity =
|
||||||
|
scanner.nextInt();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
OrderDetail detail =
|
||||||
|
new OrderDetail(
|
||||||
|
0,
|
||||||
|
item.getId(),
|
||||||
|
quantity,
|
||||||
|
item.getPrice()
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
cart.add(detail);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
total +=
|
||||||
|
item.getPrice()
|
||||||
|
*
|
||||||
|
quantity;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Added to cart."
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(cart.isEmpty()){
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Cart is empty"
|
||||||
|
);
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Save Order first
|
||||||
|
|
||||||
|
Order order =
|
||||||
|
new Order(
|
||||||
|
userId,
|
||||||
|
total
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int orderId =
|
||||||
|
orderDao.save(order);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for(OrderDetail detail:cart){
|
||||||
|
|
||||||
|
|
||||||
|
detail.setOrderId(orderId);
|
||||||
|
|
||||||
|
|
||||||
|
detailDao.save(detail);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Order created successfully!"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
printReceipt(orderId);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return orderId;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printReceipt(int orderId) {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Print order receipt
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void printReceipt(int orderId){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"\n========== RECEIPT =========="
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
List<OrderDetail> details =
|
||||||
|
detailDao.findByOrderId(orderId);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
double total = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for(OrderDetail d:details){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
MenuItem item =
|
||||||
|
menuDao.findById(
|
||||||
|
d.getMenuItemId()
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
double subtotal =
|
||||||
|
d.getQuantity()
|
||||||
|
*
|
||||||
|
d.getPrice();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
total += subtotal;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
item.getName()
|
||||||
|
+" | Qty: "
|
||||||
|
+d.getQuantity()
|
||||||
|
+" | Unit: $"
|
||||||
|
+d.getPrice()
|
||||||
|
+" | Total: $"
|
||||||
|
+subtotal
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"----------------------------"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Grand Total: $"
|
||||||
|
+total
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"============================"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showOrderHistory(int userId) {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Display user's order history
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void showOrderHistory(int userId){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"\n======= ORDER HISTORY ======="
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for(Order order:
|
||||||
|
orderDao.findByUserId(userId)){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Order ID: "
|
||||||
|
+order.getId()
|
||||||
|
+
|
||||||
|
" Total: $"+
|
||||||
|
order.getTotalPrice()
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,44 +1,302 @@
|
|||||||
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 scanner =
|
||||||
new Scanner(System.in);
|
new Scanner(System.in);
|
||||||
|
|
||||||
public void start() {
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
|
|
||||||
System.out.println();
|
private final AuthService authService =
|
||||||
System.out.println("===== JAVA PIZZERIA =====");
|
new AuthService();
|
||||||
System.out.println("1. Login");
|
|
||||||
System.out.println("2. Register");
|
|
||||||
System.out.println("3. Exit");
|
|
||||||
|
|
||||||
int choice = scanner.nextInt();
|
|
||||||
|
|
||||||
switch (choice) {
|
private final MenuService menuService =
|
||||||
|
new MenuService();
|
||||||
|
|
||||||
|
|
||||||
|
private final OrderService orderService =
|
||||||
|
new OrderService();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private User currentUser;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void start(){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(true){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"""
|
||||||
|
=======================================
|
||||||
|
🍕 WELCOME TO JAVA PIZZERIA 🍕
|
||||||
|
=======================================
|
||||||
|
|
||||||
|
1. Login
|
||||||
|
2. Register New Account
|
||||||
|
3. Exit
|
||||||
|
|
||||||
|
Choose an option:
|
||||||
|
"""
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int choice =
|
||||||
|
scanner.nextInt();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
scanner.nextLine();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
switch(choice){
|
||||||
|
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
// TODO
|
|
||||||
|
login();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
// TODO
|
|
||||||
|
register();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Goodbye!"
|
||||||
|
);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
System.out.println("Invalid choice");
|
|
||||||
|
System.out.println(
|
||||||
|
"Invalid option"
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void register() {
|
||||||
|
|
||||||
|
System.out.println("Username:");
|
||||||
|
String username = scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Password:");
|
||||||
|
String password = scanner.nextLine();
|
||||||
|
|
||||||
|
System.out.println("Email:");
|
||||||
|
String email = scanner.nextLine();
|
||||||
|
|
||||||
|
boolean success = authService.register(username, password, email);
|
||||||
|
|
||||||
|
if (success) {
|
||||||
|
System.out.println("Registration successful!");
|
||||||
|
} else {
|
||||||
|
System.out.println("Username already exists!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void login(){
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Username:"
|
||||||
|
);
|
||||||
|
|
||||||
|
String username =
|
||||||
|
scanner.nextLine();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Password:"
|
||||||
|
);
|
||||||
|
|
||||||
|
String password =
|
||||||
|
scanner.nextLine();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
currentUser =
|
||||||
|
authService.login(
|
||||||
|
username,
|
||||||
|
password
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(currentUser != null){
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Login successful"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
mainMenu();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
else{
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Wrong username or password"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void mainMenu(){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(currentUser != null){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"""
|
||||||
|
=======================================
|
||||||
|
🍽️ MAIN MENU 🍽️
|
||||||
|
=======================================
|
||||||
|
1. View Menu
|
||||||
|
2. Place Order
|
||||||
|
3. Order History
|
||||||
|
4. Logout
|
||||||
|
=========================
|
||||||
|
"""
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int choice =
|
||||||
|
scanner.nextInt();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
switch(choice){
|
||||||
|
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
|
||||||
|
menuService.showMenu();
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
|
||||||
|
orderService.placeOrder(
|
||||||
|
currentUser.getId()
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
|
||||||
|
orderService.showOrderHistory(
|
||||||
|
currentUser.getId()
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
|
||||||
|
currentUser=null;
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Logged out"
|
||||||
|
);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Invalid option"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user