Compare commits
3
Commits
00f990c653
...
1f4e9eff3a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f4e9eff3a | ||
|
|
735940a4e0 | ||
|
|
acb1983ae3 |
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
+12
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="postgres@localhost" uuid="cefa0168-48c2-4c70-8fa7-1ab9ac6efe72">
|
||||
<driver-ref>postgresql</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:postgresql://localhost:5432/postgres</jdbc-url>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+20
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://maven.devneeds.ir/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+12
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK" />
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="file://$PROJECT_DIR$/database.sql" dialect="GenericSQL" />
|
||||
</component>
|
||||
</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>
|
||||
+45
-135
@@ -1,141 +1,51 @@
|
||||
-- Restaurant Database Management System
|
||||
--
|
||||
-- Instructions:
|
||||
-- 1. Create all required tables.
|
||||
-- 2. Design appropriate PRIMARY KEY and FOREIGN KEY relationships.
|
||||
-- 3. Add suitable constraints based on the requirements.
|
||||
-- 4. Insert initial mock data.
|
||||
-- 5. Insert at least 3 menu items.
|
||||
-- 6. The script should be executable from start to finish without errors.
|
||||
CREATE TABLE users(
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
username VARCHAR(20) NOT NULL UNIQUE,
|
||||
password_hash VARCHAR(256) NOT NULL,
|
||||
email VARCHAR(50)
|
||||
);
|
||||
|
||||
CREATE TABLE menu_items(
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name VARCHAR(50) NOT NULL,
|
||||
description VARCHAR(100),
|
||||
price DECIMAL NOT NULL CHECK (price > 0),
|
||||
category VARCHAR(50)
|
||||
);
|
||||
|
||||
CREATE TABLE customer_orders(
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
|
||||
total_price DECIMAL(10 , 2),
|
||||
CONSTRAINT fk_user_id
|
||||
FOREIGN KEY (user_id)
|
||||
REFERENCES users(id)
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE order_details (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
order_id BIGINT NOT NULL,
|
||||
menu_item_id BIGINT NOT NULL,
|
||||
quantity INTEGER NOT NULL CHECK (quantity > 0),
|
||||
unit_price DECIMAL(10 , 2) NOT NULL,
|
||||
|
||||
-- =======================================================
|
||||
-- USER TABLE
|
||||
-- =======================================================
|
||||
--
|
||||
-- Represents customers using the system.
|
||||
--
|
||||
-- Required information:
|
||||
-- - Unique identifier
|
||||
-- - Username
|
||||
-- - Password
|
||||
-- - Email (optional)
|
||||
--
|
||||
-- Requirements:
|
||||
-- - Each user must have a unique identifier.
|
||||
-- - Usernames must be unique.
|
||||
-- - Username and password are required.
|
||||
-- - Passwords should not be stored in plain text.
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
CONSTRAINT fk_order
|
||||
FOREIGN KEY (order_id)
|
||||
REFERENCES customer_orders(id) ,
|
||||
|
||||
CONSTRAINT fk_menu_item
|
||||
FOREIGN KEY (menu_item_id)
|
||||
REFERENCES menu_items(id)
|
||||
|
||||
);
|
||||
|
||||
|
||||
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');
|
||||
|
||||
-- =======================================================
|
||||
-- MENU ITEM TABLE
|
||||
-- =======================================================
|
||||
--
|
||||
-- Represents available food and drink items.
|
||||
--
|
||||
-- Required information:
|
||||
-- - Unique identifier
|
||||
-- - Name
|
||||
-- - Description (optional)
|
||||
-- - Price
|
||||
-- - Category (optional)
|
||||
--
|
||||
-- Requirements:
|
||||
-- - Each menu item must have a unique identifier.
|
||||
-- - Name is required.
|
||||
-- - Price must always be positive.
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
-- ORDER TABLE
|
||||
-- =======================================================
|
||||
--
|
||||
-- Represents orders placed by customers.
|
||||
--
|
||||
-- Required information:
|
||||
-- - Unique identifier
|
||||
-- - Reference to customer
|
||||
-- - Creation date and time
|
||||
-- - Total price
|
||||
--
|
||||
-- Requirements:
|
||||
-- - Each order must belong to exactly one user.
|
||||
-- - A user can have multiple orders.
|
||||
-- - The relationship between User and Order must be implemented.
|
||||
--
|
||||
-- Note:
|
||||
-- Avoid using reserved SQL keywords as table names.
|
||||
-- Consider using a name such as "orders" or "customer_orders".
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
-- ORDER DETAIL TABLE
|
||||
-- =======================================================
|
||||
--
|
||||
-- Represents items inside an order.
|
||||
--
|
||||
-- Required information:
|
||||
-- - Unique identifier
|
||||
-- - Reference to an order
|
||||
-- - Reference to a menu item
|
||||
-- - Quantity
|
||||
-- - Item price at purchase time
|
||||
--
|
||||
-- Requirements:
|
||||
-- - Each detail record must belong to one order.
|
||||
-- - Each detail record must reference one menu item.
|
||||
-- - Quantity must always be greater than zero.
|
||||
-- - Store the item's price at the moment of purchase.
|
||||
--
|
||||
-- CREATE TABLE ...
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
-- INITIAL MENU DATA
|
||||
-- =======================================================
|
||||
--
|
||||
-- Insert at least 3 food or drink items.
|
||||
--
|
||||
-- Example categories:
|
||||
-- - Pizza
|
||||
-- - Burger
|
||||
-- - Pasta
|
||||
-- - Drink
|
||||
--
|
||||
-- INSERT INTO ...
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
-- OPTIONAL TEST DATA
|
||||
-- =======================================================
|
||||
--
|
||||
-- You may insert sample users and orders for testing.
|
||||
-- This section is optional.
|
||||
--
|
||||
-- INSERT INTO ...
|
||||
|
||||
|
||||
|
||||
-- =======================================================
|
||||
-- VERIFICATION QUERIES
|
||||
-- =======================================================
|
||||
--
|
||||
-- Uncomment these queries to verify your database.
|
||||
--
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
-- SELECT * FROM ...;
|
||||
@@ -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,25 +1,77 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MenuItemDao {
|
||||
|
||||
public List<MenuItem> findAll() {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all menu items
|
||||
List<MenuItem> result = new ArrayList<>();
|
||||
|
||||
String quary = "SELECT * FROM menu_items";
|
||||
|
||||
|
||||
try (Connection c = DatabaseConnection.getConnection();
|
||||
Statement pst = c.createStatement()){
|
||||
|
||||
ResultSet rst = pst.executeQuery(quary);
|
||||
while (rst.next()){
|
||||
MenuItem item = new MenuItem(rst.getInt("id"),
|
||||
rst.getString("name"),
|
||||
rst.getString("description"),
|
||||
rst.getDouble("price"),
|
||||
rst.getString("category"));
|
||||
result.add(item);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public MenuItem findById(int id) {
|
||||
String quary = "SELECT * FROM menu_items WHERE id = ?";
|
||||
|
||||
// TODO:
|
||||
// Find menu item by id
|
||||
|
||||
try (Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement pst = c.prepareStatement(quary)){
|
||||
|
||||
pst.setInt(1 , id);
|
||||
|
||||
try (ResultSet rst = pst.executeQuery()){
|
||||
if(rst.next()){
|
||||
MenuItem item = new MenuItem(rst.getInt("id"),
|
||||
rst.getString("name"),
|
||||
rst.getString("description"),
|
||||
rst.getDouble("price"),
|
||||
rst.getString("category"));
|
||||
|
||||
|
||||
return item;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +1,86 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDao {
|
||||
|
||||
public int save(Order order) {
|
||||
|
||||
// TODO:
|
||||
// Insert order and return generated id
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String query = "INSERT INTO customer_orders (user_id, time, total_price) VALUES (?, ?, ?)";
|
||||
|
||||
try(Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement pst = c.prepareStatement(query , Statement.RETURN_GENERATED_KEYS)){
|
||||
|
||||
pst.setInt(1 , order.getUserId());
|
||||
pst.setTimestamp(2 , Timestamp.valueOf(now));
|
||||
pst.setDouble(3 , order.getTotalPrice());
|
||||
|
||||
int i = pst.executeUpdate();
|
||||
if(i==1) System.out.println("Order saved");
|
||||
|
||||
try (ResultSet generatedKeys = pst.getGeneratedKeys()) {
|
||||
if (generatedKeys.next()) {
|
||||
int generatedId = generatedKeys.getInt(1);
|
||||
System.out.println("Order saved with ID: " + generatedId);
|
||||
return generatedId;
|
||||
} else {
|
||||
System.out.println("Creating order failed, no ID obtained.");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
String quary = "SELECT * FROM customer_orders WHERE user_id = ?";
|
||||
List<Order> result = new ArrayList<>();
|
||||
|
||||
|
||||
try (Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement pst = c.prepareStatement(quary)){
|
||||
|
||||
pst.setInt(1 , userId);
|
||||
|
||||
try (ResultSet rst = pst.executeQuery()){
|
||||
while (rst.next()){
|
||||
Order order = new Order(rst.getInt("id"),
|
||||
rst.getInt("user_id") ,
|
||||
rst.getTimestamp("time").toLocalDateTime(),
|
||||
rst.getDouble("total_price"));
|
||||
|
||||
result.add(order);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +1,71 @@
|
||||
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 quary = "INSERT INTO order_details (order_id, menu_item_id ,quantity , unit_price) VALUES (?, ?, ? ,?)";
|
||||
try (Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement prst = c.prepareStatement(quary)){
|
||||
prst.setInt(1, detail.getOrderId());
|
||||
prst.setInt(2, detail.getMenuItemId());
|
||||
prst.setInt(3, detail.getQuantity());
|
||||
prst.setDouble(4 , detail.getPrice());
|
||||
|
||||
int i = prst.executeUpdate();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
List<OrderDetail> result = new ArrayList<>();
|
||||
|
||||
String query = "Select * From order_details WHERE order_id = ?";
|
||||
|
||||
try(Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement pst = c.prepareStatement(query)) {
|
||||
|
||||
pst.setInt(1 , orderId);
|
||||
|
||||
try(ResultSet rst = pst.executeQuery()){
|
||||
while (rst.next()){
|
||||
OrderDetail orderDetail = new OrderDetail(rst.getInt("id"),
|
||||
rst.getInt("order_id"),
|
||||
rst.getInt("menu_item_id"),
|
||||
rst.getInt("quantity"),
|
||||
rst.getDouble("unit_price"));
|
||||
result.add(orderDetail);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}catch (SQLException e){
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,69 @@
|
||||
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;
|
||||
import org.mindrot.jbcrypt.BCrypt;
|
||||
|
||||
public class UserDao {
|
||||
|
||||
public boolean save(User user) {
|
||||
|
||||
// TODO:
|
||||
// Insert user into database
|
||||
String hashedPassword = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
|
||||
String query ="INSERT INTO users (username, password_hash, email) VALUES (?, ?, ?)";
|
||||
|
||||
try(Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement pst = c.prepareStatement(query)) {
|
||||
|
||||
pst.setString(1 , user.getUsername());
|
||||
pst.setString(2 , hashedPassword);
|
||||
pst.setString(3 , user.getEmail());
|
||||
|
||||
|
||||
int result = pst.executeUpdate();
|
||||
if(result==1){
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
public User findByUsername(String userName) {
|
||||
|
||||
// TODO:
|
||||
// Find a user by username
|
||||
String query = "SELECT * FROM users WHERE username = ?";
|
||||
|
||||
try (Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement pst = c.prepareStatement(query)) {
|
||||
|
||||
pst.setString(1, userName);
|
||||
|
||||
try (ResultSet rst = pst.executeQuery()) {
|
||||
if (rst.next()) {
|
||||
User user = new User(rst.getInt("id"),
|
||||
rst.getString("username"),
|
||||
rst.getString("password_hash"),
|
||||
rst.getString("email"));
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatabaseConnection {
|
||||
@@ -9,19 +10,18 @@ public class DatabaseConnection {
|
||||
|
||||
private static final String USER = "postgres"; // Your Username
|
||||
|
||||
private static final String PASSWORD = "password"; // Your Password
|
||||
private static final String PASSWORD = "12345678"; // Your Password
|
||||
|
||||
private DatabaseConnection() {
|
||||
|
||||
}
|
||||
|
||||
public static Connection getConnection()
|
||||
throws SQLException {
|
||||
public static Connection getConnection() throws SQLException {
|
||||
|
||||
// TODO:
|
||||
// Return a valid PostgreSQL connection
|
||||
Connection con = DriverManager.getConnection(URL , USER , PASSWORD);
|
||||
|
||||
return null;
|
||||
|
||||
return con;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,13 +3,38 @@ package dev.model;
|
||||
public class MenuItem {
|
||||
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private double price;
|
||||
|
||||
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 int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,11 +5,38 @@ import java.time.LocalDateTime;
|
||||
public class Order {
|
||||
|
||||
private int id;
|
||||
|
||||
private int userId;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private double totalPrice;
|
||||
|
||||
|
||||
public Order(int userId , LocalDateTime createdAt , double totalPrice){
|
||||
this.userId = userId;
|
||||
this.createdAt= createdAt;
|
||||
this.totalPrice =totalPrice;
|
||||
}
|
||||
|
||||
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 double getTotalPrice() {
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
public String getCreatedAt() {
|
||||
return createdAt.toString();
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,13 +3,44 @@ package dev.model;
|
||||
public class OrderDetail {
|
||||
|
||||
private int id;
|
||||
|
||||
private int orderId;
|
||||
|
||||
private int menuItemId;
|
||||
|
||||
private int quantity;
|
||||
|
||||
private double price;
|
||||
|
||||
public OrderDetail(int orderId , int menuItemId , int quantity , double price){
|
||||
this.orderId=orderId;
|
||||
this.menuItemId=menuItemId;
|
||||
this.quantity=quantity;
|
||||
this.price = 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 int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public int getMenuItemId() {
|
||||
return menuItemId;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package dev.model;
|
||||
|
||||
public class Resipt {
|
||||
|
||||
private MenuItem menuItem;
|
||||
private int quantity;
|
||||
|
||||
|
||||
public Resipt(MenuItem menuItem , int quantity){
|
||||
this.menuItem=menuItem;
|
||||
this.quantity=quantity;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
public MenuItem getMenuItem() {
|
||||
return menuItem;
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,29 @@ package dev.model;
|
||||
public class User {
|
||||
|
||||
private int id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
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 int getId() { return id; }
|
||||
public String getUsername() { return username; }
|
||||
public String getPassword() { return password; }
|
||||
public String getEmail() { return email; }
|
||||
|
||||
|
||||
}
|
||||
@@ -1,23 +1,51 @@
|
||||
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){
|
||||
User user = new User (username , password , email);
|
||||
if(userDao.save(user)){
|
||||
System.out.println("successfully register");
|
||||
return true;
|
||||
}else {
|
||||
System.out.println("register failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Username already exists.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public User login(String username, String password) {
|
||||
|
||||
// TODO:
|
||||
// Authenticate user
|
||||
User user = userDao.findByUsername(username);
|
||||
|
||||
if(user == null){
|
||||
System.out.println("Username not found.");
|
||||
return null;
|
||||
}else {
|
||||
String storedHash = user.getPassword();
|
||||
|
||||
if (BCrypt.checkpw(password, storedHash)) {
|
||||
return user;
|
||||
|
||||
} else {
|
||||
System.out.println("Incorrect password.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,28 @@
|
||||
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> allfood = menuItemDao.findAll();
|
||||
|
||||
for(int i = 0 ; i < allfood.size() ; i++){
|
||||
MenuItem food = allfood.get(i);
|
||||
System.out.printf("%-5d | %-20s | %-15s | %-10.2f | %s%n",
|
||||
food.getId(),
|
||||
food.getName(),
|
||||
food.getCategory(),
|
||||
food.getPrice(),
|
||||
food.getDescription());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,25 +1,116 @@
|
||||
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 dev.model.Resipt;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class OrderService {
|
||||
|
||||
private final Scanner scanner = new Scanner(System.in);
|
||||
private final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
private final OrderDao orderDao = new OrderDao();
|
||||
private final OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
List<Resipt> resipts = new ArrayList<>();
|
||||
double totalPrice= 0.0;
|
||||
|
||||
while (true){
|
||||
System.out.println("Enter the ID of the item to add (or 0 to finish):");
|
||||
int chooseId = scanner.nextInt();
|
||||
if (chooseId == 0) {
|
||||
break;
|
||||
}
|
||||
MenuItem chosen = menuItemDao.findById(chooseId);
|
||||
if (chosen == null) {
|
||||
System.out.println("❌ Invalid food! Please try again.");
|
||||
continue;
|
||||
}
|
||||
System.out.println("Enter quantity:");
|
||||
int quantity = scanner.nextInt();
|
||||
|
||||
if (quantity <= 0) {
|
||||
System.out.println("❌ Quantity must be greater than 0.");
|
||||
continue;
|
||||
}
|
||||
|
||||
resipts.add(new Resipt(chosen , quantity));
|
||||
totalPrice += chosen.getPrice() * quantity;
|
||||
System.out.println("ADD " + quantity + " " + chosen.getName());
|
||||
|
||||
}
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
Order order = new Order(userId , now , totalPrice);
|
||||
int orderId = orderDao.save(order);
|
||||
|
||||
System.out.println("orderId: "+ orderId);
|
||||
for (int i = 0; i < resipts.size() ; i++){
|
||||
OrderDetail orderDetail = new OrderDetail(orderId ,
|
||||
resipts.get(i).getMenuItem().getId()
|
||||
|
||||
,resipts.get(i).getQuantity()
|
||||
,resipts.get(i).getMenuItem().getPrice());
|
||||
|
||||
orderDetailDao.save(orderDetail);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
printReceipt(orderId);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
System.out.println("[Order Summary / Receipt]");
|
||||
System.out.println("--------------------------------------------------");
|
||||
System.out.println(" Item | Quantity | Unit | Total");
|
||||
System.out.println("--------------------------------------------------");
|
||||
|
||||
double totalprice = 0.0;
|
||||
List<OrderDetail> resipt = orderDetailDao.findByOrderId(orderId);
|
||||
for(int i = 0 ; i< resipt.size() ; i++){
|
||||
MenuItem menuItem = menuItemDao.findById(resipt.get(i).getMenuItemId());
|
||||
System.out.printf("%-20s | %-10d | %-10.2f | %-10.2f" ,menuItem.getName()
|
||||
,resipt.get(i).getQuantity() , menuItem.getPrice() , resipt.get(i).getQuantity()* menuItem.getPrice());
|
||||
|
||||
System.out.println();
|
||||
totalprice += resipt.get(i).getQuantity()* menuItem.getPrice();
|
||||
System.out.println("--------------------------------------------------");
|
||||
}
|
||||
System.out.println("Final price: " + totalprice );
|
||||
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
List<Order> orders = orderDao.findByUserId(userId);
|
||||
|
||||
System.out.printf("%-10s | %-30s | %-10s%n", "Order Id", "Date & Time", "Total Price");
|
||||
System.out.println("------------------------------------------------------------");
|
||||
for (Order o : orders){
|
||||
System.out.printf("%-10d | %-30s | $%-9.2f%n",
|
||||
o.getId(),
|
||||
o.getCreatedAt().toString(),
|
||||
o.getTotalPrice());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
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 user;
|
||||
|
||||
|
||||
public void start() {
|
||||
|
||||
@@ -16,18 +25,46 @@ public class ConsoleMenu {
|
||||
System.out.println("1. Login");
|
||||
System.out.println("2. Register");
|
||||
System.out.println("3. Exit");
|
||||
System.out.println("======================================= \n" +
|
||||
"Choose an option: ");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1:
|
||||
// TODO
|
||||
{
|
||||
System.out.println("Enter username:");
|
||||
String username =scanner.next();
|
||||
System.out.println("Enter password:");
|
||||
String password = scanner.next();
|
||||
|
||||
user = authService.login(username,password);
|
||||
|
||||
if(user == null){
|
||||
System.out.println("Try again");
|
||||
}
|
||||
else {
|
||||
System.out.println("Login success");
|
||||
menu();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 2:
|
||||
// TODO
|
||||
{
|
||||
System.out.println("Enter username:");
|
||||
String username =scanner.next();
|
||||
System.out.println("Enter password:");
|
||||
String password = scanner.next();
|
||||
System.out.println("Enter email:");
|
||||
String email = scanner.next();
|
||||
|
||||
|
||||
authService.register(username,password,email);
|
||||
break;
|
||||
}
|
||||
|
||||
case 3:
|
||||
return;
|
||||
@@ -39,6 +76,59 @@ public class ConsoleMenu {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void menu(){
|
||||
|
||||
while (true){
|
||||
System.out.println();
|
||||
System.out.println("========\uD83C\uDF7D\uFE0F MAIN MENU \uD83C\uDF7D\uFE0F========");
|
||||
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("======================================= \n" +
|
||||
"Choose an option: ");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
switch (choice){
|
||||
case 1:
|
||||
{
|
||||
menuService.showMenu();
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
orderService.placeOrder(user.getId());
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
orderService.showOrderHistory(user.getId());
|
||||
break;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
return;
|
||||
}
|
||||
default:{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user