public VendingResponse AcceptCoin(InputCoin coin)
        {
            VendingResponse response = new VendingResponse();

            if (coin == null)
            {
                throw new ArgumentNullException("Coin parameter null!");
            }

            //check if the values correspond to an accepted coin
            var currentCoin = _coinService.GetCoin(coin.Weight, coin.Diameter, coin.Thickness);

            //not a valid coin
            if (currentCoin == null)
            {
                response.Message        = "Insert Coin";
                response.IsRejectedCoin = true;
                response.RejectedCoin   = coin; //return rejected coin
                return(response);
            }

            //valid coin
            _cost                  += currentCoin.Value;
            response.Message        = _cost.ToString();
            response.IsSuccess      = true;
            response.IsRejectedCoin = false;
            return(response);
        }
        public VendingResponse SelectProduct(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentNullException("Code parameter empty!");
            }

            var response = new VendingResponse();

            //check if the code is valid
            //if no, return error object with details
            var product = _productService.GetProduct(code);

            //invalid code entered
            if (product == null)
            {
                response.Message   = "Invalid Product Selected. Please try again";
                response.IsSuccess = false;
                return(response);
            }

            //no coins entered, but selection pressed
            if (_cost == 0)
            {
                //if exact change item, message = "exact change only"
                response.Message   = "Insert Coin";
                response.IsSuccess = false;
                return(response);
            }

            //entered coins less than cost
            if (_cost < product.Price)
            {
                response.Message   = string.Format("Price : {0}", product.Price);
                response.IsSuccess = false;
                return(response);
            }

            //all good, valid code and valid amount entered
            var quantity = _productService.GetProductQuantity(code);

            if (quantity > 0)
            {
                response.Message   = "Thank You";
                response.IsSuccess = true;
                _productService.UpdateProductQuantity(code);
                response.Change = MakeChange(Convert.ToDouble(_cost - product.Price));
                _cost           = 0.00m;
                return(response);
            }

            response.Message   = "SOLD OUT";
            response.IsSuccess = false;
            return(response);
        }
Exemplo n.º 3
0
        static void WriteVendingResponseToScreen(VendingResponse response)
        {
            Console.WriteLine("Message : " + response.Message);
            Console.WriteLine("IsRejectedCoin : " + response.IsRejectedCoin);

            if (response.RejectedCoin != null)
            {
                Console.WriteLine("Coin Diameter: " + response.RejectedCoin.Diameter);
                Console.WriteLine("Coin Weight: " + response.RejectedCoin.Weight);
                Console.WriteLine("Coin Thickness: " + response.RejectedCoin.Thickness);
            }

            if (response.Change != null)
            {
                WriteItemChangeToScreen(response.Change);
            }

            Console.WriteLine("IsSuccess : " + response.IsSuccess);
        }