0

can someone please help me... Im trying to display the items in the list on the console.

the current output is displaying my two sub classes: Grocery and FreshProduce. I have also added a image showing the current result below.

any help is much appreciated.

namespace ShoppingList
{
    class ShoppingList
    {
        static void Main(string[] args)
        {
            Grocery myGrocery = new Grocery("Bread", 1);
            FreshProduce myFreshProduce = new FreshProduce("Orange", 1);

            List<object> myShoppingList = new List<object>();
            myShoppingList.Add(myGrocery);
            myShoppingList.Add(myFreshProduce);

            PrintValues(myShoppingList, '\t');

        }

        public static void PrintValues(IEnumerable myList, char mySeparator)
        {
            foreach (Object obj in myList)
            Console.Write("{0}{1}", mySeparator, obj);
            Console.WriteLine();
        }


        public abstract class Product
        {
            protected string Name;
            protected int Quantity;
        }

        public class Grocery : Product
        {
            public Grocery(string groceryName, int groceryQuantity)
            {
                Name = groceryName;
                Quantity = groceryQuantity;
            }
        }
        public class FreshProduce : Product
        {
            public FreshProduce(string freshProduceName, int         freshProduceQuantity)
            {
                Name = freshProduceName;
                Quantity = freshProduceQuantity;
            } 

        }

    }
 }

Current result

Alucard777
  • 50
  • 7
  • Not addressing the issue necessarily, but seems to me like it would make more sense to have a common `Food` class or something that both `Grocery` and `FreshProduce` derive from and do `List` rather than doing `List` – Broots Waymb Jan 24 '19 at 18:27
  • Use string.Join() method : Console.WriteLine(string.Join(",", myList.Select(x => x.Name + ":" + x.Quantity.ToString()))); – jdweng Jan 24 '19 at 18:55

1 Answers1

1

If you need to print the details of the Product, you should override the ToString() method. You can do this by using the below implementation of the Product class

public abstract class Product {
    protected string Name;
    protected int Quantity;

    public override string ToString() {
        return $"Name = {Name}, Quantity = {Quantity}";
    }
}

While I am at it, you might as well make some other minor improvements to your code. See the full code below

namespace ShoppingList {
    class ShoppingList {
        static void Main(string[] args) {
            Grocery myGrocery = new Grocery("Bread", 1);
            FreshProduce myFreshProduce = new FreshProduce("Orange", 1);

            List<Product> myShoppingList = new List<Product>();
            myShoppingList.Add(myGrocery);
            myShoppingList.Add(myFreshProduce);

            PrintValues(myShoppingList, "\t");
        }

        // instead of IEnumerable, you should use IEnumerable<Product> for better type checking
        public static void PrintValues(IEnumerable<Product> myList, string mySeparator) {
            // string.Join does exactly what you are trying to do using a loop
            Console.WriteLine(string.Join(mySeparator, myList));
        }


        public abstract class Product {
            protected string Name;
            protected int Quantity;

            public override string ToString() {
                return $"Name = {Name}, Quantity = {Quantity}";
            }
        }

        public class Grocery : Product {
            public Grocery(string groceryName, int groceryQuantity) {
                Name = groceryName;
                Quantity = groceryQuantity;
            }
        }
        public class FreshProduce : Product {
            public FreshProduce(string freshProduceName, int freshProduceQuantity) {
                Name = freshProduceName;
                Quantity = freshProduceQuantity;
            }

        }

    }
}
Vikhram
  • 4,294
  • 1
  • 20
  • 32