finish
This commit is contained in:
@@ -59,7 +59,7 @@ public class OrderDao {
|
||||
Order order = new Order();
|
||||
order.setId(rs.getInt("id"));
|
||||
order.setUserId(rs.getInt("user_id"));
|
||||
order.setCreatedAt(rs.getTimestamp("order_date").toLocalDateTime());
|
||||
order.setCreatedAt(rs.getTimestamp("orderDate").toLocalDateTime());
|
||||
order.setTotalPrice(rs.getDouble("total_price"));
|
||||
orders.add(order);
|
||||
}
|
||||
|
||||
@@ -14,20 +14,16 @@ public class OrderDetailDao {
|
||||
|
||||
public void save(OrderDetail detail) {
|
||||
|
||||
String sql = "INSERT INTO order_details (order_id, menu_items_id, quantity, price_at_purchase) VALUES (?,?,?,?)";
|
||||
String sql = "INSERT INTO order_details (order_id, menu_items_id, quantity, price_at_purchase) VALUES (? ,? ,? ,?)";
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||
{
|
||||
stmt.setInt(2, detail.getOrderId());
|
||||
stmt.setInt(3, detail.getMenuItemId());
|
||||
stmt.setInt(4, detail.getQuantity());
|
||||
stmt.setDouble(5, detail.getPrice());
|
||||
stmt.setInt(1, detail.getOrderId());
|
||||
stmt.setInt(2, detail.getMenuItemId());
|
||||
stmt.setInt(3, detail.getQuantity());
|
||||
stmt.setDouble(4, detail.getPrice());
|
||||
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
if (affectedRows == 0)
|
||||
{
|
||||
throw new SQLException("unsuccessful");
|
||||
}
|
||||
stmt.executeUpdate();
|
||||
|
||||
|
||||
} catch (SQLException e)
|
||||
@@ -54,7 +50,7 @@ public class OrderDetailDao {
|
||||
OrderDetail orderDetail = new OrderDetail();
|
||||
orderDetail.setId(rs.getInt("id"));
|
||||
orderDetail.setOrderId(rs.getInt("order_id"));
|
||||
orderDetail.setMenuItemId(rs.getInt("menu_item_id"));
|
||||
orderDetail.setMenuItemId(rs.getInt("menu_items_id"));
|
||||
orderDetail.setQuantity(rs.getInt("quantity"));
|
||||
orderDetail.setPrice(rs.getDouble("price_at_purchase"));
|
||||
|
||||
|
||||
@@ -1,23 +1,106 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.UserDao;
|
||||
import dev.model.User;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class AuthService {
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
public AuthService() {
|
||||
this.userDao = new UserDao();
|
||||
}
|
||||
|
||||
public boolean register(String username, String password, String email) {
|
||||
|
||||
// TODO:
|
||||
// Validate and register user
|
||||
if (username == null || username.trim().isEmpty())
|
||||
{
|
||||
System.err.println("username cannot be empty.");
|
||||
return false;
|
||||
}
|
||||
if (password == null || password.trim().isEmpty())
|
||||
{
|
||||
System.err.println("password cannot be empty.");
|
||||
return false;
|
||||
}
|
||||
User existingUser = userDao.findByUsername(username);
|
||||
if (existingUser != null)
|
||||
{
|
||||
System.err.println("a user with this username is already registered");
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
String hashedPassword = hashPassword(password);
|
||||
if (hashedPassword == null)
|
||||
{
|
||||
System.err.println("error");
|
||||
return false;
|
||||
}
|
||||
|
||||
User newUser = new User();
|
||||
newUser.setUsername(username.trim());
|
||||
newUser.setPassword(hashedPassword);
|
||||
newUser.setEmail(email != null ? email.trim() : null);
|
||||
|
||||
return userDao.save(newUser);
|
||||
}
|
||||
|
||||
public User login(String username, String password) {
|
||||
|
||||
// TODO:
|
||||
// Authenticate user
|
||||
if (username == null || username.trim().isEmpty())
|
||||
{
|
||||
System.err.println("username cannot be empty.");
|
||||
return null;
|
||||
}
|
||||
if (password == null || password.trim().isEmpty())
|
||||
{
|
||||
System.err.println("password cannot be empty.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
User user = userDao.findByUsername(username.trim());
|
||||
if (user == null)
|
||||
{
|
||||
System.err.println("not found");
|
||||
return null;
|
||||
}
|
||||
String hashedPassword = hashPassword(password);
|
||||
if (hashedPassword == null)
|
||||
{
|
||||
System.err.println("error");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (hashedPassword.equals(user.getPassword()))
|
||||
{
|
||||
System.out.println("welcome " + user.getUsername());
|
||||
return user;
|
||||
} else
|
||||
{
|
||||
System.err.println("incorrect password");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String hashPassword(String password)
|
||||
{
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] hashBytes = md.digest(password.getBytes());
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : hashBytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,35 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MenuService {
|
||||
|
||||
private final MenuItemDao menuItemDao;
|
||||
|
||||
public MenuService() {
|
||||
this.menuItemDao = new MenuItemDao();
|
||||
}
|
||||
|
||||
public void showMenu() {
|
||||
|
||||
// TODO:
|
||||
// Display menu items
|
||||
List<MenuItem> items = menuItemDao.findAll();
|
||||
if (items.isEmpty())
|
||||
{
|
||||
System.out.println("menu is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("\nAvailable Items:");
|
||||
for (MenuItem item : items)
|
||||
{
|
||||
System.out.printf("%d. %s - $%.2f%n",
|
||||
item.getId(),
|
||||
item.getName(),
|
||||
item.getPrice());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +1,119 @@
|
||||
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.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderService {
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
private final OrderDao orderDao;
|
||||
private final OrderDetailDao orderDetailDao;
|
||||
private final MenuItemDao menuItemDao;
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
public OrderService() {
|
||||
this.orderDao = new OrderDao();
|
||||
this.orderDetailDao = new OrderDetailDao();
|
||||
this.menuItemDao = new MenuItemDao();
|
||||
}
|
||||
|
||||
public int placeOrder(int userId, List<OrderDetail> cart) {
|
||||
|
||||
if (cart == null || cart.isEmpty()) {
|
||||
System.out.println("cart is empty");
|
||||
return -1;
|
||||
}
|
||||
|
||||
double totalPrice = 0;
|
||||
for (OrderDetail o : cart) {
|
||||
MenuItem item = menuItemDao.findById((o.getMenuItemId()));
|
||||
if (item == null) {
|
||||
System.out.println("menu item with id " + o.getMenuItemId() + "not found");
|
||||
return -1;
|
||||
}
|
||||
o.setPrice(item.getPrice());
|
||||
totalPrice += item.getPrice() * o.getQuantity();
|
||||
}
|
||||
|
||||
Order order = new Order();
|
||||
order.setUserId(userId);
|
||||
order.setTotalPrice(totalPrice);
|
||||
|
||||
int orderId = orderDao.save(order);
|
||||
if (orderId == -1) {
|
||||
System.out.println("failed to save order.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (OrderDetail detail : cart) {
|
||||
detail.setOrderId(orderId);
|
||||
orderDetailDao.save(detail);
|
||||
}
|
||||
|
||||
System.out.println("Order saved successfully!");
|
||||
return orderId;
|
||||
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||
if (details.isEmpty()) {
|
||||
System.out.println("order not found.");
|
||||
return;
|
||||
}
|
||||
|
||||
double total = 0;
|
||||
|
||||
System.out.println("\n[Order Summary / Receipt]");
|
||||
System.out.println("---------------------------------------");
|
||||
System.out.printf("%-12s %-6s %-8s %-10s%n", "Item", "Qty", "Unit", "Total");
|
||||
System.out.println("---------------------------------------");
|
||||
|
||||
for (OrderDetail detail : details) {
|
||||
MenuItem item = menuItemDao.findById(detail.getMenuItemId());
|
||||
String itemName = (item != null) ? item.getName() : "Unknown";
|
||||
double subtotal = detail.getPrice() * detail.getQuantity();
|
||||
total += subtotal;
|
||||
|
||||
System.out.printf("%-12s %-6d $%-7.2f $%-9.2f%n",
|
||||
itemName,
|
||||
detail.getQuantity(),
|
||||
detail.getPrice(),
|
||||
subtotal);
|
||||
}
|
||||
|
||||
System.out.println("---------------------------------------");
|
||||
System.out.printf("%-12s %-6s %-8s $%-9.2f%n", "", "", "Final Total:", total);
|
||||
System.out.println("---------------------------------------");
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
List<Order> orders = orderDao.findByUserId(userId);
|
||||
if (orders.isEmpty())
|
||||
{
|
||||
System.out.println("you have no orders.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("\nYour Order History:");
|
||||
System.out.println("--------------------------------------------------");
|
||||
System.out.printf("%-10s %-25s %-15s%n", "Order ID", "Date", "Total");
|
||||
System.out.println("--------------------------------------------------");
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
||||
for (Order order : orders) {
|
||||
System.out.printf("%-10d %-25s $%-14.2f%n",
|
||||
order.getId(),
|
||||
order.getCreatedAt().format(formatter),
|
||||
order.getTotalPrice());
|
||||
}
|
||||
System.out.println("--------------------------------------------------");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
package dev.ui;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.model.MenuItem;
|
||||
import dev.model.OrderDetail;
|
||||
import dev.model.User;
|
||||
import dev.service.AuthService;
|
||||
import dev.service.MenuService;
|
||||
import dev.service.OrderService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ConsoleMenu {
|
||||
@@ -7,38 +17,164 @@ 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 loggedInUser = null;
|
||||
|
||||
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");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1:
|
||||
// TODO
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// TODO
|
||||
break;
|
||||
|
||||
case 3:
|
||||
return;
|
||||
|
||||
default:
|
||||
System.out.println("Invalid choice");
|
||||
|
||||
if (loggedInUser == null) {
|
||||
showMainMenu();
|
||||
} else {
|
||||
showUserMenu();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void showMainMenu()
|
||||
{
|
||||
System.out.println();
|
||||
System.out.println("=======================================");
|
||||
System.out.println(" 🍕 WELCOME TO JAVA PIZZERIA 🍕");
|
||||
System.out.println("=======================================");
|
||||
System.out.println("1. Login");
|
||||
System.out.println("2. Register New Account");
|
||||
System.out.println("3. Exit");
|
||||
System.out.println("=======================================");
|
||||
System.out.print("Choose an option: ");
|
||||
|
||||
int choice = getIntInput();
|
||||
switch (choice)
|
||||
{
|
||||
case 1 -> login();
|
||||
case 2 -> register();
|
||||
case 3 -> {
|
||||
System.out.println("goodbye");
|
||||
System.exit(0);
|
||||
}
|
||||
default -> System.out.println("invalid choice.");
|
||||
}
|
||||
}
|
||||
|
||||
private void showUserMenu()
|
||||
{
|
||||
System.out.println();
|
||||
System.out.println("=======================================");
|
||||
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.print("Choose an option: ");
|
||||
|
||||
int choice = getIntInput();
|
||||
switch (choice)
|
||||
{
|
||||
case 1 -> menuService.showMenu();
|
||||
case 2 -> placeOrder();
|
||||
case 3 -> orderService.showOrderHistory(loggedInUser.getId());
|
||||
case 4 -> {
|
||||
loggedInUser = null;
|
||||
System.out.println("logged out.");
|
||||
}
|
||||
default -> System.out.println("invalid choice.");
|
||||
}
|
||||
}
|
||||
|
||||
private void login()
|
||||
{
|
||||
System.out.println("\n[Login]");
|
||||
System.out.print("Enter username: ");
|
||||
String username = scanner.nextLine().trim();
|
||||
System.out.print("Enter password: ");
|
||||
String password = scanner.nextLine().trim();
|
||||
|
||||
User user = authService.login(username, password);
|
||||
if (user != null)
|
||||
{
|
||||
loggedInUser = user;
|
||||
}
|
||||
}
|
||||
|
||||
private void register()
|
||||
{
|
||||
System.out.println("\n[Register]");
|
||||
System.out.print("Enter username: ");
|
||||
String username = scanner.nextLine().trim();
|
||||
System.out.print("Enter password: ");
|
||||
String password = scanner.nextLine().trim();
|
||||
System.out.print("Enter email (optional, press Enter to skip): ");
|
||||
String email = scanner.nextLine().trim();
|
||||
if (email.isEmpty()) email = null;
|
||||
|
||||
boolean success = authService.register(username, password, email);
|
||||
if (success)
|
||||
{
|
||||
System.out.println("registration successful. Please login.");
|
||||
}
|
||||
}
|
||||
|
||||
private void placeOrder()
|
||||
{
|
||||
System.out.println("\n[Placing Order]");
|
||||
menuService.showMenu();
|
||||
|
||||
List<OrderDetail> cart = new ArrayList<>();
|
||||
while (true)
|
||||
{
|
||||
System.out.print("\nEnter the ID of the item to add (or 0 to finish): ");
|
||||
int itemId = getIntInput();
|
||||
if (itemId == 0) break;
|
||||
|
||||
MenuItem item = new MenuItemDao().findById(itemId);
|
||||
if (item == null)
|
||||
{
|
||||
System.out.println("item not found.");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.print("Enter quantity: ");
|
||||
int quantity = getIntInput();
|
||||
if (quantity <= 0)
|
||||
{
|
||||
System.out.println("quantity must be greater than 0.");
|
||||
continue;
|
||||
}
|
||||
|
||||
OrderDetail detail = new OrderDetail();
|
||||
detail.setMenuItemId(itemId);
|
||||
detail.setQuantity(quantity);
|
||||
cart.add(detail);
|
||||
|
||||
System.out.printf("Added %dx %s to your cart.%n", quantity, item.getName());
|
||||
}
|
||||
|
||||
if (cart.isEmpty())
|
||||
{
|
||||
System.out.println("cart is empty. Order cancelled.");
|
||||
return;
|
||||
}
|
||||
|
||||
int orderId = orderService.placeOrder(loggedInUser.getId(), cart);
|
||||
if (orderId != -1)
|
||||
{
|
||||
orderService.printReceipt(orderId);
|
||||
}
|
||||
}
|
||||
|
||||
private int getIntInput() {
|
||||
while (true) {
|
||||
try {
|
||||
return Integer.parseInt(scanner.nextLine().trim());
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.print("please enter a number: ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user