public void configurePop(List <string> popNames, List <int> popCosts) { // to allow machine to be reconfigured // to accept different pops acceptedPops.Clear(); if ((popNames.Count) != popButtons) { throw new Exception("Buttons and pop varieties differ: " + popNames.Capacity + ", " + popButtons); } if (popNames.Count != popCosts.Count) { throw new Exception("Number of pops different to number of prices"); } for (int i = 0; i < (popNames.Count); i++) { string name = popNames.ElementAt(i); int price = popCosts.ElementAt(i); VendingPop newPop = new VendingPop(name); newPop.setCost(price); acceptedPops.Add(newPop); } }
public void pressButton(int buttonIndex) { // get pop by button index VendingPop pop = specs.getPopByIndex(buttonIndex); //Console.WriteLine("Pop: " + pop.Name); // get pop price int price = pop.getCost(); // get inserted amount int val = coinChute.getInsertValue(); if (val < price) { // do nothing //Console.WriteLine("Not enough money: make another selection"); } else { // decrement pop from chute // to get actual pop in slot // returned pop may not be chosen pop if // slots were loaded incorrectly (this is // the desired functionality) Pop physicalPop = popChute.removePop(pop); if (physicalPop != null) { // calc change and dispense pop int change = val - price; //Console.WriteLine("Price: " + price + " Change: " + change); List <Coin> changeCoins = coinChute.releaseChange(change); dispenser.dispenseItems(physicalPop, changeCoins); // bank inserted money and reset inserted value coinChute.bankInsertValue(); } else { // if either no matching pop found, or // not enough pops then // only dispense change //Console.WriteLine("Not enough pops: make another selection"); // old change swallowing code //Console.WriteLine("Not enough pops: swallowing change"); //dispenser.dispenseItems(physicalPop, changeCoins); } } }
public VendingPop getPopByIndex(int index) { if (index >= popButtons) { throw new Exception("Invalid button: " + index); } int counter = 0; VendingPop returnPop = null; foreach (VendingPop pop in acceptedPops) { if (counter == index) { returnPop = pop; break; } counter++; } return(returnPop); }