Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15737c361b | ||
|
|
df8d211b22 |
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
+9
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$APPLICATION_HOME_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$APPLICATION_HOME_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
<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="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
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
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/WS-10-Database.iml" filepath="$PROJECT_DIR$/WS-10-Database.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/main/java" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -1,25 +1,74 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.MenuItem;
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MenuItemDao {
|
public class MenuItemDao {
|
||||||
|
|
||||||
public List<MenuItem> findAll() {
|
public List<MenuItem> findAll()
|
||||||
|
{
|
||||||
|
List<MenuItem> items = new ArrayList<>();
|
||||||
|
String sql = "SELECT * FROM menu_items";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Retrieve all menu items
|
PreparedStatement ps = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
ResultSet rs = ps.executeQuery();
|
||||||
|
|
||||||
return null;
|
while (rs.next())
|
||||||
|
{
|
||||||
|
MenuItem menuItem = new MenuItem();
|
||||||
|
|
||||||
|
menuItem.setId(rs.getInt("id"));
|
||||||
|
menuItem.setName(rs.getString("name"));
|
||||||
|
menuItem.setDescription(rs.getString("description"));
|
||||||
|
menuItem.setPrice(rs.getDouble("price"));
|
||||||
|
menuItem.setCategory(rs.getString("category"));
|
||||||
|
|
||||||
|
items.add(menuItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
public MenuItem findById(int id) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT * FROM menu_items WHERE id=?";
|
||||||
// Find menu item by id
|
|
||||||
|
|
||||||
|
try(Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement ps = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
ps.setInt(1,id);
|
||||||
|
ResultSet rs = ps.executeQuery();
|
||||||
|
|
||||||
|
if(rs.next())
|
||||||
|
{
|
||||||
|
MenuItem item = new MenuItem();
|
||||||
|
|
||||||
|
item.setId(rs.getInt("id"));
|
||||||
|
item.setName(rs.getString("name"));
|
||||||
|
item.setDescription(rs.getString("description"));
|
||||||
|
item.setPrice(rs.getDouble("price"));
|
||||||
|
item.setCategory(rs.getString("category"));
|
||||||
|
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,25 +1,63 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.Order;
|
import dev.model.Order;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
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,total_price) VALUES(?,?)";
|
||||||
// Insert order and return generated id
|
|
||||||
|
|
||||||
|
try(Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement ps = conn.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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Order> findByUserId(int userId) {
|
public List<Order> findByUserId(int userId) {
|
||||||
|
|
||||||
// TODO:
|
List<Order> orders = new ArrayList<>();
|
||||||
// Retrieve all orders of a user
|
String sql = "SELECT * FROM orders WHERE user_id=?";
|
||||||
|
|
||||||
return null;
|
try(Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement ps = conn.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"));
|
||||||
|
order.setCreatedAt(rs.getTimestamp("created_at").toLocalDateTime());
|
||||||
|
|
||||||
|
orders.add(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return orders;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,62 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.OrderDetail;
|
import dev.model.OrderDetail;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
String sql = "INSERT INTO order_details(order_id,menu_item_id,quantity,price) VALUES(?,?,?,?)";
|
||||||
|
|
||||||
// TODO:
|
try(Connection conn = DatabaseConnection.getConnection();
|
||||||
// Insert order detail
|
PreparedStatement ps = conn.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) {
|
public List<OrderDetail> findByOrderId(int orderId)
|
||||||
|
{
|
||||||
|
List<OrderDetail> details = new ArrayList<>();
|
||||||
|
String sql = "SELECT * FROM order_details WHERE order_id=?";
|
||||||
|
|
||||||
// TODO:
|
try(Connection conn = DatabaseConnection.getConnection();
|
||||||
// Retrieve order details
|
PreparedStatement ps = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
ps.setInt(1,orderId);
|
||||||
|
ResultSet rs = ps.executeQuery();
|
||||||
|
|
||||||
return null;
|
while(rs.next())
|
||||||
|
{
|
||||||
|
OrderDetail detail = new OrderDetail();
|
||||||
|
|
||||||
|
detail.setOrderId(rs.getInt("order_id"));
|
||||||
|
detail.setMenuItemId(rs.getInt("menu_item_id"));
|
||||||
|
detail.setQuantity(rs.getInt("quantity"));
|
||||||
|
detail.setPrice(rs.getDouble("price"));
|
||||||
|
|
||||||
|
details.add(detail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return details;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,61 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
|
import javax.xml.transform.Result;
|
||||||
|
import java.sql.Connection;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
String sql = "INSERT INTO users(username,password,email) VALUES(?,?,?)";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Insert user into database
|
PreparedStatement ps = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
ps.setString(1, user.getUsername());
|
||||||
|
ps.setString(2, user.getPassword());
|
||||||
|
ps.setString(3, user.getEmail());
|
||||||
|
|
||||||
|
return ps.executeUpdate() > 0;
|
||||||
|
}
|
||||||
|
catch (SQLException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUsername(String username) {
|
public User findByUsername(String username)
|
||||||
|
{
|
||||||
|
String sql = "SELECT * FROM users WHERE username=?";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Find a user by username
|
PreparedStatement ps = conn.prepareStatement(sql))
|
||||||
|
{
|
||||||
|
ps.setString(1, username);
|
||||||
|
ResultSet rs = ps.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next())
|
||||||
|
{
|
||||||
|
User user = new User();
|
||||||
|
user.setId(rs.getInt("id"));
|
||||||
|
user.setUsername(rs.getString("username"));
|
||||||
|
user.setPassword(rs.getString("email"));
|
||||||
|
user.setPassword(rs.getString("password"));
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SQLException e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
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 {
|
||||||
@@ -9,19 +10,14 @@ public class DatabaseConnection {
|
|||||||
|
|
||||||
private static final String USER = "postgres"; // Your Username
|
private static final String USER = "postgres"; // Your Username
|
||||||
|
|
||||||
private static final String PASSWORD = "password"; // Your Password
|
private static final String PASSWORD = "bita3773"; // Your Password
|
||||||
|
|
||||||
private DatabaseConnection() {
|
private DatabaseConnection() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Connection getConnection()
|
public static Connection getConnection() throws SQLException
|
||||||
throws SQLException {
|
{
|
||||||
|
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||||
// TODO:
|
|
||||||
// Return a valid PostgreSQL connection
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,43 @@ public class MenuItem {
|
|||||||
|
|
||||||
private String category;
|
private String 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,29 @@ public class Order {
|
|||||||
|
|
||||||
private double totalPrice;
|
private double 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getTotalPrice() {
|
||||||
|
return totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalPrice(double totalPrice) {
|
||||||
|
this.totalPrice = totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() { return id; }
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,6 @@ package dev.model;
|
|||||||
|
|
||||||
public class OrderDetail {
|
public class OrderDetail {
|
||||||
|
|
||||||
private int id;
|
|
||||||
|
|
||||||
private int orderId;
|
private int orderId;
|
||||||
|
|
||||||
private int menuItemId;
|
private int menuItemId;
|
||||||
@@ -12,4 +10,19 @@ public class OrderDetail {
|
|||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
|
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; }
|
||||||
}
|
}
|
||||||
@@ -10,4 +10,19 @@ public class User {
|
|||||||
|
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
|
public String getUsername() { return username; }
|
||||||
|
|
||||||
|
public void setUsername(String username) { this.username = username; }
|
||||||
|
|
||||||
|
public int getId() { return id; }
|
||||||
|
|
||||||
|
public void setId(int id) { this.id = id; }
|
||||||
|
|
||||||
|
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,38 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
|
import dev.dao.UserDao;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
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)
|
||||||
|
{
|
||||||
|
if (userDao.findByUsername(username) != null)
|
||||||
|
{
|
||||||
|
System.out.println("username exists");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO:
|
password = Integer.toHexString(password.hashCode());
|
||||||
// Validate and register user
|
User user = new User();
|
||||||
|
user.setUsername(username);
|
||||||
return false;
|
user.setPassword(password);
|
||||||
|
user.setEmail(email);
|
||||||
|
return userDao.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User login(String username, String password) {
|
public User login(String username, String password)
|
||||||
|
{
|
||||||
|
User user = userDao.findByUsername(username);
|
||||||
|
|
||||||
// TODO:
|
if (user == null) return null;
|
||||||
// Authenticate user
|
|
||||||
|
String hash = Integer.toHexString(password.hashCode());
|
||||||
|
|
||||||
|
if(user.getPassword().equals(hash)) return user;
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,23 @@
|
|||||||
package dev.service;
|
package dev.service;
|
||||||
|
|
||||||
public class MenuService {
|
import dev.dao.MenuItemDao;
|
||||||
|
import dev.model.MenuItem;
|
||||||
public void showMenu() {
|
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Display menu items
|
|
||||||
|
|
||||||
|
public class MenuService
|
||||||
|
{
|
||||||
|
private final MenuItemDao dao = new MenuItemDao();
|
||||||
|
public void showMenu()
|
||||||
|
{
|
||||||
|
System.out.println("\n===================================================================");
|
||||||
|
System.out.println("id name description price category");
|
||||||
|
System.out.println("===================================================================");
|
||||||
|
for(MenuItem item : dao.findAll())
|
||||||
|
{
|
||||||
|
System.out.println(item.getId() + ") "
|
||||||
|
+ item.getName() + " - "
|
||||||
|
+ item.getDescription() + " - $" +item.getPrice()
|
||||||
|
+ " - " + item.getCategory());
|
||||||
|
System.out.println("-------------------------------------------------------------------");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,26 +1,133 @@
|
|||||||
package dev.service;
|
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 class OrderService {
|
||||||
|
|
||||||
public void placeOrder(int userId) {
|
private final Scanner scanner = new Scanner(System.in);
|
||||||
|
private final MenuItemDao menuDao = new MenuItemDao();
|
||||||
|
private final OrderDao orderDao = new OrderDao();
|
||||||
|
private final OrderDetailDao detailDao = new OrderDetailDao();
|
||||||
|
|
||||||
// TODO:
|
public void placeOrder(int userId)
|
||||||
// Create order
|
{
|
||||||
|
List<OrderDetail> details = new ArrayList<>();
|
||||||
|
|
||||||
|
double totalPrice = 0;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
System.out.println("\nEnter menu item id (0 to 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("Quantity: ");
|
||||||
|
|
||||||
|
int quantity = scanner.nextInt();
|
||||||
|
|
||||||
|
OrderDetail detail = new OrderDetail();
|
||||||
|
|
||||||
|
detail.setMenuItemId(itemId);
|
||||||
|
detail.setQuantity(quantity);
|
||||||
|
detail.setPrice(item.getPrice());
|
||||||
|
|
||||||
|
totalPrice += item.getPrice() * quantity;
|
||||||
|
|
||||||
|
details.add(detail);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (details.isEmpty())
|
||||||
|
{
|
||||||
|
System.out.println("No items selected.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Order order = new Order();
|
||||||
|
|
||||||
|
order.setUserId(userId);
|
||||||
|
order.setTotalPrice(totalPrice);
|
||||||
|
|
||||||
|
int orderId = orderDao.save(order);
|
||||||
|
|
||||||
|
for (OrderDetail detail : details)
|
||||||
|
{
|
||||||
|
detail.setOrderId(orderId);
|
||||||
|
|
||||||
|
detailDao.save(detail);
|
||||||
|
}
|
||||||
|
|
||||||
|
printReceipt(orderId);
|
||||||
|
System.out.println("Order saved successfully.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printReceipt(int orderId) {
|
public void printReceipt(int orderId)
|
||||||
|
{
|
||||||
|
List<OrderDetail> details = detailDao.findByOrderId(orderId);
|
||||||
|
|
||||||
// TODO:
|
System.out.println("\n[Order Summary / Receipt]");
|
||||||
// Print order receipt
|
System.out.println("----------------------------------------------------");
|
||||||
|
System.out.printf("%-15s %-8s %-10s %-10s%n",
|
||||||
|
"Item", "Qty", "Unit", "Total");
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
|
||||||
|
double finalTotal = 0;
|
||||||
|
|
||||||
|
for(OrderDetail detail : details)
|
||||||
|
{
|
||||||
|
MenuItem item = menuDao.findById(detail.getMenuItemId());
|
||||||
|
|
||||||
|
double subtotal = detail.getPrice() * detail.getQuantity();
|
||||||
|
|
||||||
|
finalTotal += subtotal;
|
||||||
|
|
||||||
|
System.out.printf("%-15s %-8d $%-9.2f $%-9.2f%n",
|
||||||
|
item.getName(),
|
||||||
|
detail.getQuantity(),
|
||||||
|
detail.getPrice(),
|
||||||
|
subtotal);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("----------------------------------------------------");
|
||||||
|
System.out.printf("Final Total: $%.2f%n", finalTotal);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showOrderHistory(int userId) {
|
public void showOrderHistory(int userId)
|
||||||
|
{
|
||||||
|
List<Order> orders = orderDao.findByUserId(userId);
|
||||||
|
|
||||||
// TODO:
|
if (orders.isEmpty())
|
||||||
// Display user's order history
|
{
|
||||||
|
System.out.println("No orders found.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n===== ORDER HISTORY =====");
|
||||||
|
|
||||||
|
for (Order order : orders)
|
||||||
|
{
|
||||||
|
System.out.println("Order ID: " + order.getId()
|
||||||
|
+ " | Total: $" + order.getTotalPrice()
|
||||||
|
+ " | Date: " + order.getCreatedAt()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,44 +1,135 @@
|
|||||||
package dev.ui;
|
package dev.ui;
|
||||||
|
|
||||||
|
import dev.model.User;
|
||||||
|
import dev.service.AuthService;
|
||||||
|
import dev.service.MenuService;
|
||||||
|
import dev.service.OrderService;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ConsoleMenu {
|
public class ConsoleMenu
|
||||||
|
{
|
||||||
|
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 final Scanner scanner =
|
private int userId;
|
||||||
new Scanner(System.in);
|
|
||||||
|
|
||||||
public void start() {
|
public void start()
|
||||||
|
{
|
||||||
while (true) {
|
while (true)
|
||||||
|
{
|
||||||
System.out.println();
|
System.out.println("\n===== JAVA PIZZERIA =====");
|
||||||
System.out.println("===== JAVA PIZZERIA =====");
|
|
||||||
System.out.println("1. Login");
|
System.out.println("1. Login");
|
||||||
System.out.println("2. Register");
|
System.out.println("2. Register");
|
||||||
System.out.println("3. Exit");
|
System.out.println("3. Exit");
|
||||||
|
|
||||||
int choice = scanner.nextInt();
|
int choice = getIntInput();
|
||||||
|
|
||||||
switch (choice) {
|
|
||||||
|
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
case 1:
|
case 1:
|
||||||
// TODO
|
login();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
// TODO
|
register();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
|
System.out.println("Goodbye!");
|
||||||
return;
|
return;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
System.out.println("Invalid choice");
|
System.out.println("Invalid choice.");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void login()
|
||||||
|
{
|
||||||
|
System.out.println("\n[Login]");
|
||||||
|
|
||||||
|
System.out.print("Enter Username: ");
|
||||||
|
String username = scanner.next();
|
||||||
|
|
||||||
|
System.out.print("Enter Password: ");
|
||||||
|
String password = scanner.next();
|
||||||
|
|
||||||
|
User user = authService.login(username, password);
|
||||||
|
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
userId = user.getId();
|
||||||
|
System.out.println("Welcome " + user.getUsername());
|
||||||
|
showMainMenu();
|
||||||
|
}
|
||||||
|
else System.out.println("Invalid credentials.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void register()
|
||||||
|
{
|
||||||
|
System.out.print("Username: ");
|
||||||
|
String username = scanner.next();
|
||||||
|
|
||||||
|
System.out.print("Email: ");
|
||||||
|
String email = scanner.next();
|
||||||
|
|
||||||
|
System.out.print("Password: ");
|
||||||
|
String password = scanner.next();
|
||||||
|
|
||||||
|
if (authService.register(username, password, email)) System.out.println("Registred successfully!");
|
||||||
|
else System.out.println("Registration failed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showMainMenu()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
System.out.println("\n=======================================");
|
||||||
|
System.out.println(" 🍽️ MAIN MENU 🍽️");
|
||||||
|
System.out.println("=======================================");
|
||||||
|
System.out.println("1. View Menu");
|
||||||
|
System.out.println("2. Place a New Order");
|
||||||
|
System.out.println("3. View Order History");
|
||||||
|
System.out.println("4. Logout");
|
||||||
|
System.out.println("=======================================");
|
||||||
|
System.out.println("Choose an option: ");
|
||||||
|
|
||||||
|
int choice = getIntInput();
|
||||||
|
|
||||||
|
switch (choice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
menuService.showMenu();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
menuService.showMenu();
|
||||||
|
orderService.placeOrder(userId);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
orderService.showOrderHistory(userId);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
return;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Invalid choice.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getIntInput()
|
||||||
|
{
|
||||||
|
while (!scanner.hasNextInt())
|
||||||
|
{
|
||||||
|
System.out.println("Please enter a number:");
|
||||||
|
scanner.next();
|
||||||
|
}
|
||||||
|
return scanner.nextInt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user