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
{
}
}
}
The problem I have is that the array is in the class GroceryList and I cannot add products to it. So make it so you can by reading about access modifiers ;)