Question

My problem is that I have a List in one class "Menu" containing objects of the class "Pizza". I want to access this list in a third class "Order". I am doing it in console.

class Menu
{
    public List<Pizza> pizzaOnMenu = new List<Pizza>();

    public void CreateElementToMenu()
    { 
        pizzaOnMenu.Add(new Pizza(1, "Magarita", "Tomato, cheese", 49));
        pizzaOnMenu.Add(new Pizza(2, "Hawaii", "Tomato, cheese, ham, pineapple", 55));
        pizzaOnMenu.Add(new Pizza(3, "Cappriciossa", "Tomato, cheese, ham, mushroom", 55));
        pizzaOnMenu.Add(new Pizza(4, "Special", "Tomato, cheese, beef", 60));
    }
}

I have also tried to make this as well

public List<Pizza> pizzaOnMenu(){ get; set; }

Anyway - the method is called from Main():

Menu menu = new Menu();
menu.CreateElementToMenu();

And then I try to access the list in "Order" to check if the pizza exist on the list in "Menu":

class Order
{

    private List<Pizza> orderList = new List<Pizza>();

    public void CreateOrder()
    {
        Console.WriteLine("\nWhich number from the menu do you wish to order?");
        int orderNo = int.Parse(Console.ReadLine());

        Menu menu = new Menu();
        Pizza wantedPizza = null;

        foreach (Pizza p in menu.pizzaOnMenu)
        {
            if (p.Nr == orderNo)
            {
                wantedPizza = p;
                break;
            }
         }

        if (wantedPizza != null)
        {
            orderList.Add(wantedPizza);
            Console.WriteLine("\n" + wantedPizza.PizzaName + " is added to your order");
        }    
        else
        {
            Console.WriteLine("\nThe pizza with no. " + orderNo + " was not found!");
        }
    }

However, when the method CreateOrder() is run from main and I e.g. choose no. 2 from the menu, it writes "...was not found" instead of "...is added to your order" - it seems that the list (from "Order") is empty. But I am able to print out the list created from Menu :S (if that makes sense?)

Thanks in advance.

Was it helpful?

Solution

You probably want to move calling the initialization to constructor of the Menu class, providing that Menukort and Menu are same classes:

class Menu
{
    public Menu()
    {
        CreateElementToMenu();
    }

    public List<Pizza> pizzaOnMenu = new List<Pizza>();

    public void CreateElementToMenu()
    { 
        pizzaOnMenu.Add(new Pizza(1, "Magarita", "Tomato, cheese", 49));
        pizzaOnMenu.Add(new Pizza(2, "Hawaii", "Tomato, cheese, ham, pineapple", 55));
        pizzaOnMenu.Add(new Pizza(3, "Cappriciossa", "Tomato, cheese, ham, mushroom", 55));
        pizzaOnMenu.Add(new Pizza(4, "Special", "Tomato, cheese, beef", 60));
    }
}

OTHER TIPS

Look at the foreach loop over the contents of menukort.pizzaOnMenu: I don't see a closing brace (}) where the indentation would indicate it is expected (immediately after the closing brace of the if.

Thus the if (wantedPizza != null) is checked for every possible pizza.

If this is just a cut and paste error when creating the question: edit the question having checked the code you copied in is a true copy, and then make the correction clear.

Your order class use Menukort object instead of Menu what i don't understand why

You have public void CreateElementToMenu() in Menu class.

Perhaps you should use this code in your Order class

Menu menu = new Menu();
menu.CreateElementToMenu();

use public static class intsead of public class

Just another approach to make it a little more SOLID - see http://en.wikipedia.org/wiki/SOLID_(object-oriented_design:

public class OrderDataEntry
{
    public static void TakeOrder()
    {
        Console.Write("\nWhich number from the menu do you wish to order?");
        int orderNo = int.Parse(Console.ReadLine());

        Menu menukort = new Menu();
        Pizza wantedPizza = menukort.pizzaOnMenu.FirstOrDefault<Pizza>(p => p.OrderNumber.Equals(orderNo));

        if (wantedPizza != null)
        {
            Console.WriteLine("\n" + wantedPizza.PizzaName + " is added to your order");
            Order o = new Order(wantedPizza);
        }
        else
        {
            Console.WriteLine("\nThe pizza with no. " + orderNo + " was not found!");
        }
    }
}

public class Order
{
    private List<Pizza> orderList; 

    public Order(Pizza wantedPizza)
    {
        orderList = new List<Pizza>();
        AddToOrder(wantedPizza);
    }

    public void AddToOrder(Pizza wantedPizza)
    {
        orderList.Add(wantedPizza);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top