public void DispenseCoins(decimal amount)
 {
     foreach (var coinTube in CoinTubes
              .Where(i => i.CountInTube > 0 && i.Spec.Value <= amount)
              .OrderByDescending(i => i.Spec.Value)
              .ThenByDescending(i => i.CountInTube))
     {
         while (amount > 0m && coinTube.CountInTube > 0 &&
                amount >= coinTube.Spec.Value)
         {
             amount         -= coinTube.Spec.Value;
             AmountInserted -= coinTube.Spec.Value;
             coinTube.CountInTube--;
             OnCoinDispensed(new CoinDispensedEventArgs(coinTube.Spec));
         }
     }
 }
        public bool CanDispenseCoins(decimal amount)
        {
            foreach (var coinTube in CoinTubes
                     .Where(i => i.CountInTube > 0 && i.Spec.Value <= amount)
                     .OrderByDescending(i => i.Spec.Value)
                     .ThenByDescending(i => i.CountInTube))
            {
                var virtualCountInTube = coinTube.CountInTube;

                while (amount > 0m && virtualCountInTube > 0 &&
                       amount >= coinTube.Spec.Value)
                {
                    amount -= coinTube.Spec.Value;
                    virtualCountInTube--;
                }
            }

            return(amount == 0m);
        }
 public int GetCoinInventory(CoinSpecification coinType)
 {
     return(CoinTubes.Where(i => i.Spec == coinType)
            .Sum(i => i.CountInTube));
 }