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