E-commerce Inventory Manager Java

👤 Sharing: AI
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class EcommerceInventoryManager {

    private Map<String, Product> inventory;
    private Scanner scanner;

    public EcommerceInventoryManager() {
        inventory = new HashMap<>();
        scanner = new Scanner(System.in);
    }

    public static void main(String[] args) {
        EcommerceInventoryManager manager = new EcommerceInventoryManager();
        manager.run();
    }

    public void run() {
        while (true) {
            displayMenu();
            int choice = getChoice();

            switch (choice) {
                case 1:
                    addProduct();
                    break;
                case 2:
                    viewProduct();
                    break;
                case 3:
                    updateQuantity();
                    break;
                case 4:
                    deleteProduct();
                    break;
                case 5:
                    listAllProducts();
                    break;
                case 6:
                    searchProduct();
                    break;
                case 0:
                    System.out.println("Exiting Inventory Manager. Goodbye!");
                    return;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        }
    }

    private void displayMenu() {
        System.out.println("\nE-commerce Inventory Manager");
        System.out.println("1. Add Product");
        System.out.println("2. View Product Details");
        System.out.println("3. Update Product Quantity");
        System.out.println("4. Delete Product");
        System.out.println("5. List All Products");
        System.out.println("6. Search for Product");
        System.out.println("0. Exit");
        System.out.print("Enter your choice: ");
    }

    private int getChoice() {
        try {
            return Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            return -1; // Indicate invalid input
        }
    }

    private void addProduct() {
        System.out.print("Enter product ID: ");
        String productId = scanner.nextLine();

        if (inventory.containsKey(productId)) {
            System.out.println("Product with this ID already exists.");
            return;
        }

        System.out.print("Enter product name: ");
        String name = scanner.nextLine();

        System.out.print("Enter product description: ");
        String description = scanner.nextLine();

        System.out.print("Enter product price: ");
        double price = getDoubleInput();
        if(price == -1) {
            System.out.println("Invalid input for price.  Product not added.");
            return;
        }

        System.out.print("Enter initial quantity: ");
        int quantity = getIntegerInput();
        if(quantity == -1){
            System.out.println("Invalid input for quantity. Product not added.");
            return;
        }

        Product product = new Product(productId, name, description, price, quantity);
        inventory.put(productId, product);
        System.out.println("Product added successfully!");
    }

    private void viewProduct() {
        System.out.print("Enter product ID to view: ");
        String productId = scanner.nextLine();

        if (inventory.containsKey(productId)) {
            Product product = inventory.get(productId);
            System.out.println(product); // Uses the toString() method of Product
        } else {
            System.out.println("Product not found with ID: " + productId);
        }
    }

    private void updateQuantity() {
        System.out.print("Enter product ID to update quantity: ");
        String productId = scanner.nextLine();

        if (inventory.containsKey(productId)) {
            System.out.print("Enter new quantity: ");
            int newQuantity = getIntegerInput();
            if(newQuantity == -1){
                System.out.println("Invalid quantity input. Quantity not updated.");
                return;
            }

            inventory.get(productId).setQuantity(newQuantity);
            System.out.println("Quantity updated successfully!");
        } else {
            System.out.println("Product not found with ID: " + productId);
        }
    }

    private void deleteProduct() {
        System.out.print("Enter product ID to delete: ");
        String productId = scanner.nextLine();

        if (inventory.containsKey(productId)) {
            inventory.remove(productId);
            System.out.println("Product deleted successfully!");
        } else {
            System.out.println("Product not found with ID: " + productId);
        }
    }

    private void listAllProducts() {
        if (inventory.isEmpty()) {
            System.out.println("Inventory is empty.");
            return;
        }

        System.out.println("\n--- All Products ---");
        for (Product product : inventory.values()) {
            System.out.println(product);
        }
        System.out.println("--------------------");
    }

    private void searchProduct() {
        System.out.print("Enter search term (name or description): ");
        String searchTerm = scanner.nextLine().toLowerCase();

        List<Product> searchResults = new ArrayList<>();
        for (Product product : inventory.values()) {
            if (product.getName().toLowerCase().contains(searchTerm) ||
                product.getDescription().toLowerCase().contains(searchTerm)) {
                searchResults.add(product);
            }
        }

        if (searchResults.isEmpty()) {
            System.out.println("No products found matching the search term: " + searchTerm);
        } else {
            System.out.println("\n--- Search Results ---");
            for (Product product : searchResults) {
                System.out.println(product);
            }
            System.out.println("----------------------");
        }
    }

    private double getDoubleInput() {
        try {
            return Double.parseDouble(scanner.nextLine());
        } catch (NumberFormatException e) {
            return -1; // Indicate invalid input
        }
    }

    private int getIntegerInput() {
        try {
            return Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            return -1; // Indicate invalid input
        }
    }

    // Inner class to represent a Product
    private static class Product {
        private String productId;
        private String name;
        private String description;
        private double price;
        private int quantity;

        public Product(String productId, String name, String description, double price, int quantity) {
            this.productId = productId;
            this.name = name;
            this.description = description;
            this.price = price;
            this.quantity = quantity;
        }

        public String getProductId() {
            return productId;
        }

        public String getName() {
            return name;
        }

        public String getDescription() {
            return description;
        }

        public double getPrice() {
            return price;
        }

        public int getQuantity() {
            return quantity;
        }

        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }

        @Override
        public String toString() {
            return "Product ID: " + productId +
                   "\nName: " + name +
                   "\nDescription: " + description +
                   "\nPrice: $" + String.format("%.2f", price) +
                   "\nQuantity: " + quantity + "\n";
        }
    }
}
```
👁️ Viewed: 9

Comments