public CoinChute(List <Coin> listOfCoins) { slots = new List <CoinSlot>(); insertValue = 0; bankedValue = 0; bankedCoins = new List <Coin>(); insertedCoins = new List <Coin>(); foreach (Coin coin in listOfCoins) { CoinSlot newSlot = new CoinSlot(coin); slots.Add(newSlot); } }
public List <Coin> releaseChange(int val) { List <Coin> coinChange = new List <Coin>(); int upperBound = val + 1; int largestCoinVal = 0; // loop change process until either // no more valid coins, or change // value is met (where the loop will // be broken) while (true) { // loop through coin slots to find // next denomination // if found, sets the largestSlot // to a Coin, which has a value CoinSlot largestSlot = null; foreach (CoinSlot slot in slots) { Coin type = slot.getType(); if (type.Value >= largestCoinVal && type.Value < upperBound) { largestCoinVal = type.Value; largestSlot = slot; } } // check if largest coin is null // if so, then we have not found // a suitable change coin (we will // short change them) if (largestSlot == null) { return(coinChange); } // now we have next largest coin // decrease until target met, each // time removing a coin from the // coin slot, and adding it to the // coin change list bool runLoop = true; bool changeFinished = false; while (runLoop && largestSlot.getQuantity() > 0) { // decrement change total by coin // denomination val = val - largestCoinVal; if (val >= 0) { // returned coin may be "incorrect" if the // slot was loaded incorrectly - this is // the desired functionality Coin changeCoin = largestSlot.removeCoin(); coinChange.Add(changeCoin); //Console.WriteLine("Dispensing coin: " + changeCoin.Value); // if new change value is zero, all change // has been added to the coin change list // so terminate loop if (val == 0) { runLoop = false; changeFinished = true; } } else { // if change value is negative, reverse // last decrement and move to lower // denomination val = val + largestCoinVal; runLoop = false; } } // if change finished flag is set, then // exit main loop if (changeFinished) { break; } // else, reset variables and start next // loop to find lower denomination upperBound = largestCoinVal; largestCoinVal = 0; } return(coinChange); }