java - Designing an item inventory system for a game and having issues -


i've been working on little inventory system practice oo programming. using java 7 in netbeans.

the player have inventory (arraylist of base item class), items created child of base item class.

no items should of type baseitem, extend off of that, understanding, baseitem class should abstract?

public abstract class baseitem {     protected goldvalue itemvalue;     protected string itemname;      //accessors , mutators down here } 

child item types have different properties, going implement interfaces, such stackable, consumable, etc.

the interface looks this

public interface stackable {         public void stack (stackableitem bi1);  } 

as can see, make reference stackableitem here, 1 of children of baseitem, abstract , specific items build out of.

public abstract class stackableitem extends baseitem implements stackable{      protected int quantity;     protected int maxstacks;      @override     public void stack (stackableitem si)     {         if(this.getquantity() + si.getquantity() < maxstacks){             this.setquantity(this.getquantity()+si.getquantity());             si.setquantity(0);         }         else if(this.getquantity() + si.getquantity() > maxstacks){             int diff = maxstacks - this.getquantity();             this.setquantity(this.getquantity() + diff);             si.setquantity(si.getquantity() - diff);         }     } } 

and here's example of specific item:

public class stackableitemexample extends stackableitem{      public stackableitemexample ()throws goldexception     {         this(new goldvalue(0,0,0), "unnamed", 1);     }     public stackableitemexample(goldvalue gv, string name, int quan) throws goldexception     {         this.itemvalue = gv;         this.itemname = name;         this.quantity = quan;         this.maxstacks = 10;         this.itemvalue.setgoldvalue(gv.getgold()*quan, gv.getsilver()*quan, gv.getcopper()*quan);     }   } 

my inventory, being arraylist of baseitem objects, allowed have stackableitemexample object inserted no problem. issue is, inventory system cannot baseitem.stack() since parent has no knowledge of method.

i want make extendable item system, new item can created extreme simplicity implementing , extending pre-created classes , interfaces, getting in way.

no items should of type baseitem, extend off of that, understanding, baseitem class should abstract?

sounds me


if want baseitem.stack() work, @ least 2 things:

  • move stack() base class, , give default implementation of doing nothing. override in stackableitem class, or
  • use instanceof see whether baseitem stackableitem, cast stackableitem , go there.

the second approach like:

baseitem item = yourlist.get(i); if(item instanceof stackableitem){     stackableitem stackable = (stackableitem)item;     stackable.stack()  /* works without issue */ } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -