Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

java - product list cannot be cast to iterable type


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code mixes up a number of concepts that are at odds with each other. You seem to be defining a Products class that represents both a single product and a list of products. Also, it is unclear if you wish to instantiate multiple lists of products, or if you want to have a single list of products by way of static methods.

I would recommend that you define a class named Product that represents a single product. Then, you could either define a second class named Products that represents a list of products, or you could add static methods and variables to your Product class that allow you to maintain a single list of instances of that class at any one tim

Here is a version of your code that takes the second approach listed above. The class name is Product to represent a single product, and then there are static methods that let you operate on a single list of Product objects. As it is, a list of products is built in, and is created the first time you attempt to access the list of products. A setProductList static method is also provided that would let you create a different list of products and plug that in instead of using the built in list. The class's constructor was made public to allow for this.

import java.util.ArrayList;
import java.util.List;

class Product {

    private static List<Product> productList = null;

    private static List<Product> products() {
        // If no list of products has been set, use the built-in list
        if (productList == null)
            initProductList();
        return productList;
    }

    private static void initProductList() {
        productList = new ArrayList<Product>(6);
        productList.add(new Product(0, "Foundation"));
        productList.add(new Product(1, "BB Cream"));
        productList.add(new Product(2, "Concealer"));
        productList.add(new Product(3, "Blush"));
        productList.add(new Product(4, "High Lighter"));
        productList.add(new Product(5, "Setting Spray/Powder"));
    }

    public static void setProductList(List<Product> productList) {
        Product.productList = productList;
    }

    private int id;
    private String name;

    public Product(int id, String product){
        this.id = id;
        this.name = product;
    }

    public static Product getProductById(int id){
        for(Product product: Product.products())
            if(product.id == id)
                return product;
        return null;
    }

    public static String getProductNameById(int id) {
        Product product = getProductById(id);
        if (product == null)
            return null;
        return product.name;
    }

    public static void main(String[] args) {
        System.out.println(Product.getProductNameById(0));
        System.out.println(Product.getProductNameById(2));
        System.out.println(Product.getProductNameById(4));
    }
}

Result:

Foundation
Concealer
High Lighter

UPDATE: For the record...If I had it to do over again, I would have chosen to demonstrate the other solution I suggested in the text of my answer. So much of this class's logic is about managing a list of Product objects, and little of it is about representing each Product. I think it would be better to define two classes, Product and Products, and put much of this logic in the latter class. At that point, I'd probably do away with the static aspect all together.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...