Метод Java add ArrayList не определен для типа <OBJECT> - PullRequest
0 голосов
/ 05 октября 2019

Я создаю Java-программу для домашней работы, где я должен добавить продукт в конкретный магазин. У меня проблема при попытке добавить в ArrayList из класса Store.

У меня есть продукт класса следующим образом:

class Product {

    private String pName;
    private int pPrice;
    private int pQty;

    public Product (String pName, int pPrice, int pQty) {
        this.pName = pName;
        this.pPrice = pPrice;
        this.pQty = pQty;
    }
}

И класс хранения следующим образом:

class Store {

    private String storeName;
    ArrayList<Product> pList =new ArrayList<>();

    public Store() {
        String name = storeName;
        pList = new ArrayList<Product>();
    }
    public Store(String newStoreName,ArrayList<Product> newPList) {
        this.storeName = newStoreName;
        this.pList = newPList;
    }

    void setName(String storeName) {
        this.storeName = storeName;
    }
    void setProduct(Product pList) {
        pList.add(this.pList);//This return method add undefined for type Product, how to solve this error?
    }
    String getName() {
        return storeName;
    }
    ArrayList<Product> getProductList() {
        return pList;
    }
}

Ответы [ 2 ]

1 голос
/ 05 октября 2019
void setProduct(Product pList) {
    pList.add(this.pList);//This return method add undefined for type Product, how to solve this error?
}

должно быть

void addProduct(Product product) {
    pList.add(product);
}
0 голосов
/ 05 октября 2019

1 - Вы должны изменить свой конструктор, как показано ниже: _-

public Store(String newStoreName,ArrayList<Product> newPList) {
    this.storeName = newStoreName;
    pList.addAll(newPList);// This is standard and recommended way to add all element in list. 
}

2 - изменить метод setProduct. этот оператор не работает так.

void setProduct(Product pList) {
    pList.add(pList); 
}
...