Beispiel #1
0
        public bool DoPurchase(Product product)
        {
            if (_pBox == null) throw new InvalidOperationException("_pBox");

            bool purchased = false;

            MachineStatus oldStatus = _status;
            _status = MachineStatus.InTransaction;

            CoinBox changes = _rBox;
            try
            {
                if (product.Equals(_none))
                    throw new Exception();

                int count = _pBox[product];
                if (count < 0)
                    throw new Exception("No such product is avaiable.");

                if (count == 0)
                    throw new Exception("The product is out of sale.");

                int changesValue = _rBox.TotalValue - product.Price;

                if (changesValue < 0)
                    throw new Exception("Not enough money.");

                changes = OnMakeChanges(changesValue);

                if (changes == null)
                {
                    changes = _rBox;
                    throw new Exception("Cannot return changes.");
                }

                DeliverProduct(product);

                purchased = true;
            }
            catch (Exception ex)
            {
                if (_msg != null)
                    _msg.Show(ex.Message);
            }
            finally
            {
                ReturnChanges(changes);

                _status = oldStatus;
            }

            return purchased;
        }
Beispiel #2
0
        private void DeliverProduct(Product product)
        {
            if (_pBox[product] <= 0)
                throw new InvalidOperationException("product");

            if (_msg != null)
                _msg.Show(string.Format("Delivering product: {0}", product));

            _pBox[product]--;
        }