ditmedanh

Home   »   ditmedanh

import java.util.ArrayList;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Number of products: ");
        int n = sc.nextInt();
        ArrayList products = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            System.out.println(i + 1);
            System.out.println("Enter product id: ");
            int id = sc.nextInt();
            sc.nextLine();
            System.out.println("Enter product name: ");
            String name = sc.nextLine();
            System.out.println("Enter company: ");
            String company = sc.nextLine();
            System.out.println("Enter color: ");
            String color = sc.nextLine();
            System.out.println("Enter price: ");
            double price = sc.nextDouble();
            sc.nextLine();
            products.add(new Product(id, name, company, color, price));
        }

        products.stream().filter(product -> {
            if(product.color.equals("RED") || product.color.equals("BLUE"))
                return true;
            return false;
        }).forEach(System.out::println);
    }
}

class Product {
    int id;
    String name;
    String company;
    String color;
    Double price;

    public Product(int id, String name, String company, String color, Double price) {
        this.id = id;
        this.name = name;
        this.company = company;
        this.color = color;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", company='" + company + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                '}';
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *