Jump to content
  • Advertisement
Sign in to follow this  
Viscis

[java] Inventory Recursion

This topic is 2641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have:



switch(item.getType()) {
case empty:
inventoryList.add(new Item(Item.ItemType.empty, quantity)); break;
case demoItem:
inventoryList.add(new DemoItem(Item.ItemType.demoItem, quantity)); break;
case demoItem2:
inventoryList.add(new DemoItem2(Item.ItemType.demoItem, quantity)); break;
default: System.out.println("Please define a create case for new item: " + item.getType().toString() + "."); break;
}


Every time I extend my class item I have to add another case for that particular item type. How can I do this more recursively?

I've looked at the clone() and .getClass().getConstructor methods but can't seem to get the syntax quite right.

Share this post


Link to post
Share on other sites
Advertisement

why not just use a addItem method. You jsut have to creat your Item object on the outside and pass it in.


I'm already doing this, I pass in any Item or subclass of Item. The code then needs to copy the item or make a new item based on the items subclass constructor.



private int Create(Item item, int quantity) throws InventoryFullException {
if(inventoryList.size() < noSlots) {
switch(item.getType()) {
case empty:
inventoryList.add(new Item(Item.ItemType.empty, quantity)); break;
case demoItem:
inventoryList.add(new Item(Item.ItemType.demoItem, quantity)); break;
default: System.out.println("Please define a create case for new item: " + item.getType().toString() + "."); break;
}

if(quantity > item.getStackSize()) {
return item.getStackSize() - quantity;
}
else {
return 0;
}
}
else {
return quantity;
}
}

Share this post


Link to post
Share on other sites
If item is not a class hierarchy, then you don't need to switch. You can just pass item.getType() to the constructor of the new Item.

private int Create(Item item, int quantity) throws InventoryFullException {
if(inventoryList.size() < noSlots) {
inventoryList.add(new Item(item.getType(), quantity));

if(quantity > item.getStackSize()) {
return item.getStackSize() - quantity;
}
else {
return 0;
}
}
else {
return quantity;
}
}

If it is (or will be) a hierarchy, what trouble is the clone method causing you? Are you implementing Cloneable? If you want deep copy semantics, have you written a custom clone() implementation. Remember to use a covariant return type (public Item clone()) to avoid ugly casts elsewhere in your code.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!