Dylan Wolf
Dylan Wolf

Reputation: 1

Array in Classes C#

I am really new to C# and I am currently working on an assignment that doesnt really make sense to me. I need a kick in the right direction.

The assignment:

I need to make a console Application which is called grocerylist. This grocery list contains a class named GroceryList. This class contains an instance variable products which is an array.

The array contains items of the type product.

the class product has a string name and int quantity as attributes. The product class is abstract, and has 2 sub classes called fresh and herbs.

I need to show that I can add items to the GroceryList and display the list.

The problem I have is that the array is in the class GroceryList and I cannot add products to it. I am missing something, I have been pondering for weeks what it could be and tried different solutions without result. I am even wondering if the assignment is wrong. This is what I have so far:

namespace GroceryList
{
    class Program
    {
        static void Main(string[] args)
        {
            bool Displaymenu = true;
            while (Displaymenu == true)
            {
                Displaymenu = mainMenu();
            }


        }
        public bool mainMenu()
        {
            Console.WriteLine("What would you like to do?");
            Console.WriteLine("1) add a fresh product");
            Console.WriteLine("2) add a herb");
            Console.WriteLine("3) exit");

            string result = Console.ReadLine();

            if (result == "1");
            {
                Console.Clear();
                product.fresh fresh1 = new product.fresh();
                Console.WriteLine("What type of fresh product would you like to add?");
                fresh1.naam = Console.ReadLine();

                Console.WriteLine("How much of " + fresh1.naam + " would you like to add?");
                string number = Console.ReadLine();
                int quant;
                int.TryParse(number, out quant);
                fresh1.Quantity = quant;



                return true;
            }
        }
    }
    public class GroceryList
    {
        List<product> products = new List<product>();

    }
    abstract class product
    {
        public string naam { get; set; }
        public int Quantity { get; set; }

        public class fresh : product
        {

        }
        public class herbs : product
        {

        }
    }

}

Upvotes: 0

Views: 222

Answers (3)

motdotnet
motdotnet

Reputation: 14

Since you stated you are learning the language:

  1. You may want to consider adding an Add() method on the GroceryList class.
  2. Exposing the list as public is not a sound design because someone can set that to null or delete items etc.
  3. In very specific scenarios where you need to expose it, you can use ReadOnlyCollection.

Upvotes: 0

chandler
chandler

Reputation: 1150

You could make your GroceryList class have property of products.

public class GroceryList
{
    public GroceryList()
    {
        Products = new List<Product>();
    }
    public List<Product> Products { get; set; }
}

Then adding products might look like

var bananas = new Product {Name = "banana", Quantity = 2};
var list = new GroceryList();
list.Products.Add(bananas);  

Upvotes: 0

Patrick Bell
Patrick Bell

Reputation: 769

The GroceryList class that you created needs to be instantiated (declared) in your code so that you can use the List<product> inside of it. Exactly as you declared fresh1 as a product.fresh object, you must declare an object as a GroceryList.

So, you can declare GroceryList list = new GroceryList() at the beginning of your code. Then, you will want to add items to the products field of your GroceryList list.

There are two ways to do this. One way is to directly access the list object inside of the class.

To do this, you need to make your list field public so that it can be accessed from outside the object. To declare it as public, put a public keyword infront of the field declaration.

This isn't necessarily the best practice of doing this, as for a function like adding, it's better to have a clear method that does actions like this on fields instead of modifying them directly from outside of the object (if you want to learn more about this idea, it's called Encapsulation.) A useful method that you could use for this program would be a AddProduct method.

Now, what do we need this method to do? Add a product object to the list that we have inside of our GroceryList.

So, we need to declare the method inside of the GroceryList object as follows.

public void AddProduct(product productToAdd)
{
    products.Add(productToAdd);
}

Then, when you want to add a product to the GroceryList object, you would call AddProduct(productVariableHere) on the list object that you create.

Upvotes: 1

Related Questions