/// <summary> /// call all machine item categories to try and find the item you are looking for. /// </summary> /// <param name="position">The position in the vending macine of the item.</param> /// <returns><see cref="MachineItem"/></returns> private MachineItem TryFindMachineItem(MachineItemPosition position) { var chipsTask = Task.Run(() => tryChips(position)); var chocolatesTask = Task.Run(() => tryChocolates(position)); var sweetsTask = Task.Run(() => trySweets(position)); var drinksTask = Task.Run(() => tryDrinks(position)); Task.WaitAll(chipsTask, chocolatesTask, sweetsTask, drinksTask); if (chipsTask.Result != null) { return(chipsTask.Result); } if (chocolatesTask.Result != null) { return(chocolatesTask.Result); } if (sweetsTask.Result != null) { return(sweetsTask.Result); } if (drinksTask.Result != null) { return(drinksTask.Result); } return(null); }
private MachineItem tryChocolates(MachineItemPosition position) { var chipsItem = _vendingMachineItems.Chocolates.ToList().Find(x => x.Position.Equals(position)); if (chipsItem == null) { return(null); } return(chipsItem); }
/// <summary> /// get the item from the machine based on the position in the machine it is in. /// </summary> /// <param name="position">The position of the item in the vending machine,</param> /// <param name="tenderAmount">The amount an individual has put in the vending machine.</param> /// <returns>decimal</returns> public decimal ProcessPurchase(MachineItemPosition position, decimal tenderAmount) { MachineItem item = TryFindMachineItem(position); if (item == null) { throw new ApplicationException($"Unable to find the vending machine item at position: {position.ToString()}."); } if (tenderAmount < item.Price) { throw new ApplicationException($"The amount provided: {_currencySymbol} {tenderAmount}, is less than the required amount of: {_currencySymbol} {item.Price} for the {item.Item}"); } return(calculateChange(item.Price, tenderAmount)); }