Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
2.2 kB
2
Indexable
Never
import java.util.List;
import java.util.Scanner;

public class ECommerceConsoleApp {
    private static ECommerceService service = new ECommerceService();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;
        do {
            System.out.println("\nE-Commerce Console App");
            System.out.println("1. Search Products");
            System.out.println("2. Add Product to Cart");
            System.out.println("3. View Shopping Cart");
            System.out.println("4. Checkout");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");
            choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    System.out.print("Enter product name to search: ");
                    String productName = scanner.nextLine();
                    List<Product> products = service.searchProducts(productName);
                    if (products.isEmpty()) {
                        System.out.println("No products found.");
                    } else {
                        for (Product product : products) {
                            System.out.println(product);
                        }
                    }
                    break;
                case 2:
                    System.out.print("Enter product ID to add to cart: ");
                    int productId = scanner.nextInt();
                    System.out.print("Enter quantity: ");
                    int quantity = scanner.nextInt();
                    service.addProductToCart(productId, quantity);
                    break;
                case 3:
                    service.viewShoppingCart();
                    break;
                case 4:
                    service.checkout();
                    break;
                case 5:
                    System.out.println("Thank you for using the E-Commerce Console App!");
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        } while (choice != 5);

        scanner.close();
    }
}
Leave a Comment