示例#1
0
        public VendingMachines.PurchaseResults PurchaseThisItem(int itemNumber, VendingMachines vm)
        {
            VendingMachines.PurchaseResults result = VendingMachines.PurchaseResults.PurchaseInProgress;

            Console.WriteLine();
            Console.WriteLine("***** Item [{0}] Selected for Purchase. *****", itemNumber);
            CurrentSelectedItem = itemNumber;

            if (this.IsItemOutOfStock(itemNumber))
            {
                Console.WriteLine();
                Console.WriteLine("***** Sorry, item: [{0}] is Out-Of-Stock. *****", itemNumber);
                return(VendingMachines.PurchaseResults.NotCompletedItemOutOfStock);
            }

            if (!this.AreCustomerFundsEnoughToPurchaseThisItem(itemNumber, vm))
            {
                Console.WriteLine();
                Console.WriteLine("***** Sorry, not enough money to purchase item: [{0}]. *****", itemNumber);
                return(VendingMachines.PurchaseResults.NotCompletedNotEnoughMoney);
            }

            result = this.CompetePurchase(itemNumber, vm);

            return(result);
        }
示例#2
0
        private bool AreCustomerFundsEnoughToPurchaseThisItem(int itemNumber, VendingMachines vm)
        {
            bool result = false;

            if (vm.CoinBox.CustomerCoinsValue >= vm.Inventory.ItemCostArray[itemNumber - 1])
            {
                result = true;
            }
            return(result);
        }
示例#3
0
        private VendingMachines.PurchaseResults CompetePurchase(int itemNumber, VendingMachines vm)
        {
            VendingMachines.PurchaseResults result = VendingMachines.PurchaseResults.PurchaseInProgress;

            this.DispenseThisItem(itemNumber);

            // Compute Change and Move Customer's Coins to CoinBox and ChangeBox
            this.ComputeChangeAndMoveCustomersCoins(itemNumber, vm);

            result = VendingMachines.PurchaseResults.PurchaseCompleted;
            return(result);
        }
示例#4
0
    private void PurchaseThisItem(int itemNumber, VendingMachines vm)
    {
        Vm.StateMachine.TransitionStateMachine(StateMachines.VMActions.SelectingItemForPurchase);

        VendingMachines.PurchaseResults purchaseResult = Vm.Inventory.PurchaseThisItem(itemNumber, vm);

        if (purchaseResult == VendingMachines.PurchaseResults.PurchaseCompleted)
        {
            Vm.StateMachine.TransitionStateMachine(StateMachines.VMActions.PurchaseCompleted);
        }
        else if (purchaseResult == VendingMachines.PurchaseResults.NotCompletedNotEnoughMoney)
        {
            Vm.StateMachine.TransitionStateMachine(StateMachines.VMActions.PurchaseNotCompleted);
        }
        else if (purchaseResult == VendingMachines.PurchaseResults.PurchaseNotCompleted)
        {
            Vm.StateMachine.TransitionStateMachine(StateMachines.VMActions.PurchaseNotCompleted);
        }
    }
示例#5
0
 public Menus()
 {
     Vm = new VendingMachines();
 }
示例#6
0
        private void ComputeChangeAndMoveCustomersCoins(int itemNumber, VendingMachines vm)
        {
            int[] TargetChangeVector = new[] { 0, 0, 0 }
            ;                       int TargetChangeVectorValue = 0;
            int TotalTargetChangeVectorValue = 0;

            if (vm.CoinBox.CustomerCoinsValue == vm.Inventory.ItemCostArray[itemNumber - 1])
            {
                vm.CoinBox.MoveCustomersCoinsToCoinBox(vm.CoinBox.CustomerCoins.CoinVector);
                return;
            }
            else if (vm.CoinBox.CustomerCoinsValue >= vm.Inventory.ItemCostArray[itemNumber - 1])
            {
                TargetChangeVectorValue      = vm.CoinBox.CustomerCoinsValue - vm.Inventory.ItemCostArray[itemNumber - 1];
                TotalTargetChangeVectorValue = TargetChangeVectorValue;

                // Make change from all the coins in the CoinBox

                vm.CoinBox.MoveCustomersCoinsToCoinBox(vm.CoinBox.CustomerCoins.CoinVector);

                do
                {
                    if (vm.CoinBox.CoinBoxCoins.CoinVector[2] > 0)
                    {
                        if (TargetChangeVectorValue >= Coins.COIN_3_VALUE)
                        {
                            vm.CoinBox.MoveCoinBoxCoinsToChangeBox(new int[] { 0, 0, 1 });
                            TargetChangeVectorValue -= Coins.COIN_3_VALUE;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                } while (vm.CoinBox.ChangeBoxCoinsValue < TargetChangeVectorValue);

                do
                {
                    if (vm.CoinBox.CoinBoxCoins.CoinVector[1] > 0)
                    {
                        if (TargetChangeVectorValue >= Coins.COIN_2_VALUE)
                        {
                            vm.CoinBox.MoveCoinBoxCoinsToChangeBox(new int[] { 0, 1, 0 });
                            TargetChangeVectorValue -= Coins.COIN_2_VALUE;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                } while (vm.CoinBox.ChangeBoxCoinsValue < TargetChangeVectorValue);

                do
                {
                    if (vm.CoinBox.CoinBoxCoins.CoinVector[0] > 0)
                    {
                        if (TargetChangeVectorValue >= Coins.COIN_1_VALUE)
                        {
                            vm.CoinBox.MoveCoinBoxCoinsToChangeBox(new int[] { 1, 0, 0 });
                            TargetChangeVectorValue -= Coins.COIN_1_VALUE;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                } while (vm.CoinBox.ChangeBoxCoinsValue < TargetChangeVectorValue);

                if (vm.CoinBox.ChangeBoxCoinsValue < TotalTargetChangeVectorValue)
                {
                    Console.WriteLine();
                    Console.WriteLine("***** Sorry, CoinBox does not have the coins to make correct change. *****");
                    Console.WriteLine("***** We owe you [{0}] cents. *****", TargetChangeVectorValue);
                }

                return;
            }
        }