I'm very new to object orientated programming and was wondering if it's possible to pass an int to a class that is contained by another class. My code compiles but when I run printList in my main the itemID is never printed.
Here's my ArrayList class
import java.util.ArrayList;
public class ItemList{
int itemListID
String itemName;
ArrayList<Item> itemList = new ArrayList<>();
public ItemList(String name, int listID) {
itemName = name;
itemListID = listID
}
public void addItem(String name, double weight, double price){
itemList.add(new Item(name,weight,price));
}
public int getItemListID(){
return itemListID;
}
public void printItems(){
for(int i=0; i < itemList.size(); i++){
System.out.println(itemList.get(i));
}
}
}
Here's the class of items contained within the arrayList
public class Item {
String name;
double weight;
double price;
int itemListID;
public Item(String itemName,double itemWeight, double itemPrice){
this.name = itemName;
this.weight = itemWeight;
this.price = itemPrice;
}
public int setItemListID(){
itemListID = ItemList.getItemListID();
}
public String toString(){
return(name + " has a weight of " + weight + " and a price of " + price + "item is in itemlist"
+ itemListID);
}
}
Here's the main
public class HopefulShopMain{
public static void main (String[] args){
ItemList myList = new ItemList("Fruit",1);
myList.addItem("Apple", 100, 60);
myList.addItem("Banana", 118, 50);
myList.printItems();
}
}
was wondering if it's possible to pass an int to a class that is contained by another class
Yes. This can be done at various levels (constructor, or method), and in different ways.
In your example, you could pass the itemListID into the Item's constructor:
public void addItem(String name, double weight, double price){
itemList.add(new Item(name,weight,price,this.itemListID));
}
Then your Item class can keep that value in a field just like it does with the other values it's getting in its constructor.
public class Item {
String name;
double weight;
double price;
int itemListID;
public Item(String itemName,double itemWeight, double itemPrice, int itemListID){
this.name = itemName;
this.weight = itemWeight;
this.price = itemPrice;
this.itemListID = itemListID;
}
public String toString(){
return(name + " has a weight of " + weight + " and a price of " + price + "item is in itemlist"
+ itemListID);
}
}
Alternatively, you could pass the entire ItemList
into the constructor so it can refer to any value that ItemList
has on it
Or you can make Item
a (non-static) nested class inside of ItemList
, so it can simply reference the field directly. This might be appropriate in your case, since it appears you only expect Item
to be created and accessed by the ItemList
class itself.
import java.util.ArrayList;
public class ItemList{
int itemListID
String itemName;
ArrayList<Item> itemList = new ArrayList<>();
public ItemList(String name, int listID) {
itemName = name;
itemListID = listID
}
public void addItem(String name, double weight, double price){
itemList.add(new Item(name,weight,price));
}
public int getItemListID(){
return itemListID;
}
public void printItems(){
for(int i=0; i < itemList.size(); i++){
System.out.println(itemList.get(i));
}
}
class Item {
String name;
double weight;
double price;
public Item(String itemName,double itemWeight, double itemPrice){
this.name = itemName;
this.weight = itemWeight;
this.price = itemPrice;
}
public String toString(){
return(name + " has a weight of " + weight + " and a price of " + price + "item is in itemlist"
+ ItemList.this.itemListID);
}
}
}
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.