Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
508c146732 |
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="21" 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>
|
||||
+47
-8
@@ -9,7 +9,6 @@
|
||||
-- 6. The script should be executable from start to finish without errors.
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
-- USER TABLE
|
||||
-- =======================================================
|
||||
@@ -30,6 +29,12 @@
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
|
||||
CREATE TABLE users(
|
||||
id SERIAL PRIMARY KEY,
|
||||
username TEXT UNIQUE NOT NULL,
|
||||
password VARCHAR(250) NOT NULL,
|
||||
email VARCHAR(100)
|
||||
);
|
||||
|
||||
|
||||
-- =======================================================
|
||||
@@ -52,7 +57,13 @@
|
||||
--
|
||||
-- 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(100)
|
||||
);
|
||||
|
||||
-- =======================================================
|
||||
-- ORDER TABLE
|
||||
@@ -77,7 +88,12 @@
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
|
||||
|
||||
CREATE TABLE orders (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INT NOT NULL REFERENCES users(id),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
total_price DECIMAL(10, 2) DEFAULT 0.00
|
||||
);
|
||||
|
||||
-- =======================================================
|
||||
-- ORDER DETAIL TABLE
|
||||
@@ -100,7 +116,13 @@
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
|
||||
|
||||
CREATE TABLE order_detail(
|
||||
id SERIAL PRIMARY KEY,
|
||||
order_id INT NOT NULL REFERENCES orders(id),
|
||||
menu_item_id INT NOT NULL REFERENCES menuItem(id),
|
||||
quantity INT NOT NULL CHECK ( quantity > 0 ),
|
||||
unit_price DECIMAL(10, 2) NOT NULL
|
||||
);
|
||||
|
||||
-- =======================================================
|
||||
-- INITIAL MENU DATA
|
||||
@@ -116,6 +138,12 @@
|
||||
--
|
||||
-- INSERT INTO ...
|
||||
|
||||
INSERT INTO menuItem(
|
||||
name, description, price, category
|
||||
) VALUES('Pizza Margherita', 'Classic pizza with tomato sauce and mozzarella', 10.00, 'Pizza'),
|
||||
('Beef Burger', 'Juicy beef patty with cheese, lettuce, and tomato', 8.50, 'Burger'),
|
||||
('Pasta Alfredo', 'Creamy pasta with grilled chicken and parmesan', 12.00, 'Pasta'),
|
||||
('Coca Cola', 'Cold soft drink', 2.00, 'Drink');
|
||||
|
||||
|
||||
-- =======================================================
|
||||
@@ -127,7 +155,18 @@
|
||||
--
|
||||
-- INSERT INTO ...
|
||||
|
||||
--AI generated
|
||||
INSERT INTO users (
|
||||
username, password, email
|
||||
) VALUES('ali_dev', '$2a$12$K8M6O2wWfG1...hashed_long_string...', 'ali@email.com'),
|
||||
('sara_k', '$2a$12$R9P2m1vXyZ7...hashed_long_string...', NULL),
|
||||
('reza_food', '$2a$12$T5N8b9zQpW3...hashed_long_string...', 'reza@email.com');
|
||||
|
||||
INSERT INTO orders (
|
||||
user_id, total_price
|
||||
) VALUES(1, 18.50), -- سفارش اول علی
|
||||
(2, 12.00), -- سفارش سارا
|
||||
(1, 20.00); -- سفارش دوم علی
|
||||
|
||||
-- =======================================================
|
||||
-- VERIFICATION QUERIES
|
||||
@@ -135,7 +174,7 @@
|
||||
--
|
||||
-- Uncomment these queries to verify your database.
|
||||
--
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
SELECT * FROM users;
|
||||
SELECT * FROM menuItem;
|
||||
SELECT * FROM orders;
|
||||
SELECT * FROM order_detail;
|
||||
@@ -27,6 +27,12 @@
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mindrot</groupId>
|
||||
<artifactId>jbcrypt</artifactId>
|
||||
<version>0.4</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,7 +1,13 @@
|
||||
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 {
|
||||
@@ -10,14 +16,53 @@ public class MenuItemDao {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all menu items
|
||||
List<MenuItem> menuItems = new ArrayList<>();
|
||||
String sql = "SELECT * FROM menu_items";
|
||||
|
||||
return null;
|
||||
try(Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareCall(sql);
|
||||
ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next())
|
||||
{
|
||||
int id = rs.getInt("id");
|
||||
String name = rs.getString("name");
|
||||
String description = rs.getString("description");
|
||||
double price = rs.getDouble("price");
|
||||
String category = rs.getString("category");
|
||||
|
||||
MenuItem menuItem = new MenuItem(id, name, description, price, category);
|
||||
menuItems.add(menuItem);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return menuItems;
|
||||
}
|
||||
|
||||
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.prepareCall(sql);) {
|
||||
stmt.setInt(1, id);
|
||||
try( ResultSet rs = stmt.executeQuery()) {
|
||||
if(rs.next())
|
||||
{
|
||||
int itemID = rs.getInt("id");
|
||||
String name = rs.getString("name");
|
||||
String description = rs.getString("description");
|
||||
double price = rs.getDouble("price");
|
||||
String category = rs.getString("category");
|
||||
MenuItem menuItem = new MenuItem(itemID, name, description, price, category);
|
||||
return menuItem;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.MenuItem;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDao {
|
||||
@@ -10,6 +15,22 @@ public class OrderDao {
|
||||
|
||||
// TODO:
|
||||
// Insert order and return generated id
|
||||
String sql = "INSERT INTO orders (user_id, total_price) VALUES (?, ?)";
|
||||
try (Connection conn = DatabaseConnection.getConnection();) {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setInt(1, order.getUserId());
|
||||
stmt.setDouble(2, order.getTotalPrice());
|
||||
int row = stmt.executeUpdate();
|
||||
if (row > 0) {
|
||||
try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
|
||||
if (generatedKeys.next()) {
|
||||
return generatedKeys.getInt(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -18,8 +39,26 @@ public class OrderDao {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
|
||||
return null;
|
||||
List<Order> orders = new ArrayList<>();
|
||||
String sql = "SELECT * FROM orders WHERE user_id = ?";
|
||||
try(Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setInt(1, userId);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
while (rs.next())
|
||||
{
|
||||
int id = rs.getInt("id");
|
||||
int orderUserId = rs.getInt("user_id");
|
||||
LocalDateTime localDateTime = rs.getTimestamp("created_at").toLocalDateTime();
|
||||
double totalPrice = rs.getDouble("total_price");
|
||||
Order order = new Order(id, orderUserId, localDateTime, totalPrice);
|
||||
orders.add(order);
|
||||
}
|
||||
}
|
||||
return orders;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDetailDao {
|
||||
@@ -10,15 +15,46 @@ public class OrderDetailDao {
|
||||
|
||||
// TODO:
|
||||
// Insert order detail
|
||||
|
||||
String sql = "INSERT INTO order_detail (order_id, menu_item_id,quantity, unit_price) VALUES (?, ?, ?, ?)";
|
||||
try (Connection conn = DatabaseConnection.getConnection();) {
|
||||
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setInt(1, detail.getOrderId());
|
||||
stmt.setInt(2, detail.getMenuItemId());
|
||||
stmt.setInt(3, detail.getQuantity());
|
||||
stmt.setDouble(4, detail.getPrice());
|
||||
stmt.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
// Retrieve all orders of a user
|
||||
List<OrderDetail> orderDetails = new ArrayList<>();
|
||||
String sql = "SELECT * FROM order_detail 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())
|
||||
{
|
||||
|
||||
return null;
|
||||
int id = rs.getInt("id");
|
||||
int orderUserId = rs.getInt("order_id");
|
||||
int menuItemId = rs.getInt("menu_item_id");
|
||||
int quantity = rs.getInt("quantity");
|
||||
double unitPrice = rs.getDouble("unit_price");
|
||||
OrderDetail orderDetail = new OrderDetail(id, orderUserId, menuItemId, quantity, unitPrice);
|
||||
orderDetails.add(orderDetail);
|
||||
}
|
||||
}
|
||||
return orderDetails;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.User;
|
||||
import org.mindrot.jbcrypt.BCrypt;
|
||||
import org.postgresql.jdbc.ResourceLock;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class UserDao {
|
||||
|
||||
@@ -8,16 +16,41 @@ public class UserDao {
|
||||
|
||||
// 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());
|
||||
String hashedPassword = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
|
||||
stmt.setString(2, hashedPassword);
|
||||
stmt.setString(3, user.getEmail());
|
||||
int row = stmt.executeUpdate();
|
||||
return row > 0;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
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()) {
|
||||
int id = rs.getInt("id");
|
||||
String password = rs.getString("password");
|
||||
String email = rs.getString("email");
|
||||
return new User(id, username, password, email);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatabaseConnection {
|
||||
@@ -9,7 +10,7 @@ public class DatabaseConnection {
|
||||
|
||||
private static final String USER = "postgres"; // Your Username
|
||||
|
||||
private static final String PASSWORD = "password"; // Your Password
|
||||
private static final String PASSWORD = ""; // Your Password
|
||||
|
||||
private DatabaseConnection() {
|
||||
|
||||
@@ -20,8 +21,7 @@ public class DatabaseConnection {
|
||||
|
||||
// TODO:
|
||||
// Return a valid PostgreSQL connection
|
||||
|
||||
return null;
|
||||
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package dev.model;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class MenuItem {
|
||||
|
||||
private int id;
|
||||
@@ -12,4 +14,32 @@ public class MenuItem {
|
||||
|
||||
private String category;
|
||||
|
||||
|
||||
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 String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,32 @@ public class Order {
|
||||
|
||||
private double totalPrice;
|
||||
|
||||
public Order(int id, int userId, LocalDateTime createdAt, double totalPrice) {
|
||||
this.id = id;
|
||||
this.userId = userId;
|
||||
this.createdAt = createdAt;
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
|
||||
public Order(int userId, double totalPrice) {
|
||||
this.userId = userId;
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public double getTotalPrice() {
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,42 @@ public class OrderDetail {
|
||||
|
||||
private double price;
|
||||
|
||||
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 OrderDetail(int menuItemId, int quantity, double price)
|
||||
{
|
||||
this.menuItemId = menuItemId;
|
||||
this.quantity = quantity;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getMenuItemId() {
|
||||
return menuItemId;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setOrderId(int orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,35 @@ public class User {
|
||||
|
||||
private String email;
|
||||
|
||||
|
||||
public User(int id, String username, String password, String email) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public User(String username, String password, String email)
|
||||
{
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,42 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.UserDao;
|
||||
import dev.model.User;
|
||||
import org.mindrot.jbcrypt.BCrypt;
|
||||
|
||||
public class AuthService {
|
||||
|
||||
private UserDao userDao = new UserDao();
|
||||
public boolean register(String username, String password, String email) {
|
||||
|
||||
// TODO:
|
||||
// Validate and register user
|
||||
|
||||
if(userDao.findByUsername(username) != null)
|
||||
{
|
||||
System.out.println("Username is already taken.");
|
||||
return false;
|
||||
}
|
||||
return userDao.save(new User(username, password, email));
|
||||
}
|
||||
|
||||
public User login(String username, String password) {
|
||||
|
||||
// TODO:
|
||||
// Authenticate user
|
||||
|
||||
User user = userDao.findByUsername(username);
|
||||
if(user == null)
|
||||
{
|
||||
System.out.println("Invalid username or password.");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
boolean isPasswordCorrect = BCrypt.checkpw(password, user.getPassword());
|
||||
if (!isPasswordCorrect) {
|
||||
System.out.println("Invalid password.");
|
||||
return null;
|
||||
}
|
||||
System.out.println("Login successful!");
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,32 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MenuService {
|
||||
|
||||
private MenuItemDao menuItemDao = new MenuItemDao();
|
||||
|
||||
public void showMenu() {
|
||||
|
||||
// TODO:
|
||||
// Display menu items
|
||||
List<MenuItem> items = menuItemDao.findAll();
|
||||
|
||||
//AI generated.
|
||||
System.out.println("Available Items:");
|
||||
if (items == null || items.isEmpty()) {
|
||||
System.out.println("No items available at the moment.");
|
||||
return;
|
||||
}
|
||||
for (MenuItem item : items) {
|
||||
System.out.printf("%d. %s - $%.2f%n",
|
||||
item.getId(),
|
||||
item.getName(),
|
||||
item.getPrice()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package dev.service;
|
||||
|
||||
public class OrderService {
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.dao.OrderDao;
|
||||
import dev.dao.OrderDetailDao;
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.MenuItem;
|
||||
import dev.model.Order;
|
||||
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.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class orderService {
|
||||
|
||||
private OrderDao orderDao = new OrderDao();
|
||||
private OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||
private MenuItemDao menuItemDao = new MenuItemDao();
|
||||
private MenuService menuService = new MenuService();
|
||||
private Scanner scanner = new Scanner(System.in);
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
System.out.println("\n[Placing Order]");
|
||||
menuService.showMenu();
|
||||
List<OrderDetail> cart = new ArrayList<>();
|
||||
double totalPrice = 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 item = menuItemDao.findById(itemId);
|
||||
if (item == null) {
|
||||
System.out.println("Invalid Item ID! Please try again.");
|
||||
continue;
|
||||
}
|
||||
System.out.print("Enter quantity: ");
|
||||
int quantity = scanner.nextInt();
|
||||
if (quantity <= 0) {
|
||||
System.out.println("Quantity must always be greater than zero!");
|
||||
continue;
|
||||
}
|
||||
double itemPrice = item.getPrice();
|
||||
double subTotal = itemPrice * quantity;
|
||||
totalPrice += subTotal;
|
||||
OrderDetail orderDetail = new OrderDetail(itemId, quantity, itemPrice);
|
||||
cart.add(orderDetail);
|
||||
System.out.printf("Added %dx %s to your cart.%n", quantity, item.getName());
|
||||
}
|
||||
|
||||
if (cart.isEmpty()) {
|
||||
System.out.println("Order cancelled. Your cart is empty.");
|
||||
return;
|
||||
}
|
||||
Order order = new Order(userId, totalPrice);
|
||||
int generatedOrderId = orderDao.save(order); // متد save باید آیدیِ ایجاد شده را برگرداند
|
||||
|
||||
if (generatedOrderId > 0) {
|
||||
for (OrderDetail detail : cart) {
|
||||
detail.setOrderId(generatedOrderId);
|
||||
orderDetailDao.save(detail);
|
||||
}
|
||||
System.out.println("\nOrder saved successfully!");
|
||||
printReceipt(generatedOrderId);
|
||||
} else {
|
||||
System.out.println("Failed to save order due to a database error.");
|
||||
}
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
//AI generated
|
||||
List<OrderDetail> orderDetails = orderDetailDao.findByOrderId(orderId);
|
||||
if (orderDetails.isEmpty()) {
|
||||
System.out.println("Receipt error: Order not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
// ۲. چاپ فاکتور دقیقاً بر اساس قالب ریدمی
|
||||
System.out.println("\n[Order Summary / Receipt]");
|
||||
System.out.println("---------------------------------------");
|
||||
System.out.printf("%-12s %-7s %-9s %-7s%n", "Item", "Qty", "Unit", "Total");
|
||||
System.out.println("---------------------------------------");
|
||||
|
||||
for (OrderDetail detail : orderDetails) {
|
||||
// برای نمایش نام غذا، آن را با آیدی از جدول منو میخوانیم
|
||||
MenuItem item = menuItemDao.findById(detail.getMenuItemId());
|
||||
String itemName = (item != null) ? item.getName() : "Unknown Item";
|
||||
|
||||
double totalItemPrice = detail.getPrice() * detail.getQuantity();
|
||||
|
||||
System.out.printf("%-12s %-7d $%-8.2f $%-7.2f%n",
|
||||
itemName,
|
||||
detail.getQuantity(),
|
||||
detail.getPrice(),
|
||||
totalItemPrice
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
//AI generated
|
||||
System.out.println("\n[Order History]");
|
||||
|
||||
|
||||
List<Order> orders = orderDao.findByUserId(userId);
|
||||
|
||||
if (orders == null || orders.isEmpty()) {
|
||||
System.out.println("You haven't placed any orders yet.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("--------------------------------------------");
|
||||
System.out.printf("%-10s %-22s %-10s%n", "Order ID", "Date & Time", "Amount Spent");
|
||||
System.out.println("--------------------------------------------");
|
||||
|
||||
for (Order o : orders) {
|
||||
System.out.printf("ID: %-6d %-22s $%-10.2f%n",
|
||||
o.getId(),
|
||||
o.getCreatedAt(), // این فیلد از نوع Timestamp یا String در مدل شماست
|
||||
o.getTotalPrice()
|
||||
);
|
||||
}
|
||||
System.out.println("--------------------------------------------");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +1,104 @@
|
||||
package dev.ui;
|
||||
|
||||
import dev.service.AuthService;
|
||||
import dev.service.MenuService;
|
||||
import dev.model.User;
|
||||
import dev.service.orderService;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ConsoleMenu {
|
||||
|
||||
private final Scanner scanner =
|
||||
new Scanner(System.in);
|
||||
private AuthService authService = new AuthService();
|
||||
private MenuService menuService = new MenuService();
|
||||
private orderService orderService = new orderService();
|
||||
private Scanner scanner = new Scanner(System.in);
|
||||
private User loggedInUser = null; // برای حفظ وضعیت کاربر لاگین شده
|
||||
|
||||
public void start() {
|
||||
|
||||
while (true) {
|
||||
if (loggedInUser == null) {
|
||||
showAuthMenu();
|
||||
} else {
|
||||
showRestaurantMenu();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("===== JAVA PIZZERIA =====");
|
||||
// ۱. منوی اولیه: لاگین و ثبتنام
|
||||
private void showAuthMenu() {
|
||||
System.out.println("=======================================");
|
||||
System.out.println(" 🍕 WELCOME TO JAVA PIZZERIA 🍕");
|
||||
System.out.println("=======================================");
|
||||
System.out.println("1. Login");
|
||||
System.out.println("2. Register");
|
||||
System.out.println("2. Register New Account");
|
||||
System.out.println("3. Exit");
|
||||
System.out.println("=======================================");
|
||||
System.out.print("Choose an option: ");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1:
|
||||
// TODO
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// TODO
|
||||
break;
|
||||
|
||||
case 3:
|
||||
return;
|
||||
|
||||
default:
|
||||
System.out.println("Invalid choice");
|
||||
int option = scanner.nextInt();
|
||||
scanner.nextLine(); // خالی کردن بافر اسکنر
|
||||
|
||||
switch (option) {
|
||||
case 1 -> handleLogin();
|
||||
case 2 -> handleRegister();
|
||||
case 3 -> {
|
||||
System.out.println("Goodbye!");
|
||||
System.exit(0);
|
||||
}
|
||||
default -> System.out.println("Invalid option! Try again.");
|
||||
}
|
||||
}
|
||||
|
||||
// ۲. منوی اصلی بعد از لاگین موفق
|
||||
private void showRestaurantMenu() {
|
||||
System.out.println("=======================================");
|
||||
System.out.println(" 🍽️ MAIN MENU 🍽️");
|
||||
System.out.println("=======================================");
|
||||
System.out.println("1. View Menu");
|
||||
System.out.println("2. Place a New Order");
|
||||
System.out.println("3. View Order History");
|
||||
System.out.println("4. Logout");
|
||||
System.out.println("=======================================");
|
||||
System.out.print("Choose an option: ");
|
||||
|
||||
int option = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
|
||||
switch (option) {
|
||||
case 1 -> menuService.showMenu(); // فقط متد نمایش رو از سرویس صدا میزنیم
|
||||
case 2 -> orderService.placeOrder(loggedInUser.getId()); // هدایت به بخش ثبت سفارش
|
||||
case 3 -> orderService.showOrderHistory(loggedInUser.getId());
|
||||
case 4 -> {
|
||||
loggedInUser = null; // خروج از حساب کاربری
|
||||
System.out.println("Logged out successfully.");
|
||||
}
|
||||
default -> System.out.println("Invalid option!");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleLogin() {
|
||||
System.out.println("[Login]");
|
||||
System.out.print("Enter username: ");
|
||||
String username = scanner.nextLine();
|
||||
System.out.print("Enter password: ");
|
||||
String password = scanner.nextLine();
|
||||
|
||||
// سپردن کار اصلی تایید به لایه سرویس
|
||||
loggedInUser = authService.login(username, password);
|
||||
}
|
||||
|
||||
private void handleRegister() {
|
||||
System.out.println("[Register]");
|
||||
System.out.print("Enter username: ");
|
||||
String username = scanner.nextLine();
|
||||
System.out.print("Enter password: ");
|
||||
String password = scanner.nextLine();
|
||||
System.out.print("Enter email (optional): ");
|
||||
String email = scanner.nextLine();
|
||||
boolean success = authService.register(username, password, email);
|
||||
if (success) {
|
||||
System.out.println("Account registered successfully! Please login.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user