Implement database, daos and other classes
This commit is contained in:
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
+17
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||||
|
<data-source source="LOCAL" name="restaurant_db" uuid="58cfe2c8-dec0-4953-b2b5-821b6e7313f9">
|
||||||
|
<driver-ref>postgresql</driver-ref>
|
||||||
|
<synchronize>true</synchronize>
|
||||||
|
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||||
|
<jdbc-url>jdbc:postgresql://localhost:5432/postgres</jdbc-url>
|
||||||
|
<jdbc-additional-properties>
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||||
|
</jdbc-additional-properties>
|
||||||
|
<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>
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="SqlNoDataSourceInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||||
|
</profile>
|
||||||
|
</component>
|
||||||
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://maven.myket.ir/" />
|
||||||
|
</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="PostgreSQL" />
|
||||||
|
</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
-105
@@ -1,141 +1,81 @@
|
|||||||
-- 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.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- USER TABLE
|
-- USER TABLE
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
--
|
CREATE TABLE users(
|
||||||
-- Represents customers using the system.
|
id SERIAL PRIMARY KEY,
|
||||||
--
|
username VARCHAR(50) UNIQUE NOT NULL,
|
||||||
-- Required information:
|
password_hash TEXT NOT NULL,
|
||||||
-- - Unique identifier
|
email VARCHAR(255) UNIQUE
|
||||||
-- - 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 ...
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- MENU ITEM TABLE
|
-- MENU ITEM TABLE
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
--
|
CREATE TABLE menu(
|
||||||
-- Represents available food and drink items.
|
id SERIAL PRIMARY KEY,
|
||||||
--
|
name VARCHAR(250) NOT NULL,
|
||||||
-- Required information:
|
description TEXT,
|
||||||
-- - Unique identifier
|
price NUMERIC NOT NULL CHECK (price > 0),
|
||||||
-- - Name
|
category VARCHAR(50)
|
||||||
-- - 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
|
-- ORDER TABLE
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
--
|
CREATE TABLE orders(
|
||||||
-- Represents orders placed by customers.
|
id SERIAL PRIMARY KEY,
|
||||||
--
|
user_id INT NOT NULL,
|
||||||
-- Required information:
|
created_at TIMESTAMP NOT NULL ,
|
||||||
-- - Unique identifier
|
total_price NUMERIC NOT NULL,
|
||||||
-- - 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 ...
|
|
||||||
|
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- ORDER DETAIL TABLE
|
-- ORDER DETAIL TABLE
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
--
|
CREATE TABLE order_details(
|
||||||
-- Represents items inside an order.
|
id SERIAL PRIMARY KEY ,
|
||||||
--
|
order_id INT NOT NULL ,
|
||||||
-- Required information:
|
menu_id INT NOT NULL ,
|
||||||
-- - Unique identifier
|
quantity NUMERIC NOT NULL CHECK (quantity > 0),
|
||||||
-- - Reference to an order
|
item_price NUMERIC NOT NULL,
|
||||||
-- - 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 ...
|
|
||||||
|
|
||||||
|
FOREIGN KEY (order_id) REFERENCES orders(id),
|
||||||
|
FOREIGN KEY (menu_id) REFERENCES menu(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- INITIAL MENU DATA
|
-- INITIAL MENU DATA
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
--
|
-- id is left out because it has SERIAl type and auto-generates.
|
||||||
-- Insert at least 3 food or drink items.
|
INSERT INTO menu (name, description, price, category) VALUES
|
||||||
--
|
('Margherita Pizza', 'Classic tomato, mozzarella, and basil', 9.99, 'Pizza'),
|
||||||
-- Example categories:
|
('Grilled Chicken Wrap', 'Chicken, lettuce, tomato, garlic sauce', 7.49, 'Wraps'),
|
||||||
-- - Pizza
|
('Caesar Salad', 'Romaine, parmesan, croutons, caesar dressing', 6.99, 'Salads'),
|
||||||
-- - Burger
|
('Beef Burger', 'Beef patty, cheddar, lettuce, brioche bun', 8.99, 'Burgers'),
|
||||||
-- - Pasta
|
('Spaghetti Carbonara', 'Pasta, egg, pancetta, parmesan', 10.49, 'Pasta'),
|
||||||
-- - Drink
|
('Veggie Spring Rolls', 'Crispy rolls with mixed vegetables', 5.49, 'Appetizers'),
|
||||||
--
|
('Chocolate Lava Cake', 'Warm cake with molten chocolate center', 4.99, 'Dessert'),
|
||||||
-- INSERT INTO ...
|
('Iced Latte', 'Espresso with cold milk over ice', 3.49, 'Beverages'),
|
||||||
|
('Mushroom Risotto', 'Creamy arborio rice with wild mushrooms', 9.49, 'Pasta'),
|
||||||
|
('Fish Tacos', 'Grilled fish, cabbage slaw, chipotle mayo', 8.49, 'Tacos');
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- OPTIONAL TEST DATA
|
-- OPTIONAL TEST DATA
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
--
|
|
||||||
-- You may insert sample users and orders for testing.
|
|
||||||
-- This section is optional.
|
|
||||||
--
|
|
||||||
-- INSERT INTO ...
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
-- VERIFICATION QUERIES
|
-- VERIFICATION QUERIES
|
||||||
-- =======================================================
|
-- =======================================================
|
||||||
--
|
-- SELECT * FROM users;
|
||||||
-- Uncomment these queries to verify your database.
|
-- SELECT * FROM menu;
|
||||||
--
|
-- SELECT * FROM orders;
|
||||||
-- SELECT * FROM ...;
|
-- SELECT * FROM order_details;
|
||||||
-- SELECT * FROM ...;
|
|
||||||
-- SELECT * FROM ...;
|
|
||||||
-- SELECT * FROM ...;
|
|
||||||
@@ -1,25 +1,73 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.MenuItem;
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MenuItemDao {
|
public class MenuItemDao {
|
||||||
|
|
||||||
public List<MenuItem> findAll() {
|
public List<MenuItem> findAll() {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT * FROM menu";
|
||||||
// Retrieve all menu items
|
List<MenuItem> result = new ArrayList<>();
|
||||||
|
|
||||||
return null;
|
try{
|
||||||
|
|
||||||
|
PreparedStatement st = DatabaseConnection.getConnection().prepareStatement(sql);
|
||||||
|
ResultSet rs = st.executeQuery();
|
||||||
|
while(rs.next()){
|
||||||
|
result.add(new MenuItem(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getString("name"),
|
||||||
|
rs.getString("description"),
|
||||||
|
rs.getDouble("price"),
|
||||||
|
rs.getString("category")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
st.close();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Failed to fetch menu " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
public MenuItem findById(int id) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT * FROM menu WHERE id=?";
|
||||||
// Find menu item by id
|
MenuItem itemMenu = null;
|
||||||
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
PreparedStatement st = DatabaseConnection.getConnection().prepareStatement(sql);
|
||||||
|
st.setInt(1, id);
|
||||||
|
ResultSet rs = st.executeQuery();
|
||||||
|
|
||||||
|
if(rs.next()){
|
||||||
|
itemMenu = new MenuItem(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getString("name"),
|
||||||
|
rs.getString("description"),
|
||||||
|
rs.getDouble("price"),
|
||||||
|
rs.getString("category")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
st.close();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Failed to fetch item " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return itemMenu;
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,25 +1,73 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.Order;
|
import dev.model.Order;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OrderDao {
|
public class OrderDao {
|
||||||
|
|
||||||
public int save(Order order) {
|
public int save(Order order) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "INSERT INTO orders(user_id, created_at, total_price) VALUES(?, ?, ?)";
|
||||||
// Insert order and return generated id
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
PreparedStatement st = DatabaseConnection.getConnection().prepareStatement(sql);
|
||||||
|
st.setInt(1, order.getUserId());
|
||||||
|
st.setTimestamp(2, Timestamp.valueOf(order.getCreatedAt()));
|
||||||
|
st.setDouble(3, order.getTotalPrice());
|
||||||
|
|
||||||
|
st.executeUpdate();
|
||||||
|
ResultSet rsKeys = st.getGeneratedKeys();
|
||||||
|
if(rsKeys.next()){
|
||||||
|
int orderId = rsKeys.getInt(1);
|
||||||
|
order.setId(orderId);
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
st.close();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Failed to add order " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Order> findByUserId(int userId) {
|
public List<Order> findByUserId(int userId) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT * FROM orders WHERE user_id=?";
|
||||||
// Retrieve all orders of a user
|
List<Order> result = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
PreparedStatement st = DatabaseConnection.getConnection().prepareStatement(sql);
|
||||||
|
st.setInt(1,userId);
|
||||||
|
|
||||||
|
ResultSet rs = st.executeQuery();
|
||||||
|
while (rs.next()){
|
||||||
|
result.add(new Order(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getInt("user_id"),
|
||||||
|
rs.getTimestamp("created_at").toLocalDateTime(),
|
||||||
|
rs.getInt("total_price")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
st.close();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Unable to find user: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,66 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.OrderDetail;
|
import dev.model.OrderDetail;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OrderDetailDao {
|
public class OrderDetailDao {
|
||||||
|
|
||||||
public void save(OrderDetail detail) {
|
public void save(OrderDetail detail) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "INSERT INTO order_details (order_id, menu_id, quantity, item_price) VALUES (?,?,?,?)";
|
||||||
// Insert order detail
|
|
||||||
|
try {
|
||||||
|
PreparedStatement st = DatabaseConnection.getConnection().prepareStatement(sql);
|
||||||
|
st.setInt(1, detail.getOrderId());
|
||||||
|
st.setInt(2, detail.getMenuItemId());
|
||||||
|
st.setInt(3, detail.getQuantity());
|
||||||
|
st.setDouble(4, detail.getPrice());
|
||||||
|
st.executeUpdate();
|
||||||
|
|
||||||
|
st.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error writing order details: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<OrderDetail> findByOrderId(int orderId) {
|
public List<OrderDetail> findByOrderId(int orderId) {
|
||||||
|
|
||||||
// TODO:
|
List<OrderDetail> result = new ArrayList<>();
|
||||||
// Retrieve order details
|
|
||||||
|
String sql = "SELECT * FROM order_details WHERE order_id=?";
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
PreparedStatement st = DatabaseConnection.getConnection().prepareStatement(sql);
|
||||||
|
st.setInt(1,orderId);
|
||||||
|
|
||||||
|
ResultSet rs = st.executeQuery();
|
||||||
|
while (rs.next()){
|
||||||
|
result.add(new OrderDetail(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getInt("order_id"),
|
||||||
|
rs.getInt("menu_id"),
|
||||||
|
rs.getInt("quantity"),
|
||||||
|
rs.getDouble("item_price")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
st.close();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error retrieving order details: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,63 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class UserDao {
|
public class UserDao {
|
||||||
|
|
||||||
public boolean save(User user) {
|
public boolean save(User user) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "INSERT INTO users (username, password_hash, email) VALUES (?, ?, ?)";
|
||||||
// Insert user into database
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
PreparedStatement st = DatabaseConnection.getConnection().prepareStatement(sql);
|
||||||
|
st.setString(1, user.getUsername());
|
||||||
|
st.setString(2, user.getPassword());
|
||||||
|
st.setString(3, user.getEmail());
|
||||||
|
st.executeUpdate();
|
||||||
|
st.close();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Failed to save user " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUsername(String username) {
|
public User findByUsername(String username) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT * FROM users WHERE username=?";
|
||||||
// Find a user by username
|
User result = null;
|
||||||
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
PreparedStatement st = DatabaseConnection.getConnection().prepareStatement(sql);
|
||||||
|
st.setString(1, username);
|
||||||
|
|
||||||
|
ResultSet rs = st.executeQuery();
|
||||||
|
if(rs.next()){
|
||||||
|
result = new User(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getString("username"),
|
||||||
|
rs.getString("password_hash"),
|
||||||
|
rs.getString("email")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
st.close();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Failed to fild user " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,16 @@
|
|||||||
package dev.database;
|
package dev.database;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class DatabaseConnection {
|
public class DatabaseConnection {
|
||||||
|
|
||||||
private static final String URL = "jdbc:postgresql://localhost:5432/restaurant_db"; // DB Server
|
private static final String URL = "jdbc:postgresql://localhost:5432/restaurant_db"; // DB Server
|
||||||
|
|
||||||
private static final String USER = "postgres"; // Your Username
|
private static final String USER = "matin"; // Your Username
|
||||||
|
|
||||||
private static final String PASSWORD = "password"; // Your Password
|
private static final String PASSWORD = "0000"; // Your Password
|
||||||
|
|
||||||
private DatabaseConnection() {
|
private DatabaseConnection() {
|
||||||
|
|
||||||
@@ -18,10 +19,8 @@ public class DatabaseConnection {
|
|||||||
public static Connection getConnection()
|
public static Connection getConnection()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
|
|
||||||
// TODO:
|
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||||
// Return a valid PostgreSQL connection
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -3,13 +3,47 @@ package dev.model;
|
|||||||
public class MenuItem {
|
public class MenuItem {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
private String category;
|
private String category;
|
||||||
|
|
||||||
|
public MenuItem(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 String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCategory() {
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MenuItem{" +
|
||||||
|
"id=" + id +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", description='" + description + '\'' +
|
||||||
|
", price=" + price +
|
||||||
|
", category='" + category + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,11 +5,62 @@ import java.time.LocalDateTime;
|
|||||||
public class Order {
|
public class Order {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private int userId;
|
private int userId;
|
||||||
|
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
private int totalPrice;
|
||||||
|
|
||||||
private double totalPrice;
|
public Order(int id, int totalPrice, LocalDateTime createdAt, int userId) {
|
||||||
|
this.id = id;
|
||||||
|
this.totalPrice = totalPrice;
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Order(int userId, LocalDateTime createdAt, int totalPrice) {
|
||||||
|
this.userId = userId;
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
this.totalPrice = totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTotalPrice() {
|
||||||
|
return totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalPrice(int totalPrice) {
|
||||||
|
this.totalPrice = totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getCreatedAt() {
|
||||||
|
return createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedAt(LocalDateTime createdAt) {
|
||||||
|
this.createdAt = createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(int userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Order{" +
|
||||||
|
"id=" + id +
|
||||||
|
", userId=" + userId +
|
||||||
|
", createdAt=" + createdAt +
|
||||||
|
", totalPrice=" + totalPrice +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,13 +3,74 @@ package dev.model;
|
|||||||
public class OrderDetail {
|
public class OrderDetail {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private int orderId;
|
private int orderId;
|
||||||
|
|
||||||
private int menuItemId;
|
private int menuItemId;
|
||||||
|
|
||||||
private int quantity;
|
private int quantity;
|
||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
|
public OrderDetail(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 orderId, int menuItemId, int quantity, double price) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
this.menuItemId = menuItemId;
|
||||||
|
this.quantity = quantity;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getQuantity() {
|
||||||
|
return quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQuantity(int quantity) {
|
||||||
|
this.quantity = quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMenuItemId() {
|
||||||
|
return menuItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMenuItemId(int menuItemId) {
|
||||||
|
this.menuItemId = menuItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderId(int orderId) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "OrderDetail{" +
|
||||||
|
"id=" + id +
|
||||||
|
", orderId=" + orderId +
|
||||||
|
", menuItemId=" + menuItemId +
|
||||||
|
", quantity=" + quantity +
|
||||||
|
", price=" + price +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,11 +3,42 @@ package dev.model;
|
|||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
// verify user for the first time
|
||||||
|
public User(int id, String username, String password, String email){
|
||||||
|
this.id = id;
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
// verified user
|
||||||
|
public User(String username, String password, String email){
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id){
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,40 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.UserDao;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class AuthService {
|
public class AuthService {
|
||||||
|
|
||||||
|
private final UserDao userDao = new UserDao();
|
||||||
|
|
||||||
public boolean register(String username, String password, String email) {
|
public boolean register(String username, String password, String email) {
|
||||||
|
|
||||||
// TODO:
|
User user = new User(
|
||||||
// Validate and register user
|
username,
|
||||||
|
String.valueOf(password.hashCode()),
|
||||||
|
email
|
||||||
|
);
|
||||||
|
|
||||||
|
if(userDao.save(user)){
|
||||||
|
System.out.println("Registered successfully");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public User login(String username, String password) {
|
public User login(String username, String password) {
|
||||||
|
|
||||||
// TODO:
|
User userDB = userDao.findByUsername(username);
|
||||||
// Authenticate user
|
|
||||||
|
if(userDB == null) return null;
|
||||||
|
|
||||||
|
String userPasswordDB = userDB.getPassword();
|
||||||
|
if( Objects.equals( userPasswordDB, String.valueOf(password.hashCode()) ) )
|
||||||
|
return userDB;
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,20 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.MenuItemDao;
|
||||||
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class MenuService {
|
public class MenuService {
|
||||||
|
|
||||||
|
public static final MenuItemDao menuItemDao = new MenuItemDao();
|
||||||
|
|
||||||
public void showMenu() {
|
public void showMenu() {
|
||||||
|
|
||||||
// TODO:
|
List<MenuItem> items = menuItemDao.findAll();
|
||||||
// Display menu items
|
for(MenuItem item : items){
|
||||||
|
System.out.println(item);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +1,141 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.OrderDao;
|
||||||
|
import dev.dao.OrderDetailDao;
|
||||||
|
import dev.model.MenuItem;
|
||||||
|
import dev.model.Order;
|
||||||
|
import dev.model.OrderDetail;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
public class OrderService {
|
public class OrderService {
|
||||||
|
|
||||||
|
private final static OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||||
|
private final static OrderDao orderDao = new OrderDao();
|
||||||
|
private final static MenuService menuService = new MenuService();
|
||||||
|
private final static Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
public void placeOrder(int userId) {
|
public void placeOrder(int userId) {
|
||||||
|
|
||||||
// TODO:
|
List<MenuItem> menuItems = MenuService.menuItemDao.findAll();
|
||||||
// Create order
|
|
||||||
|
|
||||||
|
if (menuItems.isEmpty()) {
|
||||||
|
System.out.println("No menu items available.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Display menu
|
||||||
|
System.out.println("========== MENU ==========");
|
||||||
|
for (MenuItem item : menuItems) {
|
||||||
|
System.out.printf("%-4d %-25s %10.2f%n",
|
||||||
|
item.getId(), item.getName(), item.getPrice());
|
||||||
|
}
|
||||||
|
System.out.println("===========================");
|
||||||
|
|
||||||
|
|
||||||
|
Order order = new Order(userId, LocalDateTime.now(), 0);
|
||||||
|
int orderId = orderDao.save(order);
|
||||||
|
|
||||||
|
double totalPrice = 0;
|
||||||
|
boolean addingItems = true;
|
||||||
|
|
||||||
|
while (addingItems) {
|
||||||
|
|
||||||
|
System.out.print("Enter menu item id to add (0 to finish): ");
|
||||||
|
int menuId = Integer.parseInt(scanner.nextLine().trim());
|
||||||
|
|
||||||
|
if (menuId == 0) {
|
||||||
|
addingItems = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
MenuItem selected = MenuService.menuItemDao.findById(menuId);
|
||||||
|
|
||||||
|
if (selected == null) {
|
||||||
|
System.out.println("Invalid item id, try again.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.print("Enter quantity: ");
|
||||||
|
int quantity = Integer.parseInt(scanner.nextLine().trim());
|
||||||
|
|
||||||
|
if (quantity <= 0) {
|
||||||
|
System.out.println("Quantity must be positive.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
OrderDetail detail = new OrderDetail(
|
||||||
|
orderId,
|
||||||
|
selected.getId(),
|
||||||
|
quantity,
|
||||||
|
selected.getPrice()
|
||||||
|
);
|
||||||
|
|
||||||
|
orderDetailDao.save(detail);
|
||||||
|
|
||||||
|
totalPrice += selected.getPrice() * quantity;
|
||||||
|
|
||||||
|
System.out.println(selected.getName() + " x" + quantity + " added.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (totalPrice == 0) {
|
||||||
|
System.out.println("No items were added. Order canceled.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
order.setId(orderId);
|
||||||
|
order.setTotalPrice((int) totalPrice);
|
||||||
|
orderDao.save(order);
|
||||||
|
|
||||||
|
System.out.println("Order placed successfully. Order ID: " + orderId);
|
||||||
|
printReceipt(orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printReceipt(int orderId) {
|
public void printReceipt(int orderId) {
|
||||||
|
|
||||||
// TODO:
|
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||||
// Print order receipt
|
|
||||||
|
if(details.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
System.out.println("Order ID: " + orderId);
|
||||||
|
double total = 0;
|
||||||
|
for (OrderDetail detail : details) {
|
||||||
|
double subtotal = detail.getPrice() * detail.getQuantity();
|
||||||
|
total += subtotal;
|
||||||
|
System.out.printf("%-20s %dx $%.2f%n",
|
||||||
|
MenuService.menuItemDao.findById(detail.getMenuItemId()).getName(),
|
||||||
|
detail.getQuantity(),
|
||||||
|
subtotal
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("---------------------------");
|
||||||
|
System.out.printf("Total: $%.2f%n", total);
|
||||||
|
System.out.println("===========================\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showOrderHistory(int userId) {
|
public void showOrderHistory(int userId) {
|
||||||
|
|
||||||
// TODO:
|
List<Order> orders = orderDao.findByUserId(userId);
|
||||||
// Display user's order history
|
|
||||||
|
if (orders.isEmpty()){
|
||||||
|
System.err.println("No orders found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Order order : orders){
|
||||||
|
System.out.println("Order ID: " + order.getId());
|
||||||
|
System.out.println("Date: " + order.getCreatedAt());
|
||||||
|
System.out.printf("Total: $%.2f%n", order.getTotalPrice());
|
||||||
|
System.out.println("---------------------------------");
|
||||||
|
printReceipt(order.getId());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
package dev.ui;
|
package dev.ui;
|
||||||
|
|
||||||
|
import dev.model.User;
|
||||||
|
import dev.service.AuthService;
|
||||||
|
import dev.service.MenuService;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ConsoleMenu {
|
public class ConsoleMenu {
|
||||||
|
|
||||||
private final Scanner scanner =
|
private final Scanner scanner = new Scanner(System.in);
|
||||||
new Scanner(System.in);
|
private final AuthService authService = new AuthService();
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
|
|
||||||
@@ -21,13 +25,42 @@ public class ConsoleMenu {
|
|||||||
|
|
||||||
switch (choice) {
|
switch (choice) {
|
||||||
|
|
||||||
case 1:
|
case 1: {
|
||||||
// TODO
|
System.out.println("Enter username: ");
|
||||||
break;
|
String username = scanner.next();
|
||||||
|
System.out.println("Enter password: ");
|
||||||
|
String password = scanner.next();
|
||||||
|
|
||||||
case 2:
|
User user = authService.login(username, password);
|
||||||
// TODO
|
|
||||||
|
if (user == null) {
|
||||||
|
System.err.println("Unable to login");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("welcome! " + user.getUsername());
|
||||||
|
MenuService menuService = new MenuService();
|
||||||
|
menuService.showMenu();
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 2: {
|
||||||
|
System.out.println("Enter username: ");
|
||||||
|
String username = scanner.nextLine();
|
||||||
|
System.out.println("Enter password: ");
|
||||||
|
String password = scanner.nextLine();
|
||||||
|
System.out.println("Enter email: ");
|
||||||
|
String email = scanner.nextLine();
|
||||||
|
|
||||||
|
if (!authService.register(username, password, email)) {
|
||||||
|
System.err.println("Registration failed.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Registered successfully.");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user