Results 1 to 9 of 9
  1. #1
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default RPG Inventory problem

    Hello

    I am trying to create items that can be added to an inventory during an rpg game. I want these items to actually be objects that have a name and a weight. I have been trying to create my first item which is a shield and I just can't figure out how to do this. Here is the code for my Inventory class and my Shield class.

    Java Code:
    package inventory;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Inventory {
    	private short maxWeight = 100;
    	private short currentWeight = 0;
    	List<String> list = new ArrayList<String>();
    	
    	public void addItem(String item, int weight){
    		if(weight+currentWeight>maxWeight){
    			System.out.println("You can not add that item " +
    					"because your bag is full");
    		}else{
    			list.add(item);
    			currentWeight+=weight;
    			System.out.println(item+" has been added to your bag");
    		}
    	}
    	
    	public void removeItem(String item, int weight){
    		list.remove(item);
    		currentWeight-=weight;
    	}
    			
    	public void printBag(){
    		System.out.println("Items in bag:");
    		System.out.printf("%s\n", list);
    		System.out.println("Weight = "+currentWeight);
    	}
    	
    	public static void main(String[] args){
    		Inventory bag = new Inventory();
    		bag.addItem("Sword", 25);
    		bag.printBag();
    		bag.addItem("Shield", 20);
    		bag.printBag();
    		bag.addItem("Cuirass", 40);
    		bag.printBag();
    		bag.removeItem("Cuirass", 40);
    		bag.printBag();
    	}
    }
    Java Code:
    package inventory;
    
    public class Shield extends Items {
    	private String item = null;
    	private byte weight;
    	byte[] array = new byte[2];
    	
    
    	public Shield(){
    		item = "Shield";
    		weight = 25;
    		array = buildItem(item, weight);
    	}
    	
    	private byte[] buildItem(String item, byte weight) {
    		array[0]=item;
    		array[1]=weight;
    		return array;
    	}
    }
    As you will see from my Inventory class, I am just passing in a String and an int for the addItem method, but ideally I would like to pass in an objectand for the addItem method to know the objects name and weight.
    Last edited by JohnPringle83; 05-12-2011 at 04:52 PM. Reason: Broken code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Myself, I'd change "Items" to Item and make it an interface, perhaps with methods getWeight() and getName(). I'd then have Shield implement this interface. I'm not sure why Shield has the byte array field. Can you explain more the purpose of this? Also, does this even compile?

    Java Code:
    byte item = "Shield";

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Have you looked at making an array list of items?

    Java Code:
    ArrayList<Items> List = new ArrayList<Items>();
    Then it will hold your shield's object reference instead of just a string.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    I did notice that shield had a byte field and I did change the code to correct this. And no, it doesn't compile. I will attempt this interface idea and get back if I have any issues.

  5. #5
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Question A new problem...

    I have used Item as an interface for the items that I create. However, when I try to remove the item from the bag it does not remove. I have a feeling it is something to with trying to remove a different instance rather than the same one. How can I solve this?

    Also, is there another way of creating the items so that I don't have to create a separate class for each item?

    Should anything go in the constructor of the inventory class?

    Java Code:
    public interface Item {
    	String getName();
    	byte getWeight();
    }
    Java Code:
    public class Sword implements Item {
    	private String name = getClass().getName();
    	private byte weight = 25;
    	
    	public String getName() {
    		return name;
    	}
    
    	public byte getWeight() {
    		return weight;
    	}
    	
    	public static void main(String[] args){
    		Sword item = new Sword();
    		System.out.println(item.name);
    		System.out.println(item.weight);
    	}
    }
    Java Code:
    public class Shield implements Item {
    	private String name = getClass().getName();
    	private byte weight = 25;
    	
    	public String getName() {
    		return name;
    	}
    
    	public byte getWeight() {
    		return weight;
    	}
    	
    	public static void main(String[] args){
    		Shield item = new Shield();
    		System.out.println(item.name);
    		System.out.println(item.weight);
    	}
    }
    Java Code:
    import java.util.ArrayList;
    
    public class Inventory {
    	
    	private short maxWeight = 100;
    	private short currentWeight = 0;
    	ArrayList<Item> list = new ArrayList<Item>();
    	
    	public Inventory(){
    		
    	}
    	
    	public void addItem(Item x){
    		if(x.getWeight()+currentWeight<=maxWeight){
    			list.add(x);
    			currentWeight += x.getWeight();
    			System.out.println(x.getName()+" has been added to your bag!");
    		}else{
    			System.out.println("You can't carry that item!");
    		}
    	}
    	
    	public void removeItem(Item x){
    		list.remove(x);
    		currentWeight -= x.getWeight();
    		System.out.println(x.getName()+" has been removed from your bag!");
    	}
    	
    	public void printBag(){
    		System.out.println("The contents of your bag are:");
    		for(Item x: list){
    			System.out.printf("%s %d", x.getName(), x.getWeight());
    		}
    	}
    	
    	public static void main(String[] args){
    		Inventory bag = new Inventory();
    		bag.addItem(new Sword());
    		bag.removeItem(new Sword());
    		bag.printBag();
    	}
    }
    Last edited by JohnPringle83; 05-12-2011 at 11:32 PM. Reason: Spelling mistake

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JohnPringle83 View Post
    I have used Item as an interface for the items that I create. However, when I try to remove the item from the bag it does not remove. I have a feeling it is something to with trying to remove a different instance father than the same one. How can I solve this?
    Do all of your classes override the equals() and hashCode() methods of Object? If not, you will want to do this if you are going to have collections such as ArrayLists, HashSets and HashMaps understand which objects are equivalent to another object.


    Also, is there another way of creating the items so that I don't have to create a separate class for each item?
    Sure, just make Item a class instead of an interface. What you are doing where each class is the same except for the name field which is derived from the class name is very skanky and suggests that you should do this simplification. You want to create different classes mainly if you are going to change the behavior of the base class or change the possible states available to the class, neither of which you need based on the code you've posted.

  7. #7
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default Now I'm really confused...

    You said earlier to make Item into an interface and now you are saying to make it into a class.

    Making Item into an interface achieved the result I was hoping for and if I change it to a class, I don't think that I will get the same result. perhaps I could create a superclass for the items instead???

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JohnPringle83 View Post
    You said earlier to make Item into an interface and now you are saying to make it into a class.
    Your requirement have changed and become more specific, so it's only natural for the recommendations to change.


    Making Item into an interface achieved the result I was hoping for and if I change it to a class, I don't think that I will get the same result. perhaps I could create a superclass for the items instead???
    Perhaps you need to sit and fully think through your requirements more before doing any more coding or designing. If all a Sword is, is something with a name and a weight, and nothing else, if in this regard it is no different than say a Toaster, then creating a simple Item class with a name and weight field will suffice. If you want the Sword however to do Sword-like things, for instance to be sheathed or un-sheathed, to be usable as a weapon (and thus have different states and behaviors than the Toaster), then you're probably going to go the interface route with the idea of "Item"-ness being only what information is necessary for the sack that contains them. But the key to all of this is to think through your requirements first before doing any coding or design. The minute you change your requirements, your code will need to change.

  9. #9
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    Eventually my sword will be doing some sword-like things, but for now I am just trying to get the Inventory class working.

    For sitting down and thinking about what I'm doing..., I'm still new to the language so I am just playing around with it really to see what happens when I do certain things. Eventually when I get enough knowledge of Java then I will be able to really think about the best ways to achieve my desired results.

    I would like to say though Fubarable that you are helping me so much in my learning process and you're always illicitly pointing me towards what I should learn about next.

    I did want to give you some rep for your most recent reply, but I need to spread some around first before I give you some more.

Similar Threads

  1. RPG inventory Problem
    By JohnPringle83 in forum New To Java
    Replies: 8
    Last Post: 05-08-2011, 03:12 AM
  2. Re: Help doing GUI in sales and inventory
    By cuddles14 in forum Eclipse
    Replies: 0
    Last Post: 03-08-2011, 12:53 AM
  3. My Inventory Program
    By ladykrimson in forum New To Java
    Replies: 28
    Last Post: 11-01-2010, 09:06 PM
  4. Inventory Program
    By tlouvierre in forum New To Java
    Replies: 5
    Last Post: 05-17-2009, 05:09 AM
  5. Inventory part 2 help please
    By badness in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •