Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var ingredients1 = GetIngredientsFromFile(coffee1);
            var ingredients2 = GetIngredientsFromFile(coffee2);
            var ingredients3 = GetIngredientsFromFile(coffee3);
            var ingredients4 = GetIngredientsFromFile(coffee4);
            var ingredients5 = GetIngredientsFromFile(coffee5);

            var ingredientLists = new List <List <CoffeeIngredient> >
            {
                GetIngredientsFromFile(coffee1),
                GetIngredientsFromFile(coffee2),
                GetIngredientsFromFile(coffee3),
                GetIngredientsFromFile(coffee4),
                GetIngredientsFromFile(coffee5)
            };

            // Factory -----------

            Console.WriteLine("Factory Pattern:");

            var coffeeFactory = new CoffeeFactory();

            foreach (var ingredientList in ingredientLists)
            {
                Coffee createdCoffee = coffeeFactory.MakeCoffee(ingredientList);
                Console.WriteLine(">> " + createdCoffee.Drink());
            }



            Console.WriteLine();

            // Builder -----------

            Console.WriteLine("Builder Pattern:");

            var coffeeBuilder = new CoffeeBuilder();

            Coffee builtCoffee = coffeeBuilder
                                 .AddIngredient(new CoffeeIngredient(4f, "cups", "water"))
                                 .AddIngredient(new CoffeeIngredient(0.25f, "cups", "coffee-beans", "roasted"))
                                 .AddIngredient(new CoffeeIngredient(0.25f, "cup", "sugar", "granulated"))
                                 .AddIngredient(new CoffeeIngredient(1f, "pinch", "cinnamon"))
                                 .AddIngredient(new CoffeeIngredient(0.25f, "tsp", "vanilla"))
                                 .BuildCoffee();

            Console.WriteLine(">> " + builtCoffee.Drink());

            Console.ReadKey();
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            var coffeeTypes = Enum.GetValues(typeof(CoffeeType));

            Console.WriteLine(PrintMessages.CoffeeTypes);

            foreach (var item in coffeeTypes)
            {
                int value = (int)item;

                Console.WriteLine($"{item} - {value}");
            }

            int coffeeNumber;

            while (true)
            {
                Console.Write(PrintMessages.EnterCoffeeNumber);
                bool isNum = int.TryParse(Console.ReadLine(), out coffeeNumber);

                if (isNum)
                {
                    if (coffeeNumber > coffeeTypes.Length || coffeeNumber < 1)
                    {
                        Console.WriteLine(PrintMessages.InvalidCoffeeNumber);
                        continue;
                    }
                    break;
                }
                else
                {
                    Console.WriteLine(PrintMessages.InvalidCoffeeNumber);
                    continue;
                }
            }

            CoffeeType coffeeType = (CoffeeType)coffeeNumber;

            Coffee coffee = CoffeeFactory.GetCoffee(coffeeType);

            coffee.Print();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            var fileReader  = new LocalFileReader();
            var lineParser  = new LineParser();
            var coffeeMaker = new CoffeeFactory();

            try
            {
                var lines = fileReader.GetAllLines(FILE_PATH);

                var ingredients = lines.Select(line => lineParser.ParseLine(line)).ToArray();
                var recipe      = new Recipe(ingredients);

                var        coffee         = coffeeMaker.GetCoffee(recipe);
                Director   coffeeDirector = null;
                BaseCoffee madeCoffee     = null;

                switch (coffee)
                {
                case Coffee.Espresso:
                    var espressoBuilder = new EspressoBuilder();
                    coffeeDirector = new Director(espressoBuilder);
                    coffeeDirector.MakeCoffee();
                    madeCoffee = espressoBuilder.GetResult();
                    break;

                case Coffee.Cappuccino:
                    var cappucinoBuilder = new CappuccinoBuilder();
                    coffeeDirector = new Director(cappucinoBuilder);
                    coffeeDirector.MakeCoffee();
                    madeCoffee = cappucinoBuilder.GetResult();
                    break;

                case Coffee.Americano:
                    var americanoBuilder = new AmericanoBuilder();
                    coffeeDirector = new Director(americanoBuilder);
                    coffeeDirector.MakeCoffee();
                    madeCoffee = americanoBuilder.GetResult();
                    break;

                case Coffee.FlatWhite:
                    var flatWhiteBuilder = new FlatWhiteBuilder();
                    coffeeDirector = new Director(flatWhiteBuilder);
                    coffeeDirector.MakeCoffee();
                    madeCoffee = flatWhiteBuilder.GetResult();
                    break;

                default:
                    Console.WriteLine("Recipe not recognized");
                    Console.ReadLine();
                    return;
                }

                Console.WriteLine(madeCoffee.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }