Witam.
Od razu zaznaczę ze w nazwach mogą występować błędy.
Mam problem z wyswietlaniem listy skladnikow.
Mam stworzoną listę Składników którą od razu inicjalizuje :
internal class Igredient
{
public string Name { get; set; }
public List<Igredient> ListIgredient { get; set; } = new List<Igredient>();
public void AddIdredient()
{
do
{
Console.WriteLine("Add Name of Idgredient");
string nameOfIdgredient = Console.ReadLine();
ListIgredient.Add(new Igredient { Name = nameOfIdgredient });
Program.youWannacalc = StringOperation.ReadParameter("If you wanna add next idgredient enter YES");
}
while (Program.youWannacalc == "YES");
}
public void ShowList(IEnumerable<Igredient> giveListIgrediens)
{
Console.WriteLine("----------------------------------");
Console.WriteLine("It is a list available ingredients");
foreach (var item in giveListIgrediens)
{
Console.WriteLine(item.Name);
}
}
Następnie w metodzie main chciałbym dodać składniki do listy. Mam obecnie coś takiego :
case 1:
var idredients = new Igredient();
idredients.AddIdredient();
idredients.ShowList(idredients.ListIgredient);
Gdy przechodzę Enumerable po liscie to wszystko ladnie sie wyswietla, jednak mam kilka problemów:
1. Wychodząc z metody niestety lista mi zanika. Nie ma już jak sie do niej odwołać jeszcze raz.
2 Chiałbym np mieć możliwość ponownego wejscia w metodę która dodaje mi składniki i dodaje do tej samej listy składniki ( w tej chwili inicjalizowana jest niestety na nowo).
3. Chciałbym w innej klasie skorzystać z tej listy ale do końca nie wiem jak : Mam narazie stworzone taką klasę :
class Pizza
{
private string Name { get; set; }
private decimal Price { get; set; }
public List<Pizza> Pizzas { get; set; } = new List<Pizza>();
public List<Sauce> Sauces { get; set; }
public List<Igredient> Igredients { get; set; }
public void AddPizza()
{
}
public void ShowAllPizzas(IEnumerable<Pizza> listPizzas)
{
Console.WriteLine("It is a list available pizzas");
foreach (var item in listPizzas)
{
Console.WriteLine($"Sauce {item.Name} for {item.Price} ");
}
}
}
Proszę o wytłumaczenie mi jak się to wszystko przypisuje.
Pozdrawiam