finishing all the TODO options #1
Generated
+10
@@ -0,0 +1,10 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Ignored default folder with query files
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
Generated
+13
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="WS-10-Database" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+20
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://mirror-maven.runflare.com/maven2" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+12
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK" />
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
+39
-17
@@ -8,7 +8,10 @@
|
||||
-- 5. Insert at least 3 menu items.
|
||||
-- 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
|
||||
@@ -28,8 +31,12 @@
|
||||
-- - Username and password are required.
|
||||
-- - 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(255) NOT NULL,
|
||||
email VARCHAR(100)
|
||||
);
|
||||
|
||||
|
||||
-- =======================================================
|
||||
@@ -50,8 +57,13 @@
|
||||
-- - Name is required.
|
||||
-- - Price must always be positive.
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
|
||||
CREATE TABLE menu_items (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
description TEXT,
|
||||
price NUMERIC(10, 2) NOT NULL CHECK (price > 0),
|
||||
category VARCHAR(50)
|
||||
);
|
||||
|
||||
|
||||
-- =======================================================
|
||||
@@ -75,8 +87,12 @@
|
||||
-- Avoid using reserved SQL keywords as table names.
|
||||
-- Consider using a name such as "orders" or "customer_orders".
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
|
||||
CREATE TABLE orders (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMP NOT NULL,
|
||||
total_price NUMERIC(10, 2) NOT NULL DEFAULT 0.00
|
||||
);
|
||||
|
||||
|
||||
-- =======================================================
|
||||
@@ -98,8 +114,13 @@
|
||||
-- - Quantity must always be greater than zero.
|
||||
-- - Store the item's price at the moment of purchase.
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
|
||||
CREATE TABLE order_details (
|
||||
id SERIAL PRIMARY KEY,
|
||||
order_id INT NOT NULL REFERENCES orders(id) ON DELETE CASCADE,
|
||||
menu_item_id INT NOT NULL REFERENCES menu_items(id),
|
||||
quantity INT NOT NULL CHECK (quantity > 0),
|
||||
price NUMERIC(10, 2) NOT NULL
|
||||
);
|
||||
|
||||
|
||||
-- =======================================================
|
||||
@@ -114,8 +135,11 @@
|
||||
-- - Pasta
|
||||
-- - Drink
|
||||
--
|
||||
-- INSERT INTO ...
|
||||
|
||||
INSERT INTO menu_items (name, description, price, category) VALUES
|
||||
('Pizza', 'Delicious cheese pizza with tomato sauce', 10.00, 'Pizza'),
|
||||
('Burger', 'Juicy beef burger with lettuce and tomato', 8.00, 'Burger'),
|
||||
('Pasta', 'Rich creamy Alfredo pasta', 12.00, 'Pasta'),
|
||||
('Soda', 'Cold carbonated drink', 2.50, 'Drink');
|
||||
|
||||
|
||||
-- =======================================================
|
||||
@@ -125,8 +149,6 @@
|
||||
-- You may insert sample users and orders for testing.
|
||||
-- This section is optional.
|
||||
--
|
||||
-- INSERT INTO ...
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
@@ -135,7 +157,7 @@
|
||||
--
|
||||
-- Uncomment these queries to verify your database.
|
||||
--
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
SELECT * FROM users;
|
||||
SELECT * FROM menu_items;
|
||||
SELECT * FROM orders;
|
||||
SELECT * FROM order_details;
|
||||
@@ -1,25 +1,57 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MenuItemDao {
|
||||
|
||||
public List<MenuItem> findAll() {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all menu items
|
||||
|
||||
return null;
|
||||
List<MenuItem> items = new ArrayList<>();
|
||||
String sql = "SELECT * FROM menu_items";
|
||||
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.setDescription(rs.getString("description"));
|
||||
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) {
|
||||
|
||||
// TODO:
|
||||
// Find menu item by id
|
||||
|
||||
String sql = "SELECT * FROM menu_items WHERE 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.setDescription(rs.getString("description"));
|
||||
item.setPrice(rs.getDouble("price"));
|
||||
item.setCategory(rs.getString("category"));
|
||||
return item;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +1,52 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDao {
|
||||
|
||||
public int save(Order order) {
|
||||
|
||||
// TODO:
|
||||
// Insert order and return generated id
|
||||
|
||||
String sql = "INSERT INTO orders (user_id, created_at, total_price) VALUES (?, ?, ?)";
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setInt(1, order.getUserId());
|
||||
stmt.setTimestamp(2, Timestamp.valueOf(order.getCreatedAt()));
|
||||
stmt.setDouble(3, order.getTotalPrice());
|
||||
stmt.executeUpdate();
|
||||
try (ResultSet keys = stmt.getGeneratedKeys()) {
|
||||
if (keys.next()) {
|
||||
return keys.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
|
||||
return null;
|
||||
List<Order> orders = new ArrayList<>();
|
||||
String sql = "SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC";
|
||||
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("created_at").toLocalDateTime());
|
||||
order.setTotalPrice(rs.getDouble("total_price"));
|
||||
orders.add(order);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +1,51 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDetailDao {
|
||||
|
||||
public void save(OrderDetail detail) {
|
||||
|
||||
// TODO:
|
||||
// Insert order detail
|
||||
|
||||
String sql = "INSERT INTO order_details (order_id, menu_item_id, quantity, price) VALUES (?, ?, ?, ?)";
|
||||
try (Connection 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) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
|
||||
return null;
|
||||
List<OrderDetail> details = new ArrayList<>();
|
||||
String sql = "SELECT * FROM order_details WHERE order_id = ?";
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, orderId);
|
||||
try (ResultSet rs = stmt.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"));
|
||||
details.add(detail);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return details;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +1,47 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.User;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class UserDao {
|
||||
|
||||
public boolean save(User user) {
|
||||
|
||||
// TODO:
|
||||
// Insert user into database
|
||||
|
||||
return false;
|
||||
String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
|
||||
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());
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
return affectedRows > 0;
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
|
||||
// TODO:
|
||||
// Find a user by username
|
||||
|
||||
String sql = "SELECT * FROM users WHERE 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +1,19 @@
|
||||
package dev.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatabaseConnection {
|
||||
|
||||
private static final String URL = "jdbc:postgresql://localhost:5432/restaurant_db"; // DB Server
|
||||
|
||||
private static final String USER = "postgres"; // Your Username
|
||||
|
||||
private static final String PASSWORD = "password"; // Your Password
|
||||
private static final String PASSWORD = "123456"; // Your Password
|
||||
|
||||
private DatabaseConnection() {
|
||||
|
||||
}
|
||||
|
||||
public static Connection getConnection()
|
||||
throws SQLException {
|
||||
|
||||
// TODO:
|
||||
// Return a valid PostgreSQL connection
|
||||
|
||||
return null;
|
||||
public static Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,59 @@ package dev.model;
|
||||
public class MenuItem {
|
||||
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private double price;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,49 @@ import java.time.LocalDateTime;
|
||||
public class Order {
|
||||
|
||||
private int id;
|
||||
|
||||
private int userId;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
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,59 @@ package dev.model;
|
||||
public class OrderDetail {
|
||||
|
||||
private int id;
|
||||
|
||||
private int orderId;
|
||||
|
||||
private int menuItemId;
|
||||
|
||||
private int quantity;
|
||||
|
||||
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,49 @@ package dev.model;
|
||||
public class User {
|
||||
|
||||
private int id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
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,52 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.UserDao;
|
||||
import dev.model.User;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class AuthService {
|
||||
|
||||
private final UserDao userDao = new UserDao();
|
||||
|
||||
public boolean register(String username, String password, String email) {
|
||||
|
||||
// TODO:
|
||||
// Validate and register user
|
||||
|
||||
return false;
|
||||
if (username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (userDao.findByUsername(username) != null) {
|
||||
return false;
|
||||
}
|
||||
User user = new User();
|
||||
user.setUsername(username);
|
||||
user.setPassword(hashPassword(password));
|
||||
user.setEmail(email);
|
||||
return userDao.save(user);
|
||||
}
|
||||
|
||||
public User login(String username, String password) {
|
||||
|
||||
// TODO:
|
||||
// Authenticate user
|
||||
|
||||
User user = userDao.findByUsername(username);
|
||||
if (user != null && user.getPassword().equals(hashPassword(password))) {
|
||||
return user;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String hashPassword(String password) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
byte[] hash = digest.digest(password.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : hash) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) {
|
||||
hexString.append('0');
|
||||
}
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,23 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MenuService {
|
||||
|
||||
private final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
|
||||
public void showMenu() {
|
||||
|
||||
// TODO:
|
||||
// Display menu items
|
||||
|
||||
List<MenuItem> items = menuItemDao.findAll();
|
||||
System.out.println("\n--- MENU ---");
|
||||
for (MenuItem item : items) {
|
||||
System.out.printf("%d. %s - $%.2f (%s)\n", item.getId(), item.getName(), item.getPrice(), item.getCategory());
|
||||
if (item.getDescription() != null && !item.getDescription().isEmpty()) {
|
||||
System.out.println(" " + item.getDescription());
|
||||
}
|
||||
}
|
||||
System.out.println("------------");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +1,128 @@
|
||||
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 {
|
||||
|
||||
private final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
private final OrderDao orderDao = new OrderDao();
|
||||
private final OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||
private final Scanner scanner = new Scanner(System.in);
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
List<MenuItem> items = menuItemDao.findAll();
|
||||
if (items.isEmpty()) {
|
||||
System.out.println("No menu items are currently available.");
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
System.out.println("\n[Placing Order]");
|
||||
System.out.println("Available Items:");
|
||||
for (MenuItem item : items) {
|
||||
System.out.printf("%d. %s - $%.2f\n", item.getId(), item.getName(), item.getPrice());
|
||||
}
|
||||
|
||||
List<OrderDetail> cart = new ArrayList<>();
|
||||
double total = 0.0;
|
||||
|
||||
while (true) {
|
||||
System.out.print("\nEnter the ID of the item to add (or 0 to finish): ");
|
||||
int itemId = scanner.nextInt();
|
||||
if (itemId == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
MenuItem selectedItem = menuItemDao.findById(itemId);
|
||||
if (selectedItem == null) {
|
||||
System.out.println("Invalid item ID.");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.print("Enter quantity: ");
|
||||
int quantity = scanner.nextInt();
|
||||
if (quantity <= 0) {
|
||||
System.out.println("Quantity must be greater than zero.");
|
||||
continue;
|
||||
}
|
||||
|
||||
OrderDetail detail = new OrderDetail();
|
||||
detail.setMenuItemId(selectedItem.getId());
|
||||
detail.setQuantity(quantity);
|
||||
detail.setPrice(selectedItem.getPrice());
|
||||
|
||||
cart.add(detail);
|
||||
total += selectedItem.getPrice() * quantity;
|
||||
System.out.printf("Added %dx %s to your cart.\n", quantity, selectedItem.getName());
|
||||
}
|
||||
|
||||
if (cart.isEmpty()) {
|
||||
System.out.println("No items selected. Order canceled.");
|
||||
return;
|
||||
}
|
||||
|
||||
Order order = new Order();
|
||||
order.setUserId(userId);
|
||||
order.setCreatedAt(LocalDateTime.now());
|
||||
order.setTotalPrice(total);
|
||||
|
||||
int orderId = orderDao.save(order);
|
||||
if (orderId != -1) {
|
||||
for (OrderDetail detail : cart) {
|
||||
detail.setOrderId(orderId);
|
||||
orderDetailDao.save(detail);
|
||||
}
|
||||
printReceipt(orderId);
|
||||
System.out.println("Order saved successfully!");
|
||||
} else {
|
||||
System.out.println("An error occurred while saving the order.");
|
||||
}
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||
if (details.isEmpty()) {
|
||||
System.out.println("No records found for the requested receipt.");
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
|
||||
System.out.println("\n[Order Summary / Receipt]");
|
||||
System.out.println("---------------------------------------");
|
||||
System.out.printf("%-15s %-7s %-9s %-7s\n", "Item", "Qty", "Unit", "Total");
|
||||
System.out.println("---------------------------------------");
|
||||
double grandTotal = 0.0;
|
||||
for (OrderDetail detail : details) {
|
||||
MenuItem item = menuItemDao.findById(detail.getMenuItemId());
|
||||
String name = item != null ? item.getName() : "Unknown";
|
||||
double total = detail.getPrice() * detail.getQuantity();
|
||||
grandTotal += total;
|
||||
System.out.printf("%-15s %-7d $%-8.2f $%-7.2f\n", name, detail.getQuantity(), detail.getPrice(), total);
|
||||
}
|
||||
System.out.println("---------------------------------------");
|
||||
System.out.printf("Final Total: $%.2f\n", grandTotal);
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
List<Order> orders = orderDao.findByUserId(userId);
|
||||
if (orders.isEmpty()) {
|
||||
System.out.println("No past orders found.");
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
|
||||
System.out.println("\n--- ORDER HISTORY ---");
|
||||
for (Order order : orders) {
|
||||
System.out.printf("Order ID: %d | Date: %s | Total Spent: $%.2f\n",
|
||||
order.getId(), order.getCreatedAt().toString(), order.getTotalPrice());
|
||||
}
|
||||
System.out.println("---------------------");
|
||||
}
|
||||
|
||||
}
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/ui.iml" filepath="$PROJECT_DIR$/ui.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/../../../../.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+61
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="9b8ebb7d-0172-408b-90d8-a9faaf49873f" name="Changes" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/../../../../../database.sql" beforeDir="false" afterPath="$PROJECT_DIR$/../../../../../database.sql" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../dao/MenuItemDao.java" beforeDir="false" afterPath="$PROJECT_DIR$/../dao/MenuItemDao.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../dao/OrderDao.java" beforeDir="false" afterPath="$PROJECT_DIR$/../dao/OrderDao.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../dao/OrderDetailDao.java" beforeDir="false" afterPath="$PROJECT_DIR$/../dao/OrderDetailDao.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../dao/UserDao.java" beforeDir="false" afterPath="$PROJECT_DIR$/../dao/UserDao.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../model/MenuItem.java" beforeDir="false" afterPath="$PROJECT_DIR$/../model/MenuItem.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../model/Order.java" beforeDir="false" afterPath="$PROJECT_DIR$/../model/Order.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../model/OrderDetail.java" beforeDir="false" afterPath="$PROJECT_DIR$/../model/OrderDetail.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../model/User.java" beforeDir="false" afterPath="$PROJECT_DIR$/../model/User.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../service/AuthService.java" beforeDir="false" afterPath="$PROJECT_DIR$/../service/AuthService.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../service/MenuService.java" beforeDir="false" afterPath="$PROJECT_DIR$/../service/MenuService.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/../service/OrderService.java" beforeDir="false" afterPath="$PROJECT_DIR$/../service/OrderService.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/ConsoleMenu.java" beforeDir="false" afterPath="$PROJECT_DIR$/ConsoleMenu.java" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="Git.Settings">
|
||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../../../../.." />
|
||||
</component>
|
||||
<component name="ProjectColorInfo"><![CDATA[{
|
||||
"associatedIndex": 3
|
||||
}]]></component>
|
||||
<component name="ProjectId" id="3GdjGu6LnNtWa8g2IcBLLhISNS0" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"ModuleVcsDetector.initialDetectionPerformed": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"RunOnceActivity.git.unshallow": "true",
|
||||
"git-widget-placeholder": "develop",
|
||||
"kotlin-language-version-configured": "true"
|
||||
}
|
||||
}]]></component>
|
||||
<component name="SharedIndexes">
|
||||
<attachedChunks>
|
||||
<set>
|
||||
<option value="bundled-jdk-30f59d01ecdd-2fc7cc6b9a17-intellij.indexing.shared.core-IU-253.31033.145" />
|
||||
</set>
|
||||
</attachedChunks>
|
||||
</component>
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="Default task">
|
||||
<changelist id="9b8ebb7d-0172-408b-90d8-a9faaf49873f" name="Changes" comment="" />
|
||||
<created>1784308248011</created>
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1784308248011</updated>
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
</project>
|
||||
@@ -1,44 +1,121 @@
|
||||
package dev.ui;
|
||||
|
||||
import dev.model.User;
|
||||
import dev.service.AuthService;
|
||||
import dev.service.MenuService;
|
||||
import dev.service.OrderService;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ConsoleMenu {
|
||||
|
||||
private final Scanner scanner =
|
||||
new Scanner(System.in);
|
||||
private final Scanner scanner = new Scanner(System.in);
|
||||
private final AuthService authService = new AuthService();
|
||||
private final MenuService menuService = new MenuService();
|
||||
private final OrderService orderService = new OrderService();
|
||||
private User currentUser = null;
|
||||
|
||||
public void start() {
|
||||
|
||||
while (true) {
|
||||
if (currentUser == null) {
|
||||
System.out.println();
|
||||
System.out.println("===== JAVA PIZZERIA =====");
|
||||
System.out.println("1. Login");
|
||||
System.out.println("2. Register");
|
||||
System.out.println("3. Exit");
|
||||
System.out.print("Choose an option: ");
|
||||
|
||||
System.out.println();
|
||||
System.out.println("===== JAVA PIZZERIA =====");
|
||||
System.out.println("1. Login");
|
||||
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:
|
||||
if (!scanner.hasNextInt()) {
|
||||
scanner.next();
|
||||
System.out.println("Invalid choice");
|
||||
continue;
|
||||
}
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
handleLogin();
|
||||
break;
|
||||
case 2:
|
||||
handleRegister();
|
||||
break;
|
||||
case 3:
|
||||
return;
|
||||
default:
|
||||
System.out.println("Invalid choice");
|
||||
}
|
||||
} else {
|
||||
System.out.println();
|
||||
System.out.println("===== MAIN MENU =====");
|
||||
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.print("Choose an option: ");
|
||||
|
||||
if (!scanner.hasNextInt()) {
|
||||
scanner.next();
|
||||
System.out.println("Invalid choice");
|
||||
continue;
|
||||
}
|
||||
int choice = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
|
||||
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("Successfully logged out.");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Invalid choice");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void handleLogin() {
|
||||
System.out.println("\n[Login]");
|
||||
System.out.print("Enter username: ");
|
||||
String username = scanner.nextLine();
|
||||
System.out.print("Enter password: ");
|
||||
String password = scanner.nextLine();
|
||||
|
||||
User user = authService.login(username, password);
|
||||
if (user != null) {
|
||||
currentUser = user;
|
||||
System.out.println("Login successful. Welcome, " + currentUser.getUsername());
|
||||
} else {
|
||||
System.out.println("Invalid username or password.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleRegister() {
|
||||
System.out.println("\n[Register New Account]");
|
||||
System.out.print("Enter username: ");
|
||||
String username = scanner.nextLine();
|
||||
System.out.print("Enter password: ");
|
||||
String password = scanner.nextLine();
|
||||
System.out.print("Enter email (optional, press Enter to skip): ");
|
||||
String email = scanner.nextLine();
|
||||
if (email.trim().isEmpty()) {
|
||||
email = null;
|
||||
}
|
||||
|
||||
boolean success = authService.register(username, password, email);
|
||||
if (success) {
|
||||
System.out.println("Registration successful. You can now log in.");
|
||||
} else {
|
||||
System.out.println("Registration failed. The username may already exist or input was invalid.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" packagePrefix="dev.ui" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Reference in New Issue
Block a user