Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 2
0
 /// <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));
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Called when a customer inserts a coin
        /// </summary>
        /// <param name="coin"></param>
        /// <returns>Total inserted value in cents</returns>
        public int CoinInsertCustomer(Coin coin)
        {
            if (CustomerCoins.Count == 0)
            {
                CustomerCoins.Add(coin);
            }
            else
            {
                if (CustomerCoins.Where(x => x.Cents == coin.Cents).Count() == 0)
                {
                    CustomerCoins.Add(coin);
                }
                else
                {
                    CustomerCoins.Where(x => x.Cents == coin.Cents).Single().Quantity++;
                }
            }

            //reset
            ChangeCoins = new List <Coin>();

            return(CustomerCoinsValue());
        }