Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
1.2 kB
2
Indexable
Never
import java.util.ArrayList;
import java.util.List;

public class Inventory {
    private List<Product> products = new ArrayList<>();

    public Inventory() {
        // Hardcoded products for the example
        products.add(new Product(1, "Laptop", "Electronics", 999.99));
        products.add(new Product(2, "Headphones", "Electronics", 199.99));
        products.add(new Product(3, "Coffee Maker", "Appliances", 49.99));
        products.add(new Product(4, "Smartphone", "Electronics", 699.99));
        products.add(new Product(5, "Tablet", "Electronics", 399.99));
    }

    public List<Product> searchProducts(String productName) {
        List<Product> result = new ArrayList<>();
        for (Product product : products) {
            if (product.getProductName().toLowerCase().contains(productName.toLowerCase())) {
                result.add(product);
            }
        }
        return result;
    }

    public Product getProductById(int productId) {
        for (Product product : products) {
            if (product.getProductId() == productId) {
                return product;
            }
        }
        return null;
    }
}
Leave a Comment