/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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);
/// <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); }
/// <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); }