Esempio n. 1
0
        public static void Main()
        {
            string command = Console.ReadLine();

            while (command != "END")
            {
                string[] tokens = command.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (tokens[0] == "Dough")
                {
                    try
                    {
                        Dough dough = new Dough(tokens[1], tokens[2], int.Parse(tokens[3]));
                        Console.WriteLine($"{dough.CalculateCalories():F2}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return;
                    }
                }
                else if (tokens[0] == "Topping")
                {
                    try
                    {
                        Topping topping = new Topping(tokens[1], int.Parse(tokens[2]));
                        Console.WriteLine($"{topping.CalculateCalories():F2}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return;
                    }
                }
                else
                {
                    string name             = tokens[1];
                    int    numberOfToppings = int.Parse(tokens[2]);

                    command = Console.ReadLine();
                    tokens  = command.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    Pizza pizza;
                    try
                    {
                        Dough dough = new Dough(tokens[1], tokens[2], int.Parse(tokens[3]));
                        pizza = new Pizza(name, dough);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return;
                    }

                    for (int i = 0; i < numberOfToppings; i++)
                    {
                        command = Console.ReadLine();
                        tokens  = command.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        try
                        {
                            Topping topping = new Topping(tokens[1], int.Parse(tokens[2]));
                            pizza.AddTopping(topping);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            return;
                        }
                    }
                    Console.WriteLine($"{pizza.Name} - {pizza.GetCalories():F2} Calories.");
                    return;
                }
                command = Console.ReadLine();
            }
        }
Esempio n. 2
0
        public static void Main()
        {
            var pizza = new Pizza();
            var dough = new Dough();

            var line = Console.ReadLine();

            if (line.Contains("Dough"))
            {
                var tokens = line
                             .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                             .ToArray();

                var type   = tokens[1];
                var tech   = tokens[2];
                var weight = double.Parse(tokens[3]);

                try
                {
                    dough = new Dough(type, tech, weight);
                    Console.WriteLine($"{dough.GetCalculateCalories():F2}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }

                line = Console.ReadLine();

                if (line.Equals("END"))
                {
                    return;
                }
            }

            if (line.Contains("Topping"))
            {
                var tokens = line
                             .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                             .ToArray();

                var toppingName   = tokens[1];
                var toppingWeight = double.Parse(tokens[2]);

                try
                {
                    var topping = new Topping(toppingName, toppingWeight);
                    Console.WriteLine($"{topping.GetCalculateCalories():F2}");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return;
                }

                return;
            }

            while (line != "END")
            {
                var lineTokens = line
                                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                 .ToArray();

                switch (lineTokens[0])
                {
                case "Pizza":
                    var name         = lineTokens[1];
                    var toppingCount = int.Parse(lineTokens[2]);

                    try
                    {
                        pizza = new Pizza(name, toppingCount);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return;
                    }

                    break;

                case "Dough":
                    var type   = lineTokens[1];
                    var tech   = lineTokens[2];
                    var weight = double.Parse(lineTokens[3]);

                    try
                    {
                        dough       = new Dough(type, tech, weight);
                        pizza.Dough = dough;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return;
                    }

                    break;

                case "Topping":
                    var toppingName   = lineTokens[1];
                    var toppingWeight = double.Parse(lineTokens[2]);

                    try
                    {
                        var topping = new Topping(toppingName, toppingWeight);
                        pizza.Toppings.Add(topping);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return;
                    }

                    break;
                }

                line = Console.ReadLine();
            }

            Console.WriteLine($"{pizza.Name} - {pizza.GetCalculateCalories():F2} Calories.");
        }