Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Pizza"/> class.
 /// </summary>
 /// <param name="pizzaOrder">The pizza order.</param>
 /// <param name="cookingTimeMs">The cooking time ms.</param>
 public Pizza(
     IPizzaOrder pizzaOrder,
     double cookingTimeMs)
     : base(pizzaOrder, pizzaOrder.Topping)
 {
     this.CookingTime = TimeSpan.FromMilliseconds(cookingTimeMs);
 }
Exemple #2
0
        /// <summary>
        /// Gets the pizza.
        /// </summary>
        /// <param name="pizzaName">Name of the pizza.</param>
        /// <returns>The pizza.</returns>
        public IPizzaOrder GetPizza(string pizzaName)
        {
            IPizzaOrder cachedPizza = _cache.Get(pizzaName) as IPizzaOrder;

            if (cachedPizza != null)
            {
                return(cachedPizza);
            }

            _count++;
            IPizzaOrder pizza = new Pizza(pizzaName);

            _cache.Set(pizzaName, pizza, ObjectCache.InfiniteAbsoluteExpiration);

            return(pizza);
        }
Exemple #3
0
        public async Task ProcessOrderAsync(
            IPizzaOrder pizzaOrder,
            CancellationToken cancellationToken)
        {
            // Calculate cooking time
            var cookingTimeMs     = pizzaOrder.Multiplier * BaseTimeMsLazy.Value;
            var toppingMultiplier = Convert.ToDouble(
                pizzaOrder.Topping.Count(c => char.IsLetter(c)));

            cookingTimeMs += toppingMultiplier * TimePerToppingLetterMsLazy.Value;

            this.logger.Debug(
                "Calculated total cooking time for pizza: {0}ms",
                cookingTimeMs);

            // Cook the pizza (in the oven)
            await this.pizzaOven.CookAsync(
                pizzaOrder,
                cookingTimeMs,
                cancellationToken);
        }
Exemple #4
0
        public async Task CookAsync(
            IPizzaOrder pizzaOrder,
            double cookingTimeMs,
            CancellationToken cancellationToken)
        {
            var pizza = new Pizza(
                pizzaOrder,
                cookingTimeMs);

            this.logger.Debug(
                "Simulating cooking pizza {0} for {1}",
                pizza,
                cookingTimeMs);

            // Simulate cooking
            await Task.Delay(
                TimeSpan.FromMilliseconds(cookingTimeMs),
                cancellationToken);

            this.logger.Debug(
                "Finished cooking pizza. Writing to output file: {0}",
                OutputFilePathLazy.Value);

            try
            {
                using (var sw = new StreamWriter(OutputFilePathLazy.Value, true))
                {
                    await sw.WriteLineAsync(
                        pizza.ToString("C"));
                }
            }
            catch (DirectoryNotFoundException ex)
            {
                this.logger.Fatal(
                    ex,
                    "The output file directory path does not exist: {0}",
                    OutputFilePathLazy.Value);
                throw;
            }
        }
        /// <summary>
        /// Takes the order.
        /// </summary>
        /// <param name="pizzaName">Name of the pizza.</param>
        /// <param name="address">The address.</param>
        public void TakeOrder(string pizzaName, string address)
        {
            IPizzaOrder pizza = _pizzaFactory.GetPizza(pizzaName);

            _pizzas.Add(new KeyValuePair <IPizzaOrder, PizzaOrderContext>(pizza, new PizzaOrderContext(address)));
        }