Hi,

I am working on the World of Zuul. I am adding the take item command, when I envoke it, the output prints twice!

The main class is Game, this is the take method. 

     public void takeItem(Command command)
     (
         if (!command.hasSecondWord())(
                // if there is no second word, we dont know which item to take
                System.out.println("Which Item 9otli ya mja9ji9?");
                return;
         )
         String itemToBePicked = command.getSecondWord();

         playerInventory.addItemToInventory(itemToBePicked);
         if (playerInventory.addItemToInventory(itemToBePicked))
         (
             currentRoom.removeItem(itemToBePicked);
         )         
         return;
     )

method in Inventory (instance playerInventory)

public boolean addItemToInventory(String itemToBePicked)
    (
        //checking the weight constraint

        if (canBePicked(itemToBePicked))
        (
            Item x = allItems.get(itemToBePicked);
            itemInventory.put(itemToBePicked, x);
            inventoryWeight = inventoryWeight + x.getItemWeight();
            System.out.println("You have added " + x.getItemName() + "to your inventory");
            return true;
        )
        else if ((game.checkAvailability(itemToBePicked)))
            (
                System.out.println("You have exceeded the maximum weight");
                return false;
            )
        else 
            (
            System.out.println( itemToBePicked + " is not present in the current room");
            return false;
        )

 public boolean canBePicked(String itemToBePicked)
        (
            Item x = allItems.get(itemToBePicked);
                int totalWeight = x.getItemWeight()+ inventoryWeight;

            if ((game.checkAvailability(itemToBePicked))||(totalWeight <= maxWeight))
            (         
                    return true;
            )
            else
            (
            return false;
            )

        )
    and the checkAvailability in Game:

public boolean checkAvailability(String itemToBePicked)
    (
        return currentRoom.checkItem(itemToBePicked);
    )
 and the method from Room (instance currentRoom)

public boolean checkItem(String itemToBePicked)
    )
        int n=items.size();
        for (int i=0 ; i<n ; i++){
                Item x = items.get(i);
                if ((x.getItemName()).equals(itemToBePicked))
                (
                    return true;
                )
            (
            return false;
            )

couldnt figure out why the output line always appears twice! like: 
you have added computer to your inventory
you have added computer to your inventory

any help is much appreciated!

Recommended Answers

couldnt figure out why the output line always appears twice! like:
you have added computer to your inventory
you have added computer to your inventory

My first guess would be the following code from takeItem():

     playerInventory.addItemToInventory(itemToBePicked); // line 16
     if (playerInventory.addItemToInventory(itemToBePicked)) // line …
Jump to Post

All 2 Replies

couldnt figure out why the output line always appears twice! like:
you have added computer to your inventory
you have added computer to your inventory

My first guess would be the following code from takeItem():

     playerInventory.addItemToInventory(itemToBePicked); // line 16
     if (playerInventory.addItemToInventory(itemToBePicked)) // line 17

Notice that addItemToInventory() is called twice in a row here.

i guess your right!

thank you for answering!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.