package com.example.orders;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ProductRepository {

    private static Map<String, Product> STORE = new HashMap<String, Product>();

    static {
        STORE.put("P-001", new Product("P-001", "Javaパーフェクトガイド", 3800, "BOOK", 12));
        STORE.put("P-002", new Product("P-002", "リファクタリング実践入門", 3200, "BOOK", 5));
        STORE.put("P-003", new Product("P-003", "27インチモニタ", 28000, "DEVICE", 3));
        STORE.put("P-004", new Product("P-004", "メカニカルキーボード", 12800, "DEVICE", 8));
        STORE.put("P-005", new Product("P-005", "USB-Cハブ", 4500, "DEVICE", 20));
        STORE.put("P-006", new Product("P-006", "ドリップコーヒー30袋", 1800, "FOOD", 40));
        STORE.put("P-007", new Product("P-007", "エナジードリンク24本", 3600, "FOOD", 15));
        STORE.put("P-008", new Product("P-008", "ノイズキャンセリングヘッドホン", 21000, "DEVICE", 4));
        STORE.put("P-009", new Product("P-009", "デスクマット", 2400, "GOODS", 30));
        STORE.put("P-010", new Product("P-010", "モニターライト", 6800, "DEVICE", 7));
    }

    public Product findById(String id) {
        return STORE.get(id);
    }

    public List<Product> findByCategory(String category) {
        List<Product> result = new ArrayList<Product>();
        Iterator<Product> it = STORE.values().iterator();
        while (it.hasNext()) {
            Product p = it.next();
            if (p.getCategory().equals(category)) {
                result.add(p);
            }
        }
        return result;
    }

    public List<Product> findAll() {
        return new ArrayList<Product>(STORE.values());
    }
}
