I'm trying to add a new object to an ArrayList. Each Item object will have 3 attributes:
I also have 3 classes:
I have the sets and gets in Item class and I have the ArrayList in Catalog. In Client, I will have options to add, remove, or edit the objects. How do I correctly add a new Item object to the ArrayList?
I get the Item class to compile fine, but the Catalog and Client classes are not compiling. Here's the error I get with Catalog class:
Catalog.java:35: error: no suitable method found for add(int,String,double)listOfObjects.add(newItemId, newDescription, newCost);
method ArrayList.add(int,Item) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(Item) is not applicable
(actual and formal argument lists differ in length)
Below is code for Item class
Public class Item
{
private int itemNum;
private String info;
private double cost;
public Item()
{ //start constructor
itemNum = 0; //default values
info = "x";
cost = 0;
} //end constructor
public CatalogItem(int newItemNum, String newInfo, double newCost)
{ //start overload constructor
this.itemNum = newItemNum;
this.info = newInfo;
this.cost = newCost;
} //end overload constructor
below are the set/gets for itemNum
public int getItemNum()
{ //start itemNum accessor
return itemNum;
} //end getItemNum
public void setItemNum(int newItemNum)
{ //start itemNum mutator
this.itemNum = newItemNum;
} //end setItemNum
} //end Item class
//below is my Catalog Class
import java.util.*;
public class Catalog
{ //start class
private ArrayList<CatalogItem> listOfObjects = new ArrayList<CatalogItem>(100); //creates ArrayList
Item newItem = new Item(newItemNum, newInfo, newCost); //instantiates Item class
/*
public Catalog()
{ //start constructor
} //end constructor
*/
public void add(CatalogItem newItem) //method adds a new Item object to the array list
{ //start add
listOfObjects.add(newItem);
} //end add
public void add(int itemNum, String info, double cost) //accepts parameters from main method to add to new object
{ //start add
int newItemNum = itemNum;
String newInfo = info;
double newCost = cost;
newItem.setItemNum(newItemNum);
newItem.setInfo(newInfo);
newItem.setCost(newCost);
listOfObjects.add(newItemNum, newInfo, newCost);
} //end add
} //end class
below is Client class. It receives input from the user regarding the itemNum, info, and cost
import java.util.*; //allows use of Scanner class
public class Client
{ //start client class
public static void main(String[] args)
{ //start main
Catalog serv = new Catalog(); //creates instance of Catalog class
Scanner scan = new Scanner(System.in); //creates instance of Scanner class called scan
public void add(int itemNum, String info, double cost) //accepts parameters from main method to add to new object
{ //start add
int newItemNum = itemNum;
String newInfo = info;
double newCost = cost;
newItem.setItemNum(newItemNum);
newItem.setInfo(newInfo);
newItem.setCost(newCost);
listOfObjects.add(newItemNum, newInfo, newCost); //adds the object to the ArrayList
} //end add
}
Any help would be greatly appreciated.
You have some errors...
In your class Catalog, this line is not correct:
listOfObjects.add(newItemNum, newInfo, newCost); //adds the object to the ArrayList
you must do:
CatalogItem cat = new CatalogItem(newItemNum, newInfo, newCost);
listOfObjects.add(cat); //adds the object to the ArrayList
And in your class Client, you have not reference to newItem nor listOfObjects, so you cannot use them in that class. If you want to add elements to listOfObjects from your Client's main method, you can do something like this:
public class Client {
public static void main(String[] args) { //start main
Catalog serv = new Catalog();
......
serv.add(1, "", 1.0d);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.