/// <summary> /// Make a product sale /// </summary> /// <param name="ProductId">The id of the product object</param> /// <returns>List of coins that is changed or the coins the customer has</returns> public List <Coin> MakeSale(int ProductId) { try { Product product = Products.Find(x => x.Id == ProductId); int TotalCents = CustomerCoins.Sum(x => x.TotalCents); bool SaleSuccess = false; if (CanMakeSale(ProductId)) { //reset change ChangeCoins = new List <Coin>(); //reduce product Products.Find(x => x.Id == ProductId).Stock--; //add to user coins to machine CustomerCoins.ForEach(coin => { CoinInsertMachine(coin); }); //check if change is needed if (TotalCents >= product.Price) { ChangeCoins = CalculateChange(TotalCents - product.Price); //remove coins from current stash ChangeCoins.ForEach(coin => { RemoveCoin(coin); }); //return change } //reset usercoins sale is make CustomerCoins = new List <Coin>(); SaleSuccess = true; } if (SaleSuccess) { // return change return(ChangeCoins); } else { //cancel return(CustomerCoins); } } catch (Exception ex) { //Error return coins Console.WriteLine(ex.Message); return(CustomerCoins); } }
/// <summary> /// Sum value of customer inserted coins in cents /// </summary> /// <returns></returns> public int CustomerCoinsValue() { if (CustomerCoins.Count == 0) { return(0); } else { //sum the accumulated total cents per coin value return(CustomerCoins.Sum(x => x.TotalCents)); } }