Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
1.1 kB
2
Indexable
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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));
    }

    // Search using Streams
    public List<Product> searchProducts(String productName) {
        return products.stream()
                .filter(product -> product.getProductName().toLowerCase().contains(productName.toLowerCase()))
                .collect(Collectors.toList());
    }

    public Product getProductById(int productId) {
        return products.stream()
                .filter(product -> product.getProductId() == productId)
                .findFirst()
                .orElse(null);
    }
}
Leave a Comment