/
itokar98
/
HomeWorkPatternsSolidTask1
Обзор
Документация
Войти
/
itokar98
/
HomeWorkPatternsSolidTask1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/Main.java
190 строк
10 KB
Ilya Tokar
first_commit
19 ноя 2025, 01:14
19 ноя 2025, 01:14
8a62050
Код
Авторство
О чём код?
import model.*; import repository.*; import service.*; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { // Initialize repositories ProductRepository productRepository = new ProductRepository(); OrderRepository orderRepository = new OrderRepository(); // Initialize services with dependency injection ProductService productService = new ProductService(productRepository); CartService cartService = new CartService(productService); OrderService orderService = new OrderService(orderRepository, cartService); Scanner scanner = new Scanner(System.in); Long currentUserId = 1L; // Simulating logged-in user System.out.println("Welcome to Online Store!"); while (true) { System.out.println("\n--- Main Menu ---"); System.out.println("1. View all products"); System.out.println("2. Search products by name"); System.out.println("3. Filter products by category"); System.out.println("4. Filter products by price range"); System.out.println("5. View my cart"); System.out.println("6. Add product to cart"); System.out.println("7. Remove product from cart"); System.out.println("8. Checkout (create order)"); System.out.println("9. View my orders"); System.out.println("10. Track order status"); System.out.println("11. Cancel order"); System.out.println("12. View recommended products"); System.out.println("0. Exit"); System.out.print("Choose an option: "); int choice = scanner.nextInt(); scanner.nextLine(); // Consume newline try { switch (choice) { case 1 -> { System.out.println("\n--- All Products ---"); List<Product> allProducts = productService.getAllProducts(); allProducts.forEach(System.out::println); } case 2 -> { System.out.print("Enter search keyword: "); String keyword = scanner.nextLine(); List<Product> searchResults = productService.searchProducts(keyword); if (searchResults.isEmpty()) { System.out.println("No products found matching '" + keyword + "'"); } else { System.out.println("\n--- Search Results for '" + keyword + "' ---"); searchResults.forEach(System.out::println); } } case 3 -> { System.out.print("Enter category: "); String category = scanner.nextLine(); List<Product> categoryProducts = productService.getProductsByCategory(category); if (categoryProducts.isEmpty()) { System.out.println("No products found in category '" + category + "'"); } else { System.out.println("\n--- Products in '" + category + "' ---"); categoryProducts.forEach(System.out::println); } } case 4 -> { System.out.print("Enter minimum price: "); double minPrice = scanner.nextDouble(); System.out.print("Enter maximum price: "); double maxPrice = scanner.nextDouble(); List<Product> priceRangeProducts = productService.getProductsByPriceRange(minPrice, maxPrice); if (priceRangeProducts.isEmpty()) { System.out.println("No products found in price range " + minPrice + "-" + maxPrice); } else { System.out.println("\n--- Products in price range " + minPrice + "-" + maxPrice + " ---"); priceRangeProducts.forEach(System.out::println); } } case 5 -> { System.out.println("\n--- Your Cart ---"); List<Product> cartItems = cartService.getCartItems(currentUserId); if (cartItems.isEmpty()) { System.out.println("Your cart is empty"); } else { cartItems.forEach(System.out::println); System.out.println("Total: $" + String.format("%.2f", cartService.getCartTotal(currentUserId))); } } case 6 -> { System.out.print("Enter product ID to add to cart: "); Long productId = scanner.nextLong(); try { cartService.addProductToCart(currentUserId, productId); System.out.println("Product added to cart!"); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } } case 7 -> { System.out.print("Enter product ID to remove from cart: "); Long productId = scanner.nextLong(); try { cartService.removeProductFromCart(currentUserId, productId); System.out.println("Product removed from cart!"); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } } case 8 -> { try { Order order = orderService.createOrderFromCart(currentUserId); System.out.println("Order created successfully!"); System.out.println("Order ID: " + order.getId()); System.out.println("Total amount: $" + String.format("%.2f", order.getTotalPrice())); System.out.println("Status: " + order.getStatus()); } catch (IllegalStateException e) { System.out.println("Error: " + e.getMessage()); } } case 9 -> { System.out.println("\n--- Your Orders ---"); List<Order> userOrders = orderService.getOrdersByUserId(currentUserId); if (userOrders.isEmpty()) { System.out.println("You have no orders yet"); } else { userOrders.forEach(order -> System.out.println("Order ID: " + order.getId() + ", Date: " + order.getOrderDate() + ", Status: " + order.getStatus() + ", Total: $" + String.format("%.2f", order.getTotalPrice())) ); } } case 10 -> { System.out.print("Enter order ID to track: "); Long orderId = scanner.nextLong(); try { Order order = orderService.getOrderById(orderId); System.out.println("\n--- Order Tracking ---"); System.out.println("Order ID: " + order.getId()); System.out.println("Status: " + order.getStatus()); System.out.println("Order Date: " + order.getOrderDate()); System.out.println("Products:"); order.getProducts().forEach(p -> System.out.println(" - " + p.getName() + ": $" + String.format("%.2f", p.getPrice())) ); System.out.println("Total: $" + String.format("%.2f", order.getTotalPrice())); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } } case 11 -> { System.out.print("Enter order ID to cancel: "); Long orderId = scanner.nextLong(); try { orderService.cancelOrder(orderId); System.out.println("Order cancelled successfully!"); } catch (IllegalStateException e) { System.out.println("Error: " + e.getMessage()); } } case 12 -> { System.out.println("\n--- Recommended Products ---"); List<Product> recommendations = orderService.getRecommendedProducts(currentUserId); if (recommendations.isEmpty()) { System.out.println("No recommendations available"); } else { recommendations.forEach(System.out::println); } } case 0 -> { System.out.println("Thank you for shopping with us!"); return; } default -> System.out.println("Invalid option. Please try again."); } } catch (Exception e) { System.out.println("An error occurred: " + e.getMessage()); scanner.nextLine(); // Clear invalid input } } } }