implement service classes
This commit is contained in:
@@ -1,23 +1,63 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.UserDao;
|
||||
import dev.model.User;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class AuthService {
|
||||
|
||||
private final UserDao userDao = new UserDao();
|
||||
|
||||
public boolean register(String username, String password, String email) {
|
||||
|
||||
// TODO:
|
||||
// Validate and register user
|
||||
if (!isValidEmail(email)){
|
||||
System.err.println("Invalid email address.");
|
||||
return false;
|
||||
}
|
||||
User user = new User(
|
||||
username,
|
||||
String.valueOf(password.hashCode()),
|
||||
email
|
||||
);
|
||||
|
||||
if (userDao.save(user)){
|
||||
System.out.println("Successfully registered user");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public User login(String username, String password) {
|
||||
|
||||
// TODO:
|
||||
// Authenticate user
|
||||
User dbUser = userDao.findByUsername(username);
|
||||
|
||||
if (dbUser == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
String dbUserPasswordHash = dbUser.getPasswordHash();
|
||||
|
||||
if (Objects.equals(dbUserPasswordHash,
|
||||
String.valueOf(password.hashCode()))){
|
||||
return dbUser;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public boolean isValidEmail(String email) {
|
||||
if (email == null || email.isBlank()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int atIndex = email.indexOf('@');
|
||||
if (atIndex <= 0) return false;
|
||||
int dotIndex = email.lastIndexOf('.');
|
||||
if (dotIndex <= atIndex + 1) return false;
|
||||
if (dotIndex == email.length() - 1) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,77 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.model.MenuItem;
|
||||
import dev.model.User;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MenuService {
|
||||
|
||||
public static final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
|
||||
public void showMenu() {
|
||||
|
||||
// TODO:
|
||||
// Display menu items
|
||||
List<MenuItem> items = menuItemDao.findAll();
|
||||
|
||||
System.out.println("=======================");
|
||||
System.out.println("== MENU ==");
|
||||
System.out.println("=======================");
|
||||
|
||||
for (MenuItem item : items) {
|
||||
|
||||
System.out.format("[%d] %s - $%.2f\n%s\nCategory: %s\n",
|
||||
item.getId(),
|
||||
item.getName(),
|
||||
item.getPrice(),
|
||||
item.getDescription(),
|
||||
item.getCategory()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void ShowPanel(User user) {
|
||||
|
||||
OrderService orderService = new OrderService();
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.println("=======================");
|
||||
System.out.println("== WELCOME ==");
|
||||
System.out.println("=======================");
|
||||
System.out.println("""
|
||||
1.Place order
|
||||
2.Order history
|
||||
3.Logout
|
||||
""");
|
||||
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1: {
|
||||
orderService.placeOrder(user.getId());
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
|
||||
orderService.showOrderHistory(user.getId());
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
System.out.println("goodbye, " + user.getUsername());
|
||||
return;
|
||||
}
|
||||
default:{
|
||||
System.out.println("invalid choice.enter a number between [1,3]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +1,169 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.dao.OrderDao;
|
||||
import dev.dao.OrderDetailDao;
|
||||
import dev.model.CartItem;
|
||||
import dev.model.MenuItem;
|
||||
import dev.model.Order;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
import java.awt.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
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) {
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
List<CartItem> cart = buildCart(scanner);
|
||||
|
||||
if (cart == null || cart.isEmpty()) {
|
||||
System.err.println("The cart is empty. Order placement aborted.");
|
||||
return;
|
||||
}
|
||||
|
||||
double totalPrice = 0;
|
||||
|
||||
for (CartItem cartItem: cart) {
|
||||
totalPrice += cartItem.getSubtotal();
|
||||
}
|
||||
|
||||
Order order = new Order();
|
||||
order.setUserId(userId);
|
||||
order.setCreatedAt(LocalDateTime.now());
|
||||
order.setTotalPrice(totalPrice);
|
||||
|
||||
int orderId = orderDao.save(order);
|
||||
|
||||
if (orderId == -1) {
|
||||
System.err.println("Order could not be placed (Database error).");
|
||||
return;
|
||||
}
|
||||
|
||||
order.setId(orderId);
|
||||
|
||||
for (CartItem cartItem : cart){
|
||||
OrderDetail orderDetail = new OrderDetail();
|
||||
orderDetail.setOrderId(orderId);
|
||||
orderDetail.setMenuItemId( cartItem.getItem().getId());
|
||||
orderDetail.setQuantity(cartItem.getQuantity());
|
||||
orderDetail.setPrice(cartItem.getItem().getPrice());
|
||||
|
||||
orderDetailDao.save(orderDetail);
|
||||
}
|
||||
|
||||
printReceipt(orderId);
|
||||
System.out.println("Order saved successfully.");
|
||||
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||
|
||||
System.out.println("\n--- order receipt: " + orderId + " ---");
|
||||
System.out.printf("%-15s %-5s %-10s%n", "name", "quantity", "totalPrice");
|
||||
|
||||
double total = 0;
|
||||
for (OrderDetail d : details) {
|
||||
MenuItem item = MenuService.menuItemDao.findById(d.getMenuItemId());
|
||||
double sub = d.getPrice() * d.getQuantity();
|
||||
total += sub;
|
||||
System.out.printf("%-15s %-5d %-10.2f%n", item.getName(), d.getQuantity(), sub);
|
||||
}
|
||||
System.out.println("------------------------------");
|
||||
System.out.printf("totalPrice: %.2f%n", total);
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
List<Order> orders = orderDao.findByUserId(userId);
|
||||
|
||||
if (orders.isEmpty())
|
||||
{
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public List<CartItem> buildCart(Scanner scanner) {
|
||||
|
||||
List<CartItem> cart = new ArrayList<>();
|
||||
|
||||
menuService.showMenu();
|
||||
|
||||
while(true) {
|
||||
System.out.println("\n[0] End order");
|
||||
System.out.println("[-1] Show menu again");
|
||||
System.out.println("Please select an item number:");
|
||||
|
||||
if (!scanner.hasNextInt()) {
|
||||
System.err.println("Invalid input. Please enter a number.");
|
||||
scanner.next();
|
||||
continue;
|
||||
}
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
if (choice == -1) {
|
||||
menuService.showMenu();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (choice == 0) {
|
||||
if (cart.isEmpty()) {
|
||||
System.out.println("Your cart is empty. Please select items.");
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
MenuItem item = MenuService.menuItemDao.findById(choice);
|
||||
|
||||
if (item == null) {
|
||||
System.err.println("Invalid item ID, please try again.");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.println("Enter quantity for " + item.getName() + ": ");
|
||||
|
||||
if (!scanner.hasNextInt()) {
|
||||
System.err.println("Invalid quantity input. Please enter a number.");
|
||||
scanner.next();
|
||||
continue;
|
||||
}
|
||||
|
||||
int quantity = scanner.nextInt();
|
||||
|
||||
if (quantity <= 0){
|
||||
System.err.println("Invalid quantity. Must be greater than 0.");
|
||||
continue;
|
||||
}
|
||||
|
||||
cart.add(new CartItem(item, quantity));
|
||||
System.out.println(quantity + "x " + item.getName() + " added to cart.");
|
||||
}
|
||||
|
||||
return cart;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user