The method add() in the type ArrayList<r> is applicable for the arguments(int,product)

HI, I am having trouble getting my arraylist to work. I am trying to pass an object from the main method through the ComputerOrder subclass to the "add" method in GenericOrder. I also want to have an id be associated with the product being passed to the add method and put in an arraylist. However, I keep getting the error message "The method add(int, GenericOrder) in the type ArrayList<GenericOrder> is not applicable for the
 arguments (int, Product)" In Eclipse. Any advice would be appreciated. Thanks.
class GenericOrder
{
	private static int OrderNumber = 1000;
	private final int id = OrderNumber ++;
	private ArrayList<GenericOrder> Product = new ArrayList<GenericOrder>();
    public void add(Product p){
    	Product.add(id,p); <--error message here.
    	
    	
    }
	
}
	
@SuppressWarnings("serial")
class ComputerOrder extends GenericOrder{
	
	@Override
	public void add(Product p) {
		if(p instanceof ComputerPart || p instanceof Peripheral || p instanceof Service) 
			super.add(p);
		else System.out.println("error");
		// TODO Auto-generated constructor stub
	}
	
}
 
@SuppressWarnings("serial")
class PartyTrayOrder extends GenericOrder{
	
	@Override
	public void add(Product p){
		if(p instanceof Cheese || p instanceof Fruit || p instanceof Service)
			super.add(p);
		 else System.out.println("error");
	}
 
}
 
class OrderProcessor{
	
	ArrayList<GenericOrder> products = new ArrayList<GenericOrder>();
	ArrayList<Peripheral> peripheral = new ArrayList<Peripheral>();
	ArrayList<Service> service = new ArrayList<Service>();
	ArrayList<Fruit> fruit = new ArrayList<Fruit>();
	ArrayList<Cheese> cheese = new ArrayList<Cheese>();
	
   public static void main(String[] args){
	   
   	Drive d = new Drive("Seagate",3,300);
   	Product p = d;
   	ComputerOrder order = new ComputerOrder();
   	order.add(p);

Open in new window

grnmachineAsked:
Who is Participating?
 
oleberConnect With a Mentor Commented:
Lets try to see what is happing

private ArrayList<GenericOrder> Product = new ArrayList<GenericOrder>();

You are creating a ArrayList that will just accept GenericOrder or sub class of GenericOrder objects. This seems wrong.



Your problem seems to be in the design to code.

You shall have a Base class for the products, for example Product. After each kind of product that you have shall derive from Product.

In the GenericOrder class you will have

private ArrayList<Product> products = new ArrayList<Product>();

now you can have your method

public void add(Product p){
        products.add(id,p);
}

Is this clear
0
 
cmalakarConnect With a Mentor Commented:
ArrayList is a data type to hold only list of objects..

It cannot store a pair of objects in each position...

You can use.. Map Collections which stores "key, value" pairs...
0
 
Ajay-SinghCommented:
Is Product a class?
0
 
cmalakarCommented:
>> I keep getting the error message "The method add(int, GenericOrder) in the type ArrayList<GenericOrder> is not applicable for the arguments (int, Product)

This error message is thrown, when your arraylist is declared for objects of one type and you are trying to add objects of another type.

ie., arraylist is declared to be a list of GenericOrder objects...

>> ArrayList<GenericOrder> Product = new ArrayList<GenericOrder>()

but you are trying to add an object of type "Product"

>> Product.add(id,p);

0
All Courses

From novice to tech pro — start learning today.