コード例 #1
0
ファイル: Pizza.cs プロジェクト: nikolayvutov/CSharp
    public double TotalCalories()
    {
        var doughtCals  = dough.DoughCalories();
        var toppingCals = topping.Select(t => t.ToppingCalories()).Sum();
        var result      = doughtCals + toppingCals;

        return(result);
    }
コード例 #2
0
    static void Main(string[] args)
    {
        try
        {
            int          toppicCounter = 0;
            List <Pizza> list          = new List <Pizza>();
            var          input         = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var          pizzaName     = input[1];
            var          pizza         = new Pizza();
            double       total         = 0;

            while (input[0] != "END")
            {
                if (input[0] == "Dough")
                {
                    var dought = new Dough(input[1], input[2], double.Parse(input[3]));

                    total += dought.DoughCalories();
                    pizza  = new Pizza(pizzaName, dought, new List <Topping>());
                }
                else if (input[0] == "Topping")
                {
                    ////// TOPPING COUNTER BLQK
                    toppicCounter++;
                    if (toppicCounter > 10)
                    {
                        throw new ArgumentException("Number of toppings should be in range [0..10].");
                    }

                    var topping = new Topping(input[1], double.Parse(input[2]));
                    total += topping.BackingTechCallories();
                    pizza.Toppings.Add(topping);
                }

                input = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            }
            list.Add(pizza);

            foreach (Pizza p in list)
            {
                if (string.IsNullOrEmpty(p.Name) || total < 1)
                {
                    return;
                }
                else
                {
                    Console.WriteLine($"{p.Name} - {total:f2} Calories.");
                    //var resu = p.TotalCalories(p);
                    //Console.WriteLine(resu);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            //throw;
        }
    }
コード例 #3
0
    public static void Main()
    {
        var            input         = Console.ReadLine().Split();
        Dough          doughForPizza = new Dough();
        List <Topping> addedToppings = new List <Topping>();
        Pizza          pizza         = new Pizza();
        var            isPizza       = false;

        try
        {
            while (input[0] != "END")
            {
                if (input[0] == "Dough")
                {
                    Dough dough = new Dough();
                    dough.FlourType       = input[1];
                    dough.BakingTechnique = input[2];
                    dough.Weight          = double.Parse(input[3]);
                    doughForPizza         = dough;
                    if (!isPizza)
                    {
                        Console.WriteLine($"{dough.DoughCalories():f2}");
                    }
                }
                else if (input[0] == "Topping")
                {
                    Topping topping = new Topping();
                    topping.ToppingType = input[1];
                    topping.Weight      = double.Parse(input[2]);
                    addedToppings.Add(topping);
                    if (!isPizza)
                    {
                        Console.WriteLine($"{topping.ToppingCalories():f2}");
                    }
                }
                else if (input[0] == "Pizza")
                {
                    isPizza                = true;
                    pizza.Name             = input[1];
                    pizza.NumberOfToppings = int.Parse(input[2]);
                }

                input = Console.ReadLine().Split();
            }
            if (!string.IsNullOrWhiteSpace(pizza.Name))
            {
                pizza.Dough    = doughForPizza;
                pizza.Toppings = addedToppings;
                Console.WriteLine($"{pizza.Name} - {pizza.PizzaCalories():f2} Calories.");
            }
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }