Example #1
0
        public static AbstractPizza CreatePizza(string pizzaName)
        {
            AbstractPizzaBuilder pizzaBuilder;

            switch (pizzaName)
            {
            case "Margarita":
                pizzaBuilder = new MargaritaBuilder();
                break;

            case "Carbonara":
                pizzaBuilder = new CarbonaraBuilder();
                break;

            case "Italian":
                pizzaBuilder = new ItalianBuilder();
                break;

            case "Pepperoni":
                pizzaBuilder = new PepperoniBuilder();
                break;

            default:
                throw new ArgumentException("Pizza with that name is not on our menu");
            }

            pizzaBuilder.AddCheese().AddTomatoes().AddOlives().AddMeat();

            return(pizzaBuilder.Create());
        }
        static void Main(string[] args)
        {
            string   number       = "";
            IBuilder pizzabuilder = null;

            while (pizzabuilder == null)
            {
                Console.WriteLine("Choose pizza: \nMargarita (1)\nItalian (2)\nPepperoni (3)\nCarbonara (4)");
                number = Console.ReadLine();
                switch (number)
                {
                case "1":
                {
                    pizzabuilder = new MargaritaBuilder();
                    break;
                }

                case "2":
                {
                    pizzabuilder = new ItalianBuilder();
                    break;
                }

                case "3":
                {
                    pizzabuilder = new PepperoniBuilder();
                    break;
                }

                case "4":
                {
                    pizzabuilder = new CarbonaraBuilder();
                    break;
                }

                default:
                {
                    Console.WriteLine("Write number");
                    break;
                }
                }
            }

            Console.WriteLine("You choosed {0} pizza with ingridients: ", pizzabuilder.createPizza().GetType().Name);
            foreach (var ingridient in pizzabuilder.createPizza().Ingridients)
            {
                Console.WriteLine("\t {0}", ingridient);
            }
            Console.WriteLine("Would you like to add extra ingridients? (y/n)");
            string answer = "";
            APizza pizza  = null;

            while (answer != "y" && answer != "n")
            {
                answer = Console.ReadLine();
                switch (answer)
                {
                case "y":
                {
                    pizza = constructPizza(pizzabuilder);
                    break;
                }

                case "n":
                {
                    pizza = pizzabuilder.createPizza();
                    break;
                }

                default:
                {
                    Console.WriteLine("Type 'n' or 'y'");
                    break;
                }
                }
            }
            Console.WriteLine("Enjoy your {0} pizza with ingridients: ", pizza.GetType().Name);
            foreach (var ingridient in pizza.Ingridients)
            {
                Console.WriteLine("\t {0}", ingridient);
            }
        }