Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically, this is a uni project... But ive been stuck on it for hours all day long...

All i want is the console to print out a the items in the list instead i get this.


SimpleTextAdventure.Inventory

SimpleTextAdventure is the name of the project itself and the inventory is the name of one of the classes.

here is the code for all classes.

C#
namespace SimpleTextAdventure
{
    public class PlayerItemInventory
    {
        // Attributes
        private static List<Inventory> _inventory = new List<Inventory>();

        // Constructors

        // Properties

        // Methods
        public static void AddItem(string item)
        {
            _inventory.Add(new Inventory(item));

        }

        public static void PrintItems()
        {
            foreach (Inventory itemName in _inventory)
            {
                Console.WriteLine(itemName);
            }
        }

        public static void RemoveItem(string itemName)
        {
            for (int i = 0; i < _inventory.Count; i++)
            {
                if (_inventory[i].ItemName == itemName)
                {
                    _inventory.Remove(_inventory[i]);
                    break;
                }
            }
        }

        public static bool HasItem(string itemName)
        {
            foreach (Inventory item in _inventory)
            {
                if (item.ItemName == itemName)
                {
                    return true;
                }
            }

            return false;
        }

        public static int CountItemsFromInventory(string inventory)
        {
            int count = 0;

            foreach (Inventory item in _inventory)
            {
                if (item.ItemName == inventory)
                {
                    count++;
                }
            }

            return count;
        }
    }
}












C#
class Inventory
    {
        // Attributes
        string _itemName;

        // Constructors

        public Inventory(string itemName)
        {
            _itemName = ItemName;
        }

        // Properties
        public string ItemName { get { return _itemName; } }

        // Methods


    }
}









C#
public class RoomThree:Room
    {
        // Attributes
        public static string[] _availableItems = new string[] { "torch", "shield", "sword" };


        public static ChatLine[] _lines = new ChatLine[]
        {
           // new ChatLine("START", "Welcome to Room 3.. There are doors to the #DIRECTIONS#.", "move+?", GameEngine.Instance.PlayerMove),
            new ChatLine("START", "Welcome to Room 3. There is a table with weapons and a phone ringing on it. " +
                "I would suggest you answer it", "answer, answer phone", AnswerPhone),
            new ChatLine("TABLE", "There are items on the table", "look, look table", LookTable),
            new ChatLine("FOUND_ITEMS", "You looked at the table and there #AVAILABLE_ITEMS#.", "", PlayerSelectsItem),
            new ChatLine("MOVE_OR_PICK", "Move towards the North door, or pick another item, there #AVAILABLE_ITEMS#", "pick+?", PlayerSelectsItem),
            new ChatLine("MOVE_OR_PICK", "Move towards the North doorm or pick another item, there #AVAILABLE_ITEMS#", "move+?", GameEngine.Instance.PlayerMove),


            new ChatLine("", "", "pick+?, select+?, choose+?", PlayerSelectsItem),
            new ChatLine("", "", "drop+?", PlayerDropsItem),
            new ChatLine("", "", "move+?", GameEngine.Instance.PlayerMove)

        };

        // Constructors

        public RoomThree()
            : base("ROOM_THREE", _lines,  "START", _availableItems)
        {
        }

        // Methods


        private static string AnswerPhone(string word, object[] args)
        {
            Console.WriteLine();
            Console.WriteLine("Hello human. My name is Sauron. I am the leader of this race. You have became a problem for me," +
                " and for my race. But I see potential in you. I see a great warrior in you.. So I will offer you this deal...." +
                "If you can get to me, I will offer you a commander role leading my army against the demons. But it will be tough, you" +
                " will have to face very experienced fighting orcs and you will have to find your way to me.. But IF you SURVIVE." +
                " I will make you my second in command. Now there are weapons on the table in front of you, and an orc in the other room." +
                " Good luck. You will need it.");

            Console.WriteLine();

            return "TABLE";
        }

        private static string LookTable(string word, object[] args)
        {
            Console.WriteLine();

            return "FOUND_ITEMS";
        }

        public static string PlayerSelectsItem(string word, object[] args)
        {
            GameEngine.Instance.RemoveItemFromRoom(word);
            PlayerItemInventory.AddItem(word);
            PlayerItemInventory.PrintItems();
            Console.WriteLine();
            return "MOVE_OR_PICK";  // Don't change action
        }

        public static string PlayerDropsItem(string word, object[] args)
        {
            if (PlayerItemInventory.HasItem(word))
            {
                PlayerItemInventory.RemoveItem(word);
                GameEngine.Instance.DropItemInRoom(word);
                Console.WriteLine("Item has been dropped in the room");
            }
            else
            {
                Console.WriteLine("You don't have that item");
            }

            return "";  // Don't change action
        }
    }
}





To just expand, instead of printing of the list items like it should. it prints out namespace.class name which in this case is SimpleTextAdventure.Inventory...




HELP!!!!!
Posted

you are iterating through _inventory list of Inventory Items. You are calling each one 'itemName' but it isn't the item name. so each itemName is really an Inventory object so you are getting the '.ToString()' of the object not the ItemName value which you want.

make the change below.

C#
public static void PrintItems()
        {
            foreach (Inventory itemName in _inventory)
            {
                Console.WriteLine(itemName.ItemName);
            }
        }
 
Share this answer
 
You have to provide member name you want to print. In your loop what you named as "itemName" is the instance of inventory class. Name you chose is misleading.


C#
public static void PrintItems()
   {
       foreach (Inventory itemName in _inventory)
       {
           Console.WriteLine(itemName.ItemName);
       }
   }



Another way (without changing your loop when you print to console) is to override ToString() method in inventory class

C#
public override string ToString()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900