I am fairly new to C# and programming in general and I am trying to make the logic to build a pizza.
Is there a function or a more efficient way that I can add multiple elements to a list in one line. For example, to build a 5 topping pizza right now I do this:
Toppings.Add(top1);
Toppings.Add(top2);
Toppings.Add(top3);
Toppings.Add(top4);
Toppings.Add(top5);
Is there a better way of doing this?
Also any suggestions on improving my code below would be greatly appreciated.
Thank you for your help in advance!
using System.Collections.Generic;
using ZePizzaria.Domain.Enums;
namespace ZePizzaria.Domain.Models
{
public class Pizza
{
public static Dictionary<EPizzaSize, double> Cost = new Dictionary<EPizzaSize, double>
{
{EPizzaSize.Mega, 25},
{EPizzaSize.XLarge, 20},
{EPizzaSize.Large, 15},
{EPizzaSize.Medium, 10},
{EPizzaSize.Small, 8.99}
};
public int PizzaId { get; set; }
public ECrust Crust { get; set; }
public double Price { get; set; }
public EPizzaSize Size { get; set; }
public List<EToppings> Toppings { get; set; }
public Pizza()
{
Crust = ECrust.Regular;
Price = Cost[EPizzaSize.Medium];
Size = EPizzaSize.Medium;
Toppings = new List<EToppings>
{
EToppings.Cheese,
EToppings.Sauce
};
}
public Pizza(ECrust crust, EPizzaSize size, EToppings top1)
: this()
{
Crust = crust;
Price = Cost[size];
Size = size;
Toppings.Add(top1);
}
public Pizza(ECrust crust, EPizzaSize size, EToppings top1, EToppings top2)
: this()
{
Crust = crust;
Size = size;
Price = Cost[size];
Toppings.Add(top1);
Toppings.Add(top2);
}
public Pizza(ECrust crust, EPizzaSize size, EToppings top1, EToppings top2, EToppings top3)
: this()
{
Crust = crust;
Price = Cost[size];
Size = size;
Toppings.Add(top1);
Toppings.Add(top2);
Toppings.Add(top3);
}
public Pizza(ECrust crust, EPizzaSize size, EToppings top1, EToppings top2, EToppings top3, EToppings top4)
: this()
{
Crust = crust;
Price = Cost[size];
Size = size;
Toppings.Add(top1);
Toppings.Add(top2);
Toppings.Add(top3);
Toppings.Add(top4);
}
public Pizza(ECrust crust, EPizzaSize size, EToppings top1, EToppings top2, EToppings top3, EToppings top4, EToppings top5)
: this()
{
Crust = crust;
Price = Cost[size];
Size = size;
Toppings.Add(top1);
Toppings.Add(top2);
Toppings.Add(top3);
Toppings.Add(top4);
Toppings.Add(top5);
}
}
}
I am trying to potentially do something like this:
Toppings.Add(top1, top2, top3, top4);