Beispiel #1
0
        /// <summary>
        /// Simulates the cooking of a sandwich returning the sandwich as an object
        /// </summary>
        /// <param name="commands">Ordered array of commands passed to the machine to cook a sandwich</param>
        /// <returns>The resulting sandwich</returns>
        public Sandwich Cook(MachineCommand[] commands)
        {
            semaphore.Wait();
            Sandwich result = ProcessCommands(commands);

            semaphore.Release();

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Adds salsa to the sandwich being cooked
        /// </summary>
        /// <param name="sandwich">Sandwich being cooked</param>
        /// <returns>The sandwich after adding the salsa</returns>
        public override Sandwich ApplyCommand(Sandwich sandwich)
        {
            IList <string> contents;

            contents = (sandwich.Contents == null) ? (new List <string>()) : (sandwich.Contents.ToList());

            if (contents.Count > 0 && contents.Last() == "salsa")
            {
                throw new InvalidOperationException("Cannot add salsa over salsa");
            }
            contents.Add("salsa");

            sandwich.Contents = contents.ToArray();

            return(sandwich);
        }
        private Sandwich ProcessCommands(MachineCommand[] commands)
        {
            DateTime timeStart = DateTime.Now;

            Sandwich result = new Sandwich();

            foreach (MachineCommand command in commands)
            {
                result = command.ApplyCommand(result);
            }

            //Sleep for the remaining time to complete the cooking
            Thread.Sleep(_TimeToPrepareInMilliseconds - ((DateTime.Now - timeStart).Milliseconds));

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Adds ingredient to the sandwich being cooked
        /// </summary>
        /// <param name="sandwich">Sandwich being cooked</param>
        /// <returns>The sandwich after adding the ingredient</returns>
        public override Sandwich ApplyCommand(Sandwich sandwich)
        {
            IList <string> contents;

            contents = (sandwich.Contents == null) ? (new List <string>()) : (sandwich.Contents.ToList());

            if (MachineStackIndex < 1 && MachineStackIndex > 3)
            {
                throw new InvalidOperationException("Invalid stack index");
            }

            contents.Add(Name);

            sandwich.Contents = contents.ToArray();

            return(sandwich);
        }
Beispiel #5
0
        /// <summary>
        /// Adds bread to the sandwich being cooked
        /// </summary>
        /// <param name="sandwich">Sandwich being cooked</param>
        /// <returns>The sandwich after adding the bread</returns>
        public override Sandwich ApplyCommand(Sandwich sandwich)
        {
            IList <string> contents;

            contents = (sandwich.Contents == null) ? (new List <string>()) : (sandwich.Contents.ToList());

            if (contents.Count(x => x == "bread") > 1)
            {
                throw new InvalidOperationException("Cannot add more than 2 breads");
            }
            if (contents.Count > 0 && contents.Last() == "bread")
            {
                throw new InvalidOperationException("Cannot add bread over bread");
            }

            contents.Add("bread");

            sandwich.Contents = contents.ToArray();

            return(sandwich);
        }
        /// <summary>
        /// Simulates the cooking of a sandwich returning the sandwich as an object
        /// </summary>
        /// <param name="commands">Ordered array of commands passed to the machine to cook a sandwich</param>
        /// <returns>The resulting sandwich</returns>
        public Sandwich Cook(MachineCommand[] commands)
        {
            lock (locker)
            {
                if (sandwichsBeingCooked == _MaxSandiwchsAtTheSameTime)
                {
                    throw new Exception();
                }
                else
                {
                    sandwichsBeingCooked++;
                }
            }

            Sandwich result = ProcessCommands(commands);

            lock (locker)
            {
                sandwichsBeingCooked--;
            }

            return(result);
        }
Beispiel #7
0
 /// <summary>
 /// Apply a command to the sandwich being cooked
 /// </summary>
 /// <param name="sandwich">Sandwich being cooked</param>
 /// <returns>The sandwich after applying the command</returns>
 public abstract Sandwich ApplyCommand(Sandwich sandwich);
Beispiel #8
0
        /// <summary>
        /// Compress the sandwich being cooked
        /// </summary>
        /// <param name="sandwich">Sandwich being cooked</param>
        /// <returns>The sandwich after compression</returns>
        public override Sandwich ApplyCommand(Sandwich sandwich)
        {
            sandwich.Compressed = true;

            return(sandwich);
        }
Beispiel #9
0
        /// <summary>
        /// Cut the sandwich being cooked
        /// </summary>
        /// <param name="sandwich">Sandwich being cooked</param>
        /// <returns>The sandwich after the cut</returns>
        public override Sandwich ApplyCommand(Sandwich sandwich)
        {
            sandwich.Cut = Cut;

            return(sandwich);
        }