forked from AdvancedProgramming1404/WS-10-Database
adding codes
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.model.MenuItem;
|
||||
import dev.database.DatabaseConnection;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
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 MenuItemDao {
|
||||
@@ -11,7 +18,23 @@ public class MenuItemDao {
|
||||
// TODO:
|
||||
// Retrieve all menu items
|
||||
|
||||
return null;
|
||||
// return null;
|
||||
List<MenuItem> menuItems = new ArrayList<>();
|
||||
String sql = "SELECT id, name, description, price, category FROM menu_items";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery()) {
|
||||
|
||||
while (rs.next()) {
|
||||
menuItems.add(mapRowToMenuItem(rs));
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return menuItems;
|
||||
}
|
||||
|
||||
public MenuItem findById(int id) {
|
||||
@@ -19,7 +42,32 @@ public class MenuItemDao {
|
||||
// TODO:
|
||||
// Find menu item by id
|
||||
|
||||
String sql = "SELECT id, name, description, price, category FROM menu_items WHERE id = ?";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setInt(1, id);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return mapRowToMenuItem(rs);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
private MenuItem mapRowToMenuItem(ResultSet rs) throws SQLException {
|
||||
int id = rs.getInt("id");
|
||||
String name = rs.getString("name");
|
||||
String description = rs.getString("description");
|
||||
BigDecimal price = rs.getBigDecimal("price");
|
||||
String category = rs.getString("category");
|
||||
|
||||
}
|
||||
return new MenuItem(id, name, description, price, category);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.model.Order;
|
||||
import dev.database.DatabaseConnection;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDao {
|
||||
@@ -10,6 +19,25 @@ public class OrderDao {
|
||||
|
||||
// TODO:
|
||||
// Insert order and return generated id
|
||||
String sql = "INSERT INTO orders (user_id, total_price) VALUES (?, ?)";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
|
||||
stmt.setInt(1, order.getUserId());
|
||||
stmt.setBigDecimal(2, order.getTotalPrice());
|
||||
|
||||
stmt.executeUpdate();
|
||||
|
||||
try (ResultSet rs = stmt.getGeneratedKeys()) {
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -18,8 +46,30 @@ public class OrderDao {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
List<Order> orders = new ArrayList<>();
|
||||
String sql = "SELECT id, user_id, created_at, total_price FROM orders WHERE user_id = ?";
|
||||
|
||||
return null;
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setInt(1, userId);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
int id = rs.getInt("id");
|
||||
int uId = rs.getInt("user_id");
|
||||
Timestamp createdAt = rs.getTimestamp("created_at");
|
||||
BigDecimal totalPrice = rs.getBigDecimal("total_price");
|
||||
|
||||
orders.add(new Order(id, uId, createdAt.toLocalDateTime(), totalPrice));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return orders;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,14 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.model.OrderDetail;
|
||||
import dev.database.DatabaseConnection;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
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 {
|
||||
@@ -10,15 +17,52 @@ public class OrderDetailDao {
|
||||
|
||||
// TODO:
|
||||
// Insert order detail
|
||||
String sql = "INSERT INTO order_details (order_id, menu_item_id, quantity, unit_price) VALUES (?, ?, ?, ?)";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setInt(1, detail.getOrderId());
|
||||
stmt.setInt(2, detail.getMenuItemId());
|
||||
stmt.setInt(3, detail.getQuantity());
|
||||
stmt.setBigDecimal(4, detail.getPrice());
|
||||
|
||||
stmt.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
List<OrderDetail> details = new ArrayList<>();
|
||||
String sql = "SELECT id, order_id, menu_item_id, quantity, unit_price FROM order_details WHERE order_id = ?";
|
||||
|
||||
return null;
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setInt(1, orderId);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
int id = rs.getInt("id");
|
||||
int oId = rs.getInt("order_id");
|
||||
int menuItemId = rs.getInt("menu_item_id");
|
||||
int quantity = rs.getInt("quantity");
|
||||
BigDecimal price = rs.getBigDecimal("unit_price");
|
||||
|
||||
details.add(new OrderDetail(id, oId, menuItemId, quantity, price));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return details;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +1,61 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.model.User;
|
||||
import dev.database.DatabaseConnection;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
public class UserDao {
|
||||
|
||||
public boolean save(User user) {
|
||||
|
||||
// TODO:
|
||||
// Insert user into database
|
||||
String sql = "INSERT INTO users (username, password_hash, email) VALUES (?, ?, ?)";
|
||||
|
||||
return false;
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setString(1, user.getUsername());
|
||||
stmt.setString(2, user.getPassword());
|
||||
stmt.setString(3, user.getEmail());
|
||||
|
||||
stmt.executeUpdate();
|
||||
return true;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
|
||||
// TODO:
|
||||
// Find a user by username
|
||||
String sql = "SELECT id, username, password_hash, email FROM users WHERE username = ?";
|
||||
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
|
||||
stmt.setString(1, username);
|
||||
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
int id = rs.getInt("id");
|
||||
String uname = rs.getString("username");
|
||||
String passwordHash = rs.getString("password_hash");
|
||||
String email = rs.getString("email");
|
||||
|
||||
return new User(id, uname, passwordHash, email);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
package dev.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatabaseConnection {
|
||||
private static final String URL = "jdbc:postgresql://localhost:5432/restaurant_db";
|
||||
|
||||
private static final String URL = "jdbc:postgresql://localhost:5432/restaurant_db"; // DB Server
|
||||
private static final String USER = "avasanatkar";
|
||||
|
||||
private static final String USER = "postgres"; // Your Username
|
||||
private static final String PASSWORD = "";
|
||||
|
||||
private static final String PASSWORD = "password"; // Your Password
|
||||
|
||||
private DatabaseConnection() {
|
||||
|
||||
@@ -21,7 +22,9 @@ public class DatabaseConnection {
|
||||
// TODO:
|
||||
// Return a valid PostgreSQL connection
|
||||
|
||||
return null;
|
||||
// return null;
|
||||
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +1,45 @@
|
||||
package dev.model;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class MenuItem {
|
||||
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private double price;
|
||||
|
||||
private BigDecimal price;
|
||||
private String category;
|
||||
|
||||
public MenuItem() {}
|
||||
|
||||
public MenuItem(int id, String name, String description, BigDecimal price, String category) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.price = price;
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public MenuItem(String name, String description, BigDecimal price, String category) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.price = price;
|
||||
this.category = 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 BigDecimal getPrice() { return price; }
|
||||
public void setPrice(BigDecimal price) { this.price = price; }
|
||||
public String getCategory() { return category; }
|
||||
public void setCategory(String category) { this.category = category; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return id + ". " + name + " - $" + price + (category != null ? " (" + category + ")" : "");
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,40 @@
|
||||
package dev.model;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class Order {
|
||||
|
||||
private int id;
|
||||
|
||||
private int userId;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
private BigDecimal totalPrice;
|
||||
|
||||
private double totalPrice;
|
||||
public Order() {}
|
||||
|
||||
public Order(int id, int userId, LocalDateTime createdAt, BigDecimal totalPrice) {
|
||||
this.id = id;
|
||||
this.userId = userId;
|
||||
this.createdAt = createdAt;
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
|
||||
public Order(int userId, BigDecimal totalPrice) {
|
||||
this.userId = userId;
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
|
||||
public int getId() { return id; }
|
||||
public void setId(int id) { this.id = id; }
|
||||
public int getUserId() { return userId; }
|
||||
public void setUserId(int userId) { this.userId = userId; }
|
||||
public LocalDateTime getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
|
||||
public BigDecimal getTotalPrice() { return totalPrice; }
|
||||
public void setTotalPrice(BigDecimal totalPrice) { this.totalPrice = totalPrice; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order #" + id + " - " + createdAt + " - Total: $" + totalPrice;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,44 @@
|
||||
package dev.model;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class OrderDetail {
|
||||
|
||||
private int id;
|
||||
|
||||
private int orderId;
|
||||
|
||||
private int menuItemId;
|
||||
|
||||
private int quantity;
|
||||
private BigDecimal price;
|
||||
|
||||
private double price;
|
||||
public OrderDetail() {}
|
||||
|
||||
public OrderDetail(int id, int orderId, int menuItemId, int quantity, BigDecimal price) {
|
||||
this.id = id;
|
||||
this.orderId = orderId;
|
||||
this.menuItemId = menuItemId;
|
||||
this.quantity = quantity;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public OrderDetail(int orderId, int menuItemId, int quantity, BigDecimal price) {
|
||||
this.orderId = orderId;
|
||||
this.menuItemId = menuItemId;
|
||||
this.quantity = quantity;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getId() { return id; }
|
||||
public void setId(int id) { this.id = id; }
|
||||
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 BigDecimal getPrice() { return price; }
|
||||
public void setPrice(BigDecimal price) { this.price = price; }
|
||||
|
||||
public BigDecimal getSubtotal() {
|
||||
return price.multiply(BigDecimal.valueOf(quantity));
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,36 @@ package dev.model;
|
||||
public class User {
|
||||
|
||||
private int id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String email;
|
||||
|
||||
public User() {}
|
||||
|
||||
public User(int id, String username, String password, String email) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public User(String username, String password, String email) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public int getId() { return id; }
|
||||
public void setId(int id) { this.id = id; }
|
||||
public String getUsername() { return username; }
|
||||
public void setUsername(String username) { this.username = username; }
|
||||
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; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{id=" + id + ", username='" + username + "', email='" + email + "'}";
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,42 @@
|
||||
package dev.service;
|
||||
|
||||
import dev.model.User;
|
||||
import dev.dao.UserDao;
|
||||
import org.mindrot.jbcrypt.BCrypt;
|
||||
|
||||
public class AuthService {
|
||||
|
||||
private final UserDao userDao = new UserDao();
|
||||
public boolean register(String username, String password, String email) {
|
||||
|
||||
// TODO:
|
||||
// Validate and register user
|
||||
if (username == null || username.isBlank() || password == null || password.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
if (userDao.findByUsername(username) != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());
|
||||
User newUser = new User(username, hashedPassword, email);
|
||||
|
||||
return userDao.save(newUser);
|
||||
}
|
||||
|
||||
public User login(String username, String password) {
|
||||
|
||||
// TODO:
|
||||
// Authenticate user
|
||||
User user = userDao.findByUsername(username);
|
||||
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (BCrypt.checkpw(password, user.getPassword())) {
|
||||
return user;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,30 @@
|
||||
package dev.service;
|
||||
import dev.dao.MenuItemDao;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
import java.util.List;
|
||||
public class MenuService {
|
||||
|
||||
private final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
public void showMenu() {
|
||||
|
||||
// TODO:
|
||||
// Display menu items
|
||||
List<MenuItem> items = menuItemDao.findAll();
|
||||
|
||||
if (items.isEmpty()) {
|
||||
System.out.println("No menu items available.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("=======================================");
|
||||
System.out.println(" MENU");
|
||||
System.out.println("=======================================");
|
||||
|
||||
for (MenuItem item : items) {
|
||||
System.out.println(item);
|
||||
}
|
||||
|
||||
System.out.println("=======================================");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +1,108 @@
|
||||
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.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
public class OrderService {
|
||||
private final OrderDao orderDao = new OrderDao();
|
||||
private final OrderDetailDao orderDetailDao = new OrderDetailDao();
|
||||
private final MenuItemDao menuItemDao = new MenuItemDao();
|
||||
|
||||
public void placeOrder(int userId) {
|
||||
public int placeOrder(int userId, Map<Integer, Integer> itemQuantities) {
|
||||
|
||||
// TODO:
|
||||
// Create order
|
||||
|
||||
if (itemQuantities == null || itemQuantities.isEmpty()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
BigDecimal total = BigDecimal.ZERO;
|
||||
|
||||
for (Map.Entry<Integer, Integer> entry : itemQuantities.entrySet()) {
|
||||
MenuItem item = menuItemDao.findById(entry.getKey());
|
||||
if (item == null) {
|
||||
continue;
|
||||
}
|
||||
int quantity = entry.getValue();
|
||||
total = total.add(item.getPrice().multiply(BigDecimal.valueOf(quantity)));
|
||||
}
|
||||
|
||||
Order order = new Order(userId, total);
|
||||
int orderId = orderDao.save(order);
|
||||
|
||||
if (orderId == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (Map.Entry<Integer, Integer> entry : itemQuantities.entrySet()) {
|
||||
MenuItem item = menuItemDao.findById(entry.getKey());
|
||||
if (item == null) {
|
||||
continue;
|
||||
}
|
||||
int quantity = entry.getValue();
|
||||
|
||||
OrderDetail detail = new OrderDetail(orderId, item.getId(), quantity, item.getPrice());
|
||||
orderDetailDao.save(detail);
|
||||
}
|
||||
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void printReceipt(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Print order receipt
|
||||
List<OrderDetail> details = orderDetailDao.findByOrderId(orderId);
|
||||
|
||||
if (details.isEmpty()) {
|
||||
System.out.println("No details found for order #" + orderId);
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("---------------------------------------");
|
||||
System.out.printf("%-12s %-6s %-10s %-10s%n", "Item", "Qty", "Unit", "Total");
|
||||
System.out.println("---------------------------------------");
|
||||
|
||||
BigDecimal grandTotal = BigDecimal.ZERO;
|
||||
|
||||
for (OrderDetail detail : details) {
|
||||
MenuItem item = menuItemDao.findById(detail.getMenuItemId());
|
||||
String name = (item != null) ? item.getName() : "Unknown item";
|
||||
BigDecimal subtotal = detail.getSubtotal();
|
||||
grandTotal = grandTotal.add(subtotal);
|
||||
|
||||
System.out.printf("%-12s %-6d $%-9.2f $%-9.2f%n",
|
||||
name, detail.getQuantity(), detail.getPrice(), subtotal);
|
||||
}
|
||||
|
||||
System.out.println("---------------------------------------");
|
||||
System.out.println("Final Total: $" + grandTotal);
|
||||
}
|
||||
|
||||
public void showOrderHistory(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Display user's order history
|
||||
List<Order> orders = orderDao.findByUserId(userId);
|
||||
|
||||
if (orders.isEmpty()) {
|
||||
System.out.println("No past orders found.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("=======================================");
|
||||
System.out.println(" ORDER HISTORY");
|
||||
System.out.println("=======================================");
|
||||
|
||||
for (Order order : orders) {
|
||||
System.out.println(order);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,23 @@
|
||||
package dev.ui;
|
||||
|
||||
import java.util.Scanner;
|
||||
import dev.model.MenuItem;
|
||||
import dev.model.User;
|
||||
import dev.service.AuthService;
|
||||
import dev.service.MenuService;
|
||||
import dev.service.OrderService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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();
|
||||
public void start() {
|
||||
|
||||
while (true) {
|
||||
@@ -23,10 +34,12 @@ public class ConsoleMenu {
|
||||
|
||||
case 1:
|
||||
// TODO
|
||||
handleLogin();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// TODO
|
||||
handleRegister();
|
||||
break;
|
||||
|
||||
case 3:
|
||||
@@ -40,5 +53,138 @@ public class ConsoleMenu {
|
||||
}
|
||||
|
||||
}
|
||||
private void handleRegister() {
|
||||
|
||||
System.out.println();
|
||||
System.out.println("[Register New Account]");
|
||||
System.out.print("Choose a username: ");
|
||||
String username = scanner.next();
|
||||
System.out.print("Choose a password: ");
|
||||
String password = scanner.next();
|
||||
scanner.nextLine();
|
||||
System.out.print("Email (optional, press enter to skip): ");
|
||||
String email = scanner.nextLine();
|
||||
|
||||
if (email.isBlank()) {
|
||||
email = null;
|
||||
}
|
||||
|
||||
boolean success = authService.register(username, password, email);
|
||||
|
||||
if (success) {
|
||||
System.out.println("Registration successful! You can now login.");
|
||||
} else {
|
||||
System.out.println("Registration failed. Username may already be taken.");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleLogin() {
|
||||
|
||||
System.out.println();
|
||||
System.out.println("[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) {
|
||||
System.out.println("Invalid username or password.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Welcome, " + user.getUsername() + "!");
|
||||
showMainMenu(user);
|
||||
}
|
||||
|
||||
private void showMainMenu(User user) {
|
||||
|
||||
boolean loggedIn = true;
|
||||
|
||||
while (loggedIn) {
|
||||
|
||||
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");
|
||||
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
|
||||
case 1:
|
||||
menuService.showMenu();
|
||||
break;
|
||||
|
||||
case 2:
|
||||
handlePlaceOrder(user);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
orderService.showOrderHistory(user.getId());
|
||||
break;
|
||||
|
||||
case 4:
|
||||
loggedIn = false;
|
||||
System.out.println("Logged out.");
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Invalid choice");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handlePlaceOrder(User user) {
|
||||
|
||||
menuService.showMenu();
|
||||
|
||||
Map<Integer, Integer> itemQuantities = new HashMap<>();
|
||||
|
||||
System.out.println();
|
||||
System.out.println("[Placing Order]");
|
||||
|
||||
while (true) {
|
||||
System.out.print("Enter the ID of the item to add (or 0 to finish): ");
|
||||
int itemId = scanner.nextInt();
|
||||
|
||||
if (itemId == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
System.out.print("Enter quantity: ");
|
||||
int quantity = scanner.nextInt();
|
||||
|
||||
if (quantity <= 0) {
|
||||
System.out.println("Quantity must be greater than zero.");
|
||||
continue;
|
||||
}
|
||||
|
||||
itemQuantities.merge(itemId, quantity, Integer::sum);
|
||||
System.out.println("Added " + quantity + "x item #" + itemId + " to your cart.");
|
||||
}
|
||||
|
||||
if (itemQuantities.isEmpty()) {
|
||||
System.out.println("No items added. Order cancelled.");
|
||||
return;
|
||||
}
|
||||
|
||||
int orderId = orderService.placeOrder(user.getId(), itemQuantities);
|
||||
|
||||
if (orderId == -1) {
|
||||
System.out.println("Failed to place order.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("[Order Summary / Receipt]");
|
||||
orderService.printReceipt(orderId);
|
||||
System.out.println("Order saved successfully!");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user