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

    Default Problem adding item to Vector<InventoryItem>

    I am having difficult again with an Inventory that I am trying to create for an RPG. I can not find a way to add the item to the inventory.
    I know that my Inventory class works, but I just can't seem to get the items from the Item class to add to the inventory.

    Here is my code:
    I have commented out the part I'm trying to get to work

    Java Code:
    import java.util.Iterator;
    import java.util.Vector;
    
    public class Inventory {
    	short currentWeight = 0;
    	short maxWeight = 100;
    	Vector<InventoryItem> list;
    	Item item = new Item();
    	
    	public Inventory(){
    		list = new Vector<InventoryItem>();
    	}
    	
    	void addItem(InventoryItem item){
    		if((currentWeight+item.weight)<=maxWeight){
    			list.add(item);
    			currentWeight+=item.weight;
    			System.out.printf("%s has been added to your inventory", item.name);
    		}else{
    			System.out.println("You can not carry this item");
    		}
    	}
    	
    	void removeItem(InventoryItem item){
    		list.remove(item);
    		currentWeight-=item.weight;
    	}
    	
    	void listItems(){
    		System.out.println("The items in your bag are:");
    		Iterator<InventoryItem> it = list.iterator();
    		while(it.hasNext()){
    			System.out.printf("%s", list.get(0)); //Not sure if this will have the desired effect of printing out the name of the item
    		}
    		System.out.println("Encumberence: "+currentWeight+"/"+maxWeight);
    	}
    		
    	public static void main(String[] args){
    		Inventory bag = new Inventory();
    		//bag.addItem();
    		//bag.removeItem();
    		bag.listItems();
    	}
    }
    Java Code:
    public class InventoryItem {
    	String name;
    	int weight;
    	
    	public InventoryItem(String name, int weight){
    		this.name = name;
    		this.weight = weight;
    	}
    }
    
    class Item{
    	InventoryItem sword;
    	InventoryItem shield;
    	InventoryItem helmet;
    	InventoryItem cuirass;
    	InventoryItem greaves;
    	InventoryItem boots;
    	InventoryItem gauntlets;
    	public Item(){
    		sword = new InventoryItem("sword", 20);
    		shield = new InventoryItem("shield", 15);
    		helmet = new InventoryItem("helmet", 5);
    		cuirass = new InventoryItem("cuirass", 25);
    		greaves = new InventoryItem("greaves", 20);
    		boots = new InventoryItem("boots", 5);
    		gauntlets = new InventoryItem("gauntlets", 20);
    	}
    }
    Last edited by JohnPringle83; 05-15-2011 at 11:11 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I just can't seem to get the items from the Item class to add to the inventory.
    Are you getting an error?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You commented out the add item method. Also, addItem takes an InventoryItem as an argument.

    You also will need to instantiate an instance of Item to access it's instance variables(or make them constants(static final)).
    Last edited by sunde887; 05-15-2011 at 11:53 PM.

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

    Default

    I did instantiate an instance of Item at the top of the class along with my variable declarations. I thought this would be enough but I changed it to be within my main method like this:

    Java Code:
    public static void main(String[] args){
    	Inventory bag = new Inventory();
    	Item item = new Item();
    	bag.addItem(item.sword);
    	bag.addItem(item.shield);
    	bag.removeItem(item.shield);
    	bag.listItems();
    }
    However, when I run the program, I get:

    InventoryItem@7d2152e6InventoryItem@7d2152e6Invent oryItem@7d2152e6InventoryItem@7d2152e6InventoryIte m@7d2152e6

    and this goes on and on until I terminate the program.

    I have a feeling it is to do with the while loop in the listItems Method but I can't seem to figure out how to stop it.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You should consider just using a for each loop and allow it to handle the iterator for you. Also, override toString so it prints a name instead of the gobbledygook.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Obviously the loop that prints the output is looping forever. What is supposed to stop the loop? Read about how to use an Interator.

    Also the get(0) always returns the SAME object.

    Another item: Add an "\n" at the end of the printed output instead of printing everything on the same line

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

    Default Finally it works :)

    ok, I have added a toString method to my InventoryItem class which just returns the name of the item.

    I have also changed my listItems Method to this:
    Java Code:
    void listItems(){
    		System.out.println("The items in your bag are:");
    		for(Iterator<InventoryItem> it = list.iterator(); it.hasNext();){
    			String output = it.next().toString();
    			System.out.println(output);
    		}
    		System.out.println("Encumberence: "+currentWeight+"/"+maxWeight);
    	}
    and now I get the desired output:

    sword has been added to your inventory
    helmet has been added to your inventory
    shield has been added to your inventory
    cuirass has been added to your inventory
    cuirass has been removed from your inventory
    The items in your bag are:
    sword
    helmet
    shield
    Encumberence: 40/100

    I also added \n where needed, thanks for the tip norm

    Thanks for your help.

Similar Threads

  1. Adding an item to arrayList at a specific index
    By randoms:) in forum New To Java
    Replies: 2
    Last Post: 05-11-2011, 04:25 PM
  2. Help required with complex method: adding item
    By nignogs in forum New To Java
    Replies: 14
    Last Post: 05-11-2011, 01:06 PM
  3. NullPointer adding an item to a LinkedList
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-10-2011, 03:39 AM
  4. Vector empty item?
    By libia in forum Java Applets
    Replies: 1
    Last Post: 12-07-2010, 11:20 AM
  5. Adding a double element to a vector
    By peachyco in forum New To Java
    Replies: 5
    Last Post: 11-25-2007, 06:07 PM

Posting Permissions

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