Pregunta

This here is the code for a class that modelizes a catalog for a computer parts shop using an ArrayList. All the included objects (products) have been defined, including methods for getting and setting a quantity variable.

import java.util.*;

public class AvailablesCatalog {

public AvailablesCatalog(List cat1) {

    Motherboard item1 = new Motherboard("MD4652", 1995, "Lenovo", 100.50,  "Intel", 32, 5, 0);
    CPU item2 = new CPU("RTJ357", 1850, "Intel", 182.50, 2.9, 6, 0);
    Graphics item3 = new Graphics("P99E0", 2014, "AMD", 70.50, "AMD", 6, 0);
    RAM item4 = new RAM("THN46", 1999, "Microsoft", 30.50, "DDR2", 4, 1600, 0);
    HD item5 = new HD("M9052", 2001, "LG", 100, "SSD", 2.5, 750, 0);
    Monitor item6 = new Monitor("D42", 2006, "LG", 200, "LED", 17.5, "1920x1080", "HDMI", 0);
    Keyboard item7 = new Keyboard("F16", 2010, "Microsoft", 25.70, "Wireless", 0);
    Mouse item8 = new Mouse("JERRY", 2010, "Microsoft", 30.50, "Laser", "Wireless", 0);
    Printer item9 = new Printer("PRNTR", 1995, "Lexmark", 40.50, "Laser", "Colored", 0);

    cat1.add(item1);    
    cat1.add(item2);   
    cat1.add(item3);   
    cat1.add(item4);
    cat1.add(item5);
    cat1.add(item6);   
    cat1.add(item7);   
    cat1.add(item8);
    cat1.add(item9);

}

public String toString(List cat1) {
    int flag = 0;
    for(int i=0; i<cat1.size(); i++) {
        if(cat1.get(i).getQuantity() != 0) {
            System.out.println(cat1.get(i).toString());
        }
        else {
            flag ++;
        }
    }
    if(flag == 9) {
        System.out.println("No products");
    }
    return "-------------------------------------------------------------------------";
}

}

As you can see, i'm trying to use the getQuantity() method in the AvailablesCatalog class. The problem is that when i try to compile i get a cannot find symbol error for the getQuantity method. Does that mean that i actually need to define the method in the catalog's class as well? In which case, how do i make it return each different product's quantity? Thanks in advance.

EDIT: since i didn't make that clear, all the product objects have a common Product superclass. Then, should i change toString(List cat1)

to

toString(List<Product> cat1)

?

EDIT 2: That was indeed the case after all, the only prblem was being that i was defining getQuantity() in all of my products, instead of the base class (Product) Once again, thanks everyone.

¿Fue útil?

Solución 4

You are not using a genericized List, so when you call get(i) on it, it returns an Object, and there is no getQuantity() method on Object. Try declaring your List as a List<Category> or whatever your base class for RAM, Motherboard, Keyboard etc is.

Otros consejos

because compiler doesn't know that your objects in List would be of type Catalog

change

List cat1

to

List<Catalog> cat1

in toString()

Assumming Catalog is your class containing this method and you have strictly all objects of this type in list

First of all, we do not understand what items you're adding to the list. What are the superclass of Motherboard, Graphics, CPU, Monitor, Keyboard, Mouse, etc.?

Since you're using a generic List without specifying a type, it is seen as a raw type and thus, the items in the list are implicitly an Object and Object doesn't have a getQuantity() method.

A solution will be to create an Item abstract, Serialized class that all the relevant subclass must inherit, i.e., Keyboard, Mouse, Motherboard, etc., and a List of these Items, i.e., List<Item> cat1.

Then your toString will have the following method signature:

public String toString(List<? extends Item> cat1)

I hope this helps.

You could use Product as a generic wildcard argument

public String toString(List<? extends Product> categoryList) {
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top