public void loadCoins(int coinKindIndex, List <Coin> coins) { // load in software SoftwareCoinRacks getRack = softwareRacks[coinKindIndex]; getRack.incQuantity(coins.Count); // load in hardware CoinRack[] racks = vendingHardware.CoinRacks; CoinRack coinRack = null; try { coinRack = racks[coinKindIndex]; } catch (Exception e) { Console.WriteLine("Invalid coin kind index"); } coinRack.LoadCoins(coins); //Console.WriteLine("Load: " + coinKinds[coinKindIndex]); }
public void PressButton(int vmIndex, int value) { if (vmIndex < 0 || vmIndex >= theVendingMachineFactory.Count) { throw new Exception("Null vmIndex (PB)."); } VendingMachine theMachine = theVendingMachineFactory[vmIndex]; if (value < 0 || theMachine.SelectionButtons.Length <= value) { throw new Exception("Null value (PB)."); } if (theMachine.PopCanCosts[value] <= theVMLogics[vmIndex].cashReceptacle) { theMachine.PopCanRacks[value].DispensePopCan(); theMachine.CoinReceptacle.StoreCoins(); int change = theVMLogics[vmIndex].cashReceptacle - theMachine.PopCanCosts[value]; List <int> sortedCoins = new List <int>(); sortedCoins.AddRange(theVMLogics[vmIndex].coinKinds); sortedCoins.Sort(); while (change > 0) { int coinValue = sortedCoins[sortedCoins.Count - 1]; int modChange = change / coinValue; CoinRack theCoinRack = theMachine.GetCoinRackForCoinKind(coinValue); int amountRemoved = Math.Min(theCoinRack.Count, modChange); change -= amountRemoved * coinValue; for (int i = 0; i < amountRemoved; i++) { theCoinRack.ReleaseCoin(); } sortedCoins.RemoveAt(sortedCoins.Count - 1); if (sortedCoins.Count == 0) { break; } } theVMLogics[vmIndex].cashReceptacle = 0; } // TODO: Implement }
void printCoinLoaded(object sender, CoinEventArgs e) { // this code updates the software understanding of // the coin racks when the receptacle stores its // coins by listening to event, as it may go into // storage bin if coin rack full int coinValue = e.Coin.Value; Console.WriteLine("Coin rack loaded with: " + coinValue); CoinRack rack = (CoinRack)sender; // implement code here to update software coin rack // presume that receptacle always puts coins in right // racks, so the coin value indicates which rack foreach (SoftwareCoinRacks coinRack in softwareRacks) { if (coinRack.getValue() == coinValue) { coinRack.incQuantity(1); break; } } }