Develop #2
+47
-15
@@ -7,9 +7,10 @@
|
|||||||
-- 4. Insert initial mock data.
|
-- 4. Insert initial mock data.
|
||||||
-- 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;
|
||||||
|
DROP TABLE IF EXISTS orders;
|
||||||
|
DROP TABLE IF EXISTS menu_items;
|
||||||
|
DROP TABLE IF EXISTS users;
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- USER TABLE
|
-- USER TABLE
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
@@ -29,9 +30,12 @@
|
|||||||
-- - Passwords should not be stored in plain text.
|
-- - Passwords should not be stored in plain text.
|
||||||
--
|
--
|
||||||
-- CREATE TABLE ...
|
-- CREATE TABLE ...
|
||||||
|
CREATE TABLE users (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
username VARCHAR(50) UNIQUE NOT NULL,
|
||||||
|
password VARCHAR(255) NOT NULL,
|
||||||
|
email VARCHAR(100)
|
||||||
|
);
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- MENU ITEM TABLE
|
-- MENU ITEM TABLE
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
@@ -51,9 +55,13 @@
|
|||||||
-- - Price must always be positive.
|
-- - Price must always be positive.
|
||||||
--
|
--
|
||||||
-- CREATE TABLE ...
|
-- CREATE TABLE ...
|
||||||
|
CREATE TABLE menu_items (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(100) NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
price DECIMAL(10, 2) NOT NULL CHECK (price > 0),
|
||||||
|
category VARCHAR(50)
|
||||||
|
);
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- ORDER TABLE
|
-- ORDER TABLE
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
@@ -76,9 +84,17 @@
|
|||||||
-- Consider using a name such as "orders" or "customer_orders".
|
-- Consider using a name such as "orders" or "customer_orders".
|
||||||
--
|
--
|
||||||
-- CREATE TABLE ...
|
-- CREATE TABLE ...
|
||||||
|
CREATE TABLE orders (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
user_id INTEGER NOT NULL,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
total_price DECIMAL(10, 2) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT fk_user
|
||||||
|
FOREIGN KEY (user_id)
|
||||||
|
REFERENCES users(id)
|
||||||
|
ON DELETE CASCADE
|
||||||
|
);
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- ORDER DETAIL TABLE
|
-- ORDER DETAIL TABLE
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
@@ -99,9 +115,22 @@
|
|||||||
-- - Store the item's price at the moment of purchase.
|
-- - Store the item's price at the moment of purchase.
|
||||||
--
|
--
|
||||||
-- CREATE TABLE ...
|
-- CREATE TABLE ...
|
||||||
|
CREATE TABLE order_details (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
order_id INTEGER NOT NULL,
|
||||||
|
menu_item_id INTEGER NOT NULL,
|
||||||
|
quantity INTEGER NOT NULL CHECK (quantity > 0),
|
||||||
|
unit_price DECIMAL(10, 2) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT fk_order
|
||||||
|
FOREIGN KEY (order_id)
|
||||||
|
REFERENCES orders(id)
|
||||||
|
ON DELETE CASCADE,
|
||||||
|
|
||||||
|
CONSTRAINT fk_menu_item
|
||||||
|
FOREIGN KEY (menu_item_id)
|
||||||
|
REFERENCES menu_items(id)
|
||||||
|
);
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- INITIAL MENU DATA
|
-- INITIAL MENU DATA
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
@@ -115,9 +144,12 @@
|
|||||||
-- - Drink
|
-- - Drink
|
||||||
--
|
--
|
||||||
-- INSERT INTO ...
|
-- INSERT INTO ...
|
||||||
|
INSERT INTO menu_items(name, description, price, category) VALUES ('Margherita Pizza', 'Classic pizza with tomato and mozzarella', 12.99, 'Pizza'),
|
||||||
|
('Pepperoni Pizza', 'Pizza with pepperoni and cheese', 14.99, 'Pizza'),
|
||||||
|
('Cheeseburger', 'Beef burger with cheddar cheese', 10.99, 'Burger'),
|
||||||
|
('Spaghetti Carbonara', 'Pasta with eggs, cheese, and pancetta', 13.99, 'Pasta'),
|
||||||
|
('Coca-Cola', 'Classic carbonated soft drink', 3.99, 'Drink'),
|
||||||
|
('Sparkling Water', 'Natural sparkling mineral water', 2.99, 'Drink');
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- OPTIONAL TEST DATA
|
-- OPTIONAL TEST DATA
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
|
|||||||
@@ -1,12 +1,29 @@
|
|||||||
package dev;
|
package dev;
|
||||||
|
|
||||||
|
import dev.dao.MenuItemDao;
|
||||||
|
import dev.dao.OrderDao;
|
||||||
|
import dev.dao.OrderDetailDao;
|
||||||
|
import dev.dao.UserDao;
|
||||||
|
import dev.service.AuthService;
|
||||||
|
import dev.service.MenuService;
|
||||||
|
import dev.service.OrderService;
|
||||||
import dev.ui.ConsoleMenu;
|
import dev.ui.ConsoleMenu;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
ConsoleMenu menu = new ConsoleMenu();
|
UserDao userDao = new UserDao();
|
||||||
|
MenuItemDao menuItemDao = new MenuItemDao();
|
||||||
|
OrderDao orderDao = new OrderDao();
|
||||||
|
OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||||
|
|
||||||
|
AuthService authService = new AuthService(userDao);
|
||||||
|
MenuService menuService = new MenuService(menuItemDao);
|
||||||
|
OrderService orderService = new OrderService(orderDao, orderDetailDao, menuItemDao);
|
||||||
|
|
||||||
|
|
||||||
|
ConsoleMenu menu = new ConsoleMenu(authService, menuService, orderService);
|
||||||
menu.start();
|
menu.start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,59 @@
|
|||||||
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() {
|
public List<MenuItem> findAll() {
|
||||||
|
List<MenuItem> items = new ArrayList<>();
|
||||||
|
String sql = "SELECT * FROM menu_items";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Retrieve all menu items
|
Statement stmt = conn.createStatement();
|
||||||
|
ResultSet rs = stmt.executeQuery(sql)){
|
||||||
|
|
||||||
return null;
|
while (rs.next()){
|
||||||
|
items.add(new MenuItem(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getString("name"),
|
||||||
|
rs.getString("description"),
|
||||||
|
rs.getDouble("price"),
|
||||||
|
rs.getString("category")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error fetching menu: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
public MenuItem findById(int id) {
|
||||||
|
String sql = "SELECT * FROM menu_items WHERE id = ?";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Find menu item by id
|
PreparedStatement pstmt = conn.prepareStatement(sql)){
|
||||||
|
|
||||||
|
pstmt.setInt(1, id);
|
||||||
|
ResultSet rs = pstmt.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) {
|
||||||
|
System.err.println("Error finding menu item: " + e.getMessage());
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,25 +1,56 @@
|
|||||||
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) {
|
||||||
|
String sql = "INSERT INTO orders (user_id, total_price) VALUES (?, ?) RETURNING id";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Insert order and return generated id
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
pstmt.setInt(1, order.getUserId());
|
||||||
|
pstmt.setDouble(2, order.getTotalPrice());
|
||||||
|
|
||||||
|
try (ResultSet rs = pstmt.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getInt(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error saving order: " + e.getMessage());
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Order> findByUserId(int userId) {
|
public List<Order> findByUserId(int userId) {
|
||||||
|
List<Order> orders = new ArrayList<>();
|
||||||
|
String sql = "SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Retrieve all orders of a user
|
PreparedStatement pstmt = conn.prepareStatement(sql)){
|
||||||
|
|
||||||
return null;
|
pstmt.setInt(1, userId);
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()){
|
||||||
|
Order order = new Order(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getInt("user_id"),
|
||||||
|
rs.getTimestamp("created_at").toLocalDateTime(),
|
||||||
|
rs.getDouble("total_price")
|
||||||
|
);
|
||||||
|
orders.add(order);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error fetching order history: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return orders;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,57 @@
|
|||||||
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) {
|
||||||
|
String sql = "INSERT INTO order_details (order_id, menu_item_id, quantity, unit_price) VALUES (?, ?, ?)";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Insert order detail
|
PreparedStatement pstmt = conn.prepareStatement(sql)){
|
||||||
|
|
||||||
|
pstmt.setInt(1, detail.getOrderId());
|
||||||
|
pstmt.setInt(2, detail.getMenuItemId());
|
||||||
|
pstmt.setInt(3, detail.getQuantity());
|
||||||
|
pstmt.setDouble(4, detail.getPrice());
|
||||||
|
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error saving order detail: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<OrderDetail> findByOrderId(int orderId) {
|
public List<OrderDetail> findByOrderId(int orderId) {
|
||||||
|
List<OrderDetail> details = new ArrayList<>();
|
||||||
|
String sql = "SELECT * FROM order_details WHERE order_id = ?";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Retrieve order details
|
PreparedStatement pstmt = conn.prepareStatement(sql)){
|
||||||
|
|
||||||
return null;
|
pstmt.setInt(1, orderId);
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
while (rs.next()){
|
||||||
|
details.add(new OrderDetail(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getInt("order_id"),
|
||||||
|
rs.getInt("menu_item_id"),
|
||||||
|
rs.getInt("quantity"),
|
||||||
|
rs.getDouble("unit_price")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error fetching order details: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return details;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,51 @@
|
|||||||
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) {
|
public boolean save(User user) {
|
||||||
|
String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
|
||||||
|
|
||||||
// TODO:
|
try(Connection conn = DatabaseConnection.getConnection();
|
||||||
// Insert user into database
|
PreparedStatement pstmt = conn.prepareStatement(sql)){
|
||||||
|
|
||||||
|
pstmt.setString(1, user.getUsername());
|
||||||
|
pstmt.setString(2, user.getPassword());
|
||||||
|
pstmt.setString(3, user.getEmail());
|
||||||
|
|
||||||
|
int affectedRows = pstmt.executeUpdate();
|
||||||
|
return affectedRows > 0;
|
||||||
|
|
||||||
|
}catch (SQLException e) {
|
||||||
|
System.err.println("Error saving user: " + e.getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public User findByUsername(String username) {
|
public User findByUsername(String username) {
|
||||||
|
String sql = "SELECT * FROM users WHERE username = ?";
|
||||||
|
|
||||||
// TODO:
|
try(Connection conn = DatabaseConnection.getConnection();
|
||||||
// Find a user by username
|
PreparedStatement pstmt = conn.prepareStatement(sql)){
|
||||||
|
|
||||||
|
pstmt.setString(1, username);
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()){
|
||||||
|
return new User(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getString("username"),
|
||||||
|
rs.getString("password"),
|
||||||
|
rs.getString("email")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error finding user: " + e.getMessage());
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
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";
|
||||||
|
private static final String USER = "postgres";
|
||||||
private static final String USER = "postgres"; // Your Username
|
private static final String PASSWORD = "Zahra1386";
|
||||||
|
|
||||||
private static final String PASSWORD = "password"; // Your Password
|
|
||||||
|
|
||||||
private DatabaseConnection() {
|
private DatabaseConnection() {
|
||||||
|
|
||||||
@@ -17,11 +16,6 @@ public class DatabaseConnection {
|
|||||||
|
|
||||||
public static Connection getConnection()
|
public static Connection getConnection()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
|
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||||
// TODO:
|
|
||||||
// Return a valid PostgreSQL connection
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -3,13 +3,38 @@ package dev.model;
|
|||||||
public class MenuItem {
|
public class MenuItem {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
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; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("%d. %s - $%.2f (%s)", id, name, price, category);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,11 +5,28 @@ import java.time.LocalDateTime;
|
|||||||
public class Order {
|
public class Order {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private int userId;
|
private int userId;
|
||||||
|
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
private double totalPrice;
|
private double totalPrice;
|
||||||
|
|
||||||
|
public Order() {}
|
||||||
|
|
||||||
|
public Order(int id, int userId, LocalDateTime createdAt, double totalPrice) {
|
||||||
|
this.id = id;
|
||||||
|
this.userId = userId;
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
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; }
|
||||||
}
|
}
|
||||||
@@ -3,13 +3,33 @@ package dev.model;
|
|||||||
public class OrderDetail {
|
public class OrderDetail {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private int orderId;
|
private int orderId;
|
||||||
|
|
||||||
private int menuItemId;
|
private int menuItemId;
|
||||||
|
|
||||||
private int quantity;
|
private int quantity;
|
||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
|
public OrderDetail() {}
|
||||||
|
|
||||||
|
public OrderDetail(int id, int orderId, int menuItemId, int quantity, double price) {
|
||||||
|
this.id = id;
|
||||||
|
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,28 @@ 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(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,30 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.UserDao;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
public class AuthService {
|
public class AuthService {
|
||||||
|
|
||||||
|
private final UserDao userDao = new UserDao();
|
||||||
|
|
||||||
|
public AuthService(UserDao userDao) {}
|
||||||
|
|
||||||
public boolean register(String username, String password, String email) {
|
public boolean register(String username, String password, String email) {
|
||||||
|
String hashedPassword = Integer.toHexString(password.hashCode());
|
||||||
|
User user = new User(0, username, hashedPassword, email);
|
||||||
|
|
||||||
// TODO:
|
return userDao.save(user);
|
||||||
// Validate and register user
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public User login(String username, String password) {
|
public User login(String username, String password) {
|
||||||
|
User user = userDao.findByUsername(username);
|
||||||
|
|
||||||
// TODO:
|
if (user != null ){
|
||||||
// Authenticate user
|
String hashedInput = Integer.toHexString(password.hashCode());
|
||||||
|
if (user.getPassword().equals(hashedInput)){
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,47 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.MenuItemDao;
|
||||||
|
import dev.model.MenuItem;
|
||||||
|
import dev.model.User;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class MenuService {
|
public class MenuService {
|
||||||
|
private final MenuItemDao menuItemDao;
|
||||||
|
private Scanner scanner = new Scanner(System.in);
|
||||||
|
private User currentUsers;
|
||||||
|
|
||||||
public void showMenu() {
|
public MenuService(MenuItemDao menuItemDao) {
|
||||||
|
this.menuItemDao = menuItemDao;
|
||||||
// TODO:
|
this.scanner = scanner;
|
||||||
// Display menu items
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCurrentUsers(User user){
|
||||||
|
this.currentUsers = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void logout(){
|
||||||
|
this.currentUsers = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showMenuItems() {
|
||||||
|
if (currentUsers == null) {
|
||||||
|
System.out.println(" Please login first!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<MenuItem> items = menuItemDao.findAll();
|
||||||
|
if (items.isEmpty()) {
|
||||||
|
System.out.println("No menu items available.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n--- 🍴 RESTAURANT MENU ---");
|
||||||
|
for (MenuItem item : items) {
|
||||||
|
System.out.println(item.toString());
|
||||||
|
System.out.println(" " + item.getDescription());
|
||||||
|
}
|
||||||
|
System.out.println("---------------------------");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,26 +1,133 @@
|
|||||||
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.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class OrderService {
|
public class OrderService {
|
||||||
|
private final OrderDao orderDao ;
|
||||||
|
private final OrderDetailDao orderDetailDao ;
|
||||||
|
private final MenuItemDao menuItemDao ;
|
||||||
|
private final Scanner scanner ;
|
||||||
|
|
||||||
|
public OrderService(OrderDao orderDao, OrderDetailDao orderDetailDao, MenuItemDao menuItemDao) {
|
||||||
|
this.orderDao = orderDao;
|
||||||
|
this.orderDetailDao = orderDetailDao;
|
||||||
|
this.menuItemDao = menuItemDao;
|
||||||
|
this.scanner = new Scanner(System.in);
|
||||||
|
}
|
||||||
|
|
||||||
public void placeOrder(int userId) {
|
public void placeOrder(int userId) {
|
||||||
|
List<OrderDetail> cart = new ArrayList<>();
|
||||||
|
double totalPrice = 0.0;
|
||||||
|
|
||||||
// TODO:
|
System.out.println("\n--- 🛒 STARTING NEW ORDER ---");
|
||||||
// Create order
|
System.out.println("Enter Menu ID to add to cart. Type '0' to finish and checkout.");
|
||||||
|
|
||||||
|
|
||||||
|
while (true){
|
||||||
|
List<MenuItem> menu = menuItemDao.findAll();
|
||||||
|
for (MenuItem item : menu) {
|
||||||
|
System.out.println(item.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("\nSelect Menu ID: ");
|
||||||
|
int selectedId;
|
||||||
|
try {
|
||||||
|
selectedId = Integer.parseInt(scanner.nextLine());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.out.println(" Invalid input. Please enter a number.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selectedId == 0) break;
|
||||||
|
|
||||||
|
MenuItem item = menuItemDao.findById(selectedId);
|
||||||
|
if (item == null) {
|
||||||
|
System.out.println(" Invalid Menu ID! Try again.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("Enter quantity for " + item.getName() + ": ");
|
||||||
|
int qty;
|
||||||
|
try {
|
||||||
|
qty = Integer.parseInt(scanner.nextLine());
|
||||||
|
if (qty <= 0) throw new NumberFormatException();
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
System.out.println(" Quantity must be a positive number.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
double subtotal = item.getPrice() * qty;
|
||||||
|
totalPrice += subtotal;
|
||||||
|
cart.add(new OrderDetail(0, 0, item.getId(), qty, item.getPrice()));
|
||||||
|
System.out.println(" Added to cart.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cart.isEmpty()) {
|
||||||
|
System.out.println("🛒 Cart is empty. Order cancelled.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Order order = new Order(0, userId, LocalDateTime.now(), totalPrice);
|
||||||
|
int orderId = orderDao.save(order);
|
||||||
|
|
||||||
|
if (orderId != -1){
|
||||||
|
for (OrderDetail detail : cart){
|
||||||
|
detail.setOrderId(orderId);
|
||||||
|
orderDetailDao.save(detail);
|
||||||
|
}
|
||||||
|
System.out.println("\n Order placed successfully!");
|
||||||
|
printReceipt(orderId);
|
||||||
|
}else {
|
||||||
|
System.out.println(" Error occurred while saving the order.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printReceipt(int orderId) {
|
public void printReceipt(int orderId) {
|
||||||
|
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||||
|
double grandTotal = 0.0;
|
||||||
|
|
||||||
// TODO:
|
System.out.println("\n=================================");
|
||||||
// Print order receipt
|
System.out.println(" RECEIPT");
|
||||||
|
System.out.println("=================================");
|
||||||
|
|
||||||
|
|
||||||
|
for (OrderDetail detail : details){
|
||||||
|
MenuItem item = menuItemDao.findById(detail.getMenuItemId());
|
||||||
|
if (item == null) continue;
|
||||||
|
|
||||||
|
double subtotal = detail.getQuantity() * detail.getPrice();
|
||||||
|
grandTotal += subtotal;
|
||||||
|
|
||||||
|
System.out.printf("%-15s x%d $%.2f%n", item.getName(), detail.getQuantity(), subtotal);
|
||||||
|
}
|
||||||
|
System.out.println("--------------------------------");
|
||||||
|
System.out.printf("GRAND TOTAL: $%.2f%n", grandTotal);
|
||||||
|
System.out.println("================================\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showOrderHistory(int userId) {
|
public void showOrderHistory(int userId) {
|
||||||
|
List<Order> orders = orderDao.findByUserId(userId);
|
||||||
|
|
||||||
// TODO:
|
if (orders.isEmpty()){
|
||||||
// Display user's order history
|
System.out.println("No previous orders found.");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("\n========== YOUR ORDER HISTORY ==========");
|
||||||
|
for (Order order : orders) {
|
||||||
|
System.out.printf("Order ID: %d | Date: %s | Total: $%.2f%n",
|
||||||
|
order.getId(), order.getCreatedAt().toString(), order.getTotalPrice());
|
||||||
|
}
|
||||||
|
System.out.println("------------------------------");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,32 +1,68 @@
|
|||||||
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 AuthService authService;
|
||||||
|
private final MenuService menuService;
|
||||||
|
private final OrderService orderService;
|
||||||
|
|
||||||
private final Scanner scanner =
|
public ConsoleMenu(AuthService authService, MenuService menuService, OrderService orderService) {
|
||||||
new Scanner(System.in);
|
this.scanner = new Scanner(System.in);
|
||||||
|
this.authService = authService;
|
||||||
|
this.menuService = menuService;
|
||||||
|
this.orderService = orderService;
|
||||||
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
System.out.println("===== JAVA PIZZERIA =====");
|
System.out.println("===== \uD83C\uDF55 PIZZA RESTAURANT SYSTEM =====");
|
||||||
System.out.println("1. Login");
|
System.out.println("1. Login");
|
||||||
System.out.println("2. Register");
|
System.out.println("2. Register");
|
||||||
System.out.println("3. Exit");
|
System.out.println("3. Exit");
|
||||||
|
System.out.print("Select an option: ");
|
||||||
|
|
||||||
int choice = scanner.nextInt();
|
int choice = Integer.parseInt(scanner.nextLine());
|
||||||
|
|
||||||
switch (choice) {
|
switch (choice) {
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
// TODO
|
System.out.println("Username: ");
|
||||||
|
String user = scanner.nextLine();
|
||||||
|
System.out.println("Password: ");
|
||||||
|
String pass = scanner.nextLine();
|
||||||
|
|
||||||
|
User user1 = authService.login(user,pass);
|
||||||
|
if (user1 != null){
|
||||||
|
System.out.println("Welcome back");
|
||||||
|
menuService.setCurrentUsers(user1);
|
||||||
|
showMainMenu(user1);
|
||||||
|
}else {
|
||||||
|
System.out.println("Login failed");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
// TODO
|
System.out.println("New username: ");
|
||||||
|
String user2 = scanner.nextLine();
|
||||||
|
System.out.println("New password: ");
|
||||||
|
String pass2 = scanner.nextLine();
|
||||||
|
System.out.println("Email: ");
|
||||||
|
String email = scanner.nextLine();
|
||||||
|
|
||||||
|
authService.register(user2, pass2, email);
|
||||||
|
System.out.println("Registration successful");
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
@@ -40,5 +76,38 @@ public class ConsoleMenu {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
private void showMainMenu(User authenticatedUser) {
|
||||||
|
while (true) {
|
||||||
|
System.out.println("\n===== MAIN MENU =====");
|
||||||
|
System.out.println("1. Show Menu");
|
||||||
|
System.out.println("2. Place Order");
|
||||||
|
System.out.println("3. Order History");
|
||||||
|
System.out.println("4. Logout");
|
||||||
|
System.out.print("Select an option: ");
|
||||||
|
|
||||||
|
int choice = Integer.parseInt(scanner.nextLine());
|
||||||
|
|
||||||
|
switch (choice) {
|
||||||
|
case 1:
|
||||||
|
menuService.showMenuItems();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
orderService.placeOrder(authenticatedUser.getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
orderService.showOrderHistory(authenticatedUser.getId());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
menuService.logout();
|
||||||
|
System.out.println("Logged out");
|
||||||
|
return;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Invalid choice");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user