order & report
This commit is contained in:
Generated
+12
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="postgres@localhost" uuid="cefa0168-48c2-4c70-8fa7-1ab9ac6efe72">
|
||||
<driver-ref>postgresql</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:postgresql://localhost:5432/postgres</jdbc-url>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="file://$PROJECT_DIR$/database.sql" dialect="GenericSQL" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -52,7 +52,7 @@ public class MenuItemDao {
|
||||
if(rst.next()){
|
||||
MenuItem item = new MenuItem(rst.getInt("id"),
|
||||
rst.getString("name"),
|
||||
rst.getString("descrition"),
|
||||
rst.getString("description"),
|
||||
rst.getDouble("price"),
|
||||
rst.getString("category"));
|
||||
|
||||
|
||||
@@ -1,25 +1,86 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDao {
|
||||
|
||||
public int save(Order order) {
|
||||
|
||||
// TODO:
|
||||
// Insert order and return generated id
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String query = "INSERT INTO customer_orders (user_id, time, total_price) VALUES (?, ?, ?)";
|
||||
|
||||
try(Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement pst = c.prepareStatement(query , Statement.RETURN_GENERATED_KEYS)){
|
||||
|
||||
pst.setInt(1 , order.getUserId());
|
||||
pst.setTimestamp(2 , Timestamp.valueOf(now));
|
||||
pst.setDouble(3 , order.getTotalPrice());
|
||||
|
||||
int i = pst.executeUpdate();
|
||||
if(i==1) System.out.println("Order saved");
|
||||
|
||||
try (ResultSet generatedKeys = pst.getGeneratedKeys()) {
|
||||
if (generatedKeys.next()) {
|
||||
int generatedId = generatedKeys.getInt(1);
|
||||
System.out.println("Order saved with ID: " + generatedId);
|
||||
return generatedId;
|
||||
} else {
|
||||
System.out.println("Creating order failed, no ID obtained.");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
String quary = "SELECT * FROM customer_orders WHERE user_id = ?";
|
||||
List<Order> result = new ArrayList<>();
|
||||
|
||||
|
||||
try (Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement pst = c.prepareStatement(quary)){
|
||||
|
||||
pst.setInt(1 , userId);
|
||||
|
||||
try (ResultSet rst = pst.executeQuery()){
|
||||
while (rst.next()){
|
||||
Order order = new Order(rst.getInt("id"),
|
||||
rst.getInt("user_id") ,
|
||||
rst.getTimestamp("time").toLocalDateTime(),
|
||||
rst.getDouble("total_price"));
|
||||
|
||||
result.add(order);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +1,71 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDetailDao {
|
||||
|
||||
public void save(OrderDetail detail) {
|
||||
|
||||
// TODO:
|
||||
// Insert order detail
|
||||
String quary = "INSERT INTO order_details (order_id, menu_item_id ,quantity , unit_price) VALUES (?, ?, ? ,?)";
|
||||
try (Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement prst = c.prepareStatement(quary)){
|
||||
prst.setInt(1, detail.getOrderId());
|
||||
prst.setInt(2, detail.getMenuItemId());
|
||||
prst.setInt(3, detail.getQuantity());
|
||||
prst.setDouble(4 , detail.getPrice());
|
||||
|
||||
int i = prst.executeUpdate();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
List<OrderDetail> result = new ArrayList<>();
|
||||
|
||||
String query = "Select * From order_details WHERE order_id = ?";
|
||||
|
||||
try(Connection c = DatabaseConnection.getConnection();
|
||||
PreparedStatement pst = c.prepareStatement(query)) {
|
||||
|
||||
pst.setInt(1 , orderId);
|
||||
|
||||
try(ResultSet rst = pst.executeQuery()){
|
||||
while (rst.next()){
|
||||
OrderDetail orderDetail = new OrderDetail(rst.getInt("id"),
|
||||
rst.getInt("order_id"),
|
||||
rst.getInt("menu_item_id"),
|
||||
rst.getInt("quantity"),
|
||||
rst.getDouble("unit_price"));
|
||||
result.add(orderDetail);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}catch (SQLException e){
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -5,11 +5,38 @@ import java.time.LocalDateTime;
|
||||
public class Order {
|
||||
|
||||
private int id;
|
||||
|
||||
private int userId;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private double totalPrice;
|
||||
|
||||
|
||||
public Order(int userId , LocalDateTime createdAt , double totalPrice){
|
||||
this.userId = userId;
|
||||
this.createdAt= createdAt;
|
||||
this.totalPrice =totalPrice;
|
||||
}
|
||||
|
||||
public Order(int id ,int userId , LocalDateTime createdAt , double totalPrice){
|
||||
this.id= id;
|
||||
this.userId = userId;
|
||||
this.createdAt= createdAt;
|
||||
this.totalPrice =totalPrice;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public double getTotalPrice() {
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
public String getCreatedAt() {
|
||||
return createdAt.toString();
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,13 +3,44 @@ package dev.model;
|
||||
public class OrderDetail {
|
||||
|
||||
private int id;
|
||||
|
||||
private int orderId;
|
||||
|
||||
private int menuItemId;
|
||||
|
||||
private int quantity;
|
||||
|
||||
private double price;
|
||||
|
||||
public OrderDetail(int orderId , int menuItemId , int quantity , double price){
|
||||
this.orderId=orderId;
|
||||
this.menuItemId=menuItemId;
|
||||
this.quantity=quantity;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public OrderDetail(int id , int orderId , int menuItemId , int quantity , double price){
|
||||
this.id =id;
|
||||
this.orderId=orderId;
|
||||
this.menuItemId=menuItemId;
|
||||
this.quantity=quantity;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public int getMenuItemId() {
|
||||
return menuItemId;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package dev.model;
|
||||
|
||||
public class Resipt {
|
||||
|
||||
private MenuItem menuItem;
|
||||
private int quantity;
|
||||
|
||||
|
||||
public Resipt(MenuItem menuItem , int quantity){
|
||||
this.menuItem=menuItem;
|
||||
this.quantity=quantity;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
public MenuItem getMenuItem() {
|
||||
return menuItem;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,116 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.dao.OrderDao;
|
||||
import dev.dao.OrderDetailDao;
|
||||
import dev.model.MenuItem;
|
||||
import dev.model.Order;
|
||||
import dev.model.OrderDetail;
|
||||
import dev.model.Resipt;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class OrderService {
|
||||
|
||||
private final Scanner scanner = new Scanner(System.in);
|
||||
private final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
private final OrderDao orderDao = new OrderDao();
|
||||
private final OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
List<Resipt> resipts = new ArrayList<>();
|
||||
double totalPrice= 0.0;
|
||||
|
||||
while (true){
|
||||
System.out.println("Enter the ID of the item to add (or 0 to finish):");
|
||||
int chooseId = scanner.nextInt();
|
||||
if (chooseId == 0) {
|
||||
break;
|
||||
}
|
||||
MenuItem chosen = menuItemDao.findById(chooseId);
|
||||
if (chosen == null) {
|
||||
System.out.println("❌ Invalid food! Please try again.");
|
||||
continue;
|
||||
}
|
||||
System.out.println("Enter quantity:");
|
||||
int quantity = scanner.nextInt();
|
||||
|
||||
if (quantity <= 0) {
|
||||
System.out.println("❌ Quantity must be greater than 0.");
|
||||
continue;
|
||||
}
|
||||
|
||||
resipts.add(new Resipt(chosen , quantity));
|
||||
totalPrice += chosen.getPrice() * quantity;
|
||||
System.out.println("ADD " + quantity + " " + chosen.getName());
|
||||
|
||||
}
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
Order order = new Order(userId , now , totalPrice);
|
||||
int orderId = orderDao.save(order);
|
||||
|
||||
System.out.println("orderId: "+ orderId);
|
||||
for (int i = 0; i < resipts.size() ; i++){
|
||||
OrderDetail orderDetail = new OrderDetail(orderId ,
|
||||
resipts.get(i).getMenuItem().getId()
|
||||
|
||||
,resipts.get(i).getQuantity()
|
||||
,resipts.get(i).getMenuItem().getPrice());
|
||||
|
||||
orderDetailDao.save(orderDetail);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
printReceipt(orderId);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
System.out.println("[Order Summary / Receipt]");
|
||||
System.out.println("--------------------------------------------------");
|
||||
System.out.println(" Item | Quantity | Unit | Total");
|
||||
System.out.println("--------------------------------------------------");
|
||||
|
||||
double totalprice = 0.0;
|
||||
List<OrderDetail> resipt = orderDetailDao.findByOrderId(orderId);
|
||||
for(int i = 0 ; i< resipt.size() ; i++){
|
||||
MenuItem menuItem = menuItemDao.findById(resipt.get(i).getMenuItemId());
|
||||
System.out.printf("%-20s | %-10d | %-10.2f | %-10.2f" ,menuItem.getName()
|
||||
,resipt.get(i).getQuantity() , menuItem.getPrice() , resipt.get(i).getQuantity()* menuItem.getPrice());
|
||||
|
||||
System.out.println();
|
||||
totalprice += resipt.get(i).getQuantity()* menuItem.getPrice();
|
||||
System.out.println("--------------------------------------------------");
|
||||
}
|
||||
System.out.println("Final price: " + totalprice );
|
||||
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
List<Order> orders = orderDao.findByUserId(userId);
|
||||
|
||||
System.out.printf("%-10s | %-30s | %-10s%n", "Order Id", "Date & Time", "Total Price");
|
||||
System.out.println("------------------------------------------------------------");
|
||||
for (Order o : orders){
|
||||
System.out.printf("%-10d | %-30s | $%-9.2f%n",
|
||||
o.getId(),
|
||||
o.getCreatedAt().toString(),
|
||||
o.getTotalPrice());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package dev.ui;
|
||||
import dev.model.User;
|
||||
import dev.service.AuthService;
|
||||
import dev.service.MenuService;
|
||||
import dev.service.OrderService;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -11,6 +12,7 @@ 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 User user;
|
||||
|
||||
|
||||
@@ -93,22 +95,26 @@ public class ConsoleMenu {
|
||||
case 1:
|
||||
{
|
||||
menuService.showMenu();
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
orderService.placeOrder(user.getId());
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
orderService.showOrderHistory(user.getId());
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
case 4:
|
||||
{
|
||||
return;
|
||||
}
|
||||
default:{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user