Exemple #1
0
        private static void CalculateCoins(decimal remainingAmountRequiredInChange, IDictionary <CoinType, int> coinsAvailable, IDictionary <CoinType, int> coinsToReturnInChange, CoinType currentCoinType)
        {
            if (remainingAmountRequiredInChange > 0)
            {
                if ((remainingAmountRequiredInChange >= currentCoinType.Value()))
                {
                    if (coinsToReturnInChange.ContainsKey(currentCoinType))
                    {
                        coinsToReturnInChange[currentCoinType] += 1;
                    }
                    else
                    {
                        coinsToReturnInChange.Add(currentCoinType, 1);
                    }
                    coinsAvailable[currentCoinType] -= 1;
                    remainingAmountRequiredInChange -= currentCoinType.Value();
                    CalculateCoins(remainingAmountRequiredInChange, coinsAvailable, coinsToReturnInChange, currentCoinType);
                }
                else
                {
                    currentCoinType--;
                    switch (currentCoinType)
                    {
                    case CoinType.Nickel:
                        CalculateCoins(remainingAmountRequiredInChange, coinsAvailable, coinsToReturnInChange, CoinType.Nickel);
                        break;

                    case CoinType.Dime:
                        CalculateCoins(remainingAmountRequiredInChange, coinsAvailable, coinsToReturnInChange, CoinType.Dime);
                        break;

                    case CoinType.Quartet:
                        CalculateCoins(remainingAmountRequiredInChange, coinsAvailable, coinsToReturnInChange, CoinType.Quartet);
                        break;

                    case CoinType.Dollar:
                        CalculateCoins(remainingAmountRequiredInChange, coinsAvailable, coinsToReturnInChange, CoinType.Dollar);
                        break;
                    }
                }
            }
        }