public Boolean Transfer(CoinBox Destination, decimal Amount, Boolean ExceedIfRequired) { Boolean result = false; // start out assuming transfer didn't happen if (this.ValueOf >= Amount) { decimal amountRemoved = 0M; decimal amountDeposited = 0M; decimal amountLeftToTransfer = Amount; // try to withdraw exact amount and deposit it into Destination foreach (Coin.Denomination denominationEnumeral in reversedCoinList) { Coin aCoin = new Coin(denominationEnumeral); amountDeposited = 0M; amountRemoved = this.removeCoinDenomiation(amountLeftToTransfer, aCoin.DenominationEnumeral); while (amountDeposited < amountRemoved) { Destination.Deposit(aCoin); amountDeposited += aCoin.ValueOf; } amountLeftToTransfer -= amountRemoved; } // Nothing left to transfer? Success. if (amountLeftToTransfer <= 0M) { result = true; } // withdraw an overage if necessary and deposit it into Destination else if (amountLeftToTransfer > 0M && ExceedIfRequired) { foreach (Coin.Denomination denominationEnumeral in Coin.AllDenominations) { while (amountLeftToTransfer > 0M) { if (this.coinCount(denominationEnumeral) > 0) { this.Withdraw(denominationEnumeral); Coin aCoin = new Coin(denominationEnumeral); Destination.Deposit(aCoin); amountLeftToTransfer -= aCoin.ValueOf; } else { break; // there are no coins of this denomination: go to next denomination } } } // Transfer done result = true; } } else // value of source coinbox less than requested transfer amount: no transfer done { result = false; } return(result); }
public decimal Transfer(CoinBox Destination) { decimal result = 0M; int count = box.Count; for (int index = 0; index < count; index++) { result += box[0].ValueOf; Destination.Deposit(box[0]); this.box.Remove(box[0]); } return(result); }
public VendingMachine() { InitializeComponent(); _canRack = new CanRack(); _canRack.FillTheCanRack(); _coinBoxTransaction = new CoinBox(); _sodaPrice = new PurchasePrice(35); labelCredit.Text = String.Format("{0:c}", _coinBoxTransaction.ValueOf); labelExactChange.Text = "Exact Change Required"; labelExactChange.Visible = false; labelGeneralMessage.Text = "Welcome"; buttonLemon.Enabled = false; buttonOrange.Enabled = false; buttonRegular.Enabled = false; //create the locked coin box object _coinBoxLocked = new CoinBox(); //deposit a couple of coins from each denomination in the coinbox to make change foreach (Coin.Denomination cn in Coin.AllDenominations) { for (int i = 0; i < 2; i++) { _coinBoxLocked.Deposit(new Coin(cn)); } } //create listView for coins in the locked coin box initializeCoinBoxDisplay(listViewCoinBoxLocked); updateCoinBoxDisplayData(listViewCoinBoxLocked, _coinBoxLocked); //create listView for inventory initializeInventoryDisplay(); updateInventoryData(); _foodLocker = new FoodLocker(10); //create listView for snacks initializeSnackDisplay(); updateSnackData(); }
public Boolean Transfer(CoinBox Destination, decimal Amount, Boolean ExceedIfRequired) { Boolean result = false; // start out assuming transfer didn't happen if (this.ValueOf >= Amount) { decimal amountRemoved = 0M; decimal amountDeposited = 0M; decimal amountLeftToTransfer = Amount; // try to withdraw exact amount and deposit it into Destination foreach (Coin.Denomination denominationEnumeral in reversedCoinList) { Coin aCoin = new Coin(denominationEnumeral); amountDeposited = 0M; amountRemoved = this.removeCoinDenomiation(amountLeftToTransfer, aCoin.DenominationEnumeral); while (amountDeposited < amountRemoved) { Destination.Deposit(aCoin); amountDeposited += aCoin.ValueOf; } amountLeftToTransfer -= amountRemoved; } // Nothing left to transfer? Success. if (amountLeftToTransfer <= 0M) { result = true; } // withdraw an overage if necessary and deposit it into Destination else if (amountLeftToTransfer > 0M && ExceedIfRequired) { foreach (Coin.Denomination denominationEnumeral in Coin.AllDenominations) { while (amountLeftToTransfer > 0M) { if (this.coinCount(denominationEnumeral) > 0) { this.Withdraw(denominationEnumeral); Coin aCoin = new Coin(denominationEnumeral); Destination.Deposit(aCoin); amountLeftToTransfer -= aCoin.ValueOf; } else { break; // there are no coins of this denomination: go to next denomination } } } // Transfer done result = true; } } else // value of source coinbox less than requested transfer amount: no transfer done { result = false; } return result; }
public decimal Transfer(CoinBox Destination) { decimal result = 0M; int count = box.Count; for (int index = 0; index < count; index++) { result += box[0].ValueOf; Destination.Deposit(box[0]); this.box.Remove(box[0]); } return result; }
static void Main(string[] args) { PurchasePrice sodaPrice = new PurchasePrice(0.35M); CanRack sodaRack = new CanRack(); CoinBox changeBox = new CoinBox(new List<Coin> { new Coin(Coin.Denomination.QUARTER), new Coin(Coin.Denomination.DIME), new Coin(Coin.Denomination.NICKEL), new Coin(Coin.Denomination.QUARTER), new Coin(Coin.Denomination.QUARTER), new Coin(Coin.Denomination.DIME) }); Console.WriteLine("Welcome to the .NET C# Soda Vending Machine"); Boolean timeToExit = false; do { sodaRack.DisplayCanRack(); Console.Write("Please insert {0:c} worth of coins: ", sodaPrice.PriceDecimal); decimal totalValueInserted = 0M; while (totalValueInserted < sodaPrice.PriceDecimal) { Coin coinInserted = null; while (coinInserted == null) { try { // get the coin inserted string coinNameInserted = Console.ReadLine().ToUpper(); coinInserted = new Coin(coinNameInserted); } catch (System.ArgumentException) { Console.WriteLine("That's not a recognized coin. Please retry."); } } Console.WriteLine("You have inserted a {0} worth {1:c}", coinInserted, coinInserted.ValueOf); changeBox.Deposit(coinInserted); // running total of the value of the coins inserted totalValueInserted += coinInserted.ValueOf; Console.WriteLine("Total value inserted is {0:c}", totalValueInserted); } // select a flavor of soda Boolean canDispensed = false; while (!canDispensed) { Flavor flavorEnumeral = Flavor.REGULAR; Boolean flavorChosen = false; Console.Write("What flavor would you like? : "); while (!flavorChosen) { try { // get the flavor request string flavorName = Console.ReadLine().ToUpper(); // Well, this used to be trouble. flavorEnumeral = FlavorOps.ToFlavor(flavorName); flavorChosen = true; } catch (System.ArgumentException) { Console.WriteLine("That's not a recognized flavor. Please retry."); } catch (VENDBADFLAVORException ve) { Console.WriteLine(ve.Message); Console.WriteLine("Please retry"); } } if (!sodaRack.IsEmpty(flavorEnumeral)) { sodaRack.RemoveACanOf(flavorEnumeral); Console.WriteLine("Thanks, here is your can of {0}.", flavorEnumeral); canDispensed = true; } else { Console.WriteLine("We are out of {0}", flavorEnumeral); } } Console.Write("Exit the vending machine? (y/n): "); string response = Console.ReadLine(); timeToExit = response.Trim().ToUpper().StartsWith("Y"); } while (!timeToExit); Console.WriteLine("Contents of Coin Box:"); Console.WriteLine("{0}\tHalf Dollar(s)", changeBox.HalfDollarCount); Console.WriteLine("{0}\tQuarter(s)", changeBox.QuarterCount); Console.WriteLine("{0}\tDime(s)", changeBox.DimeCount); Console.WriteLine("{0}\tNickel(s)", changeBox.NickelCount); Console.WriteLine("{0}\tSlug(s)", changeBox.SlugCount); Console.WriteLine(); Console.WriteLine("Total value in coin box is {0:c}", changeBox.ValueOf); }
static void Main(string[] args) { PurchasePrice sodaPrice = new PurchasePrice(0.35M); CanRack sodaRack = new CanRack(); CoinBox changeBox = new CoinBox(new List <Coin> { new Coin(Coin.Denomination.QUARTER), new Coin(Coin.Denomination.DIME), new Coin(Coin.Denomination.NICKEL), new Coin(Coin.Denomination.QUARTER), new Coin(Coin.Denomination.QUARTER), new Coin(Coin.Denomination.DIME) }); Console.WriteLine("Welcome to the .NET C# Soda Vending Machine"); Boolean timeToExit = false; do { sodaRack.DisplayCanRack(); Console.Write("Please insert {0:c} worth of coins: ", sodaPrice.PriceDecimal); decimal totalValueInserted = 0M; while (totalValueInserted < sodaPrice.PriceDecimal) { Coin coinInserted = null; while (coinInserted == null) { try { // get the coin inserted string coinNameInserted = Console.ReadLine().ToUpper(); coinInserted = new Coin(coinNameInserted); } catch (System.ArgumentException) { Console.WriteLine("That's not a recognized coin. Please retry."); } } Console.WriteLine("You have inserted a {0} worth {1:c}", coinInserted, coinInserted.ValueOf); changeBox.Deposit(coinInserted); // running total of the value of the coins inserted totalValueInserted += coinInserted.ValueOf; Console.WriteLine("Total value inserted is {0:c}", totalValueInserted); } // select a flavor of soda Boolean canDispensed = false; while (!canDispensed) { Flavor flavorEnumeral = Flavor.REGULAR; Boolean flavorChosen = false; Console.Write("What flavor would you like? : "); while (!flavorChosen) { try { // get the flavor request string flavorName = Console.ReadLine().ToUpper(); // Well, this used to be trouble. flavorEnumeral = FlavorOps.ToFlavor(flavorName); flavorChosen = true; } catch (System.ArgumentException) { Console.WriteLine("That's not a recognized flavor. Please retry."); } catch (VENDBADFLAVORException ve) { Console.WriteLine(ve.Message); Console.WriteLine("Please retry"); } } if (!sodaRack.IsEmpty(flavorEnumeral)) { sodaRack.RemoveACanOf(flavorEnumeral); Console.WriteLine("Thanks, here is your can of {0}.", flavorEnumeral); canDispensed = true; } else { Console.WriteLine("We are out of {0}", flavorEnumeral); } } Console.Write("Exit the vending machine? (y/n): "); string response = Console.ReadLine(); timeToExit = response.Trim().ToUpper().StartsWith("Y"); } while (!timeToExit); Console.WriteLine("Contents of Coin Box:"); Console.WriteLine("{0}\tHalf Dollar(s)", changeBox.HalfDollarCount); Console.WriteLine("{0}\tQuarter(s)", changeBox.QuarterCount); Console.WriteLine("{0}\tDime(s)", changeBox.DimeCount); Console.WriteLine("{0}\tNickel(s)", changeBox.NickelCount); Console.WriteLine("{0}\tSlug(s)", changeBox.SlugCount); Console.WriteLine(); Console.WriteLine("Total value in coin box is {0:c}", changeBox.ValueOf); }