// This method will remove a can of the specified flavor from the rack. public void RemoveACanOf(string FlavorOfCanToBeRemoved) { FlavorOfCanToBeRemoved = FlavorOfCanToBeRemoved.ToUpper(); if (IsEmpty(FlavorOfCanToBeRemoved)) { Debug.WriteLine($"Error: Failed attempt to remove a can of {FlavorOfCanToBeRemoved} from an empty rack"); } else { // convert the string Flavor into the appropriate int value Flavor flavorEnumeral = FlavorOps.ToFlavor(FlavorOfCanToBeRemoved); //if (Enum.IsDefined(typeof(Flavor),FlavorOfCanToBeRemoved)) if (rack.ContainsKey(flavorEnumeral)) { //Write success to the debug log Debug.WriteLine("removing a can of {0} flavored soda from the rack", FlavorOfCanToBeRemoved, DUMMYARGUMENT); //int flavorIndex = (int)flavorEnumeral; rack[flavorEnumeral]--; } else { Debug.WriteLine("Error: attempt to remove a can of unknown flavor {0}", FlavorOfCanToBeRemoved, DUMMYARGUMENT); } } }
// This method adds a can of the specified flavor to the rack. public void AddACanOf(string FlavorOfCanToBeAdded) { FlavorOfCanToBeAdded = FlavorOfCanToBeAdded.ToUpper(); if (IsFull(FlavorOfCanToBeAdded)) { Debug.WriteLine("*** Failed attempt to add a can of {0} to a full rack", FlavorOfCanToBeAdded, DUMMYARGUMENT); } else { // convert the string Flavor into the appropriate int value Flavor flavorEnumeral = FlavorOps.ToFlavor(FlavorOfCanToBeAdded); //Check to see if the rack has the flavor value in it if (rack.ContainsKey(flavorEnumeral)) { //Write success to the debug log Debug.WriteLine($"Info: Adding a can of {flavorEnumeral} flavored soda to the rack"); //Flavor flavorIndex = flavorEnumeral; rack[flavorEnumeral]++; } else { Debug.WriteLine("Error: attempt to add a can of unknown flavor {0}", FlavorOfCanToBeAdded, DUMMYARGUMENT); } } }
// OPTIONAL – return true if the rack is empty of a specified flavor // false otherwise public Boolean IsEmpty(string FlavorOfBinToBeChecked) { FlavorOfBinToBeChecked = FlavorOfBinToBeChecked.ToUpper(); bool result = false; // convert the string Flavor into the appropriate int value Flavor flavorEnumeral = FlavorOps.ToFlavor(FlavorOfBinToBeChecked); if (rack.ContainsKey(flavorEnumeral)) { // Debug.WriteLine("Checking if can rack is empty of flavor {0}", FlavorOfBinToBeChecked); result = rack[flavorEnumeral] == EMPTYBIN; } else { Debug.WriteLine("Error: attempt to check rack status of unknown flavor {0}", FlavorOfBinToBeChecked, DUMMYARGUMENT); } return(result); }
// This public void will empty the rack of a given flavor. // This takes a slightly different approach and sometimes uses f and sometimes uses FlavorOps.ToFlavor on the fly public void EmptyCanRackOf(string FlavorOfBinToBeEmptied) { //Convert the string value to all uppercase to purify input FlavorOfBinToBeEmptied = FlavorOfBinToBeEmptied.ToUpper(); if (rack.ContainsKey(FlavorOps.ToFlavor(FlavorOfBinToBeEmptied))) { //Convert string to type Flavor using FlavorOps Flavor f = FlavorOps.ToFlavor(FlavorOfBinToBeEmptied); //Write success to the debug log Debug.WriteLine($"Info: Emptying can rack of flavor {FlavorOfBinToBeEmptied}"); //Do the actual emptying of the rack rack[f] = EMPTYBIN; } else { Debug.WriteLine($"Error: Illegal attempt to empty bin of invalid flavor {FlavorOfBinToBeEmptied}"); } }
// OPTIONAL – returns true if the rack is full of a specified flavor // false otherwise public Boolean IsFull(string FlavorOfBinToBeChecked) { FlavorOfBinToBeChecked = FlavorOfBinToBeChecked.ToUpper(); Boolean result = false; // convert the string Flavor into the appropriate int value Flavor flavorEnumeral = FlavorOps.ToFlavor(FlavorOfBinToBeChecked); if (rack.ContainsKey(flavorEnumeral)) { //Write success to the debug log Debug.WriteLine($"Info: Checking if can rack is full of flavor {FlavorOfBinToBeChecked}"); //int flavorIndex = (int)flavorEnumeral; result = rack[flavorEnumeral] == BINSIZE; } else { Debug.WriteLine("Error: attempt to check rack status of unknown flavor {0}", FlavorOfBinToBeChecked, DUMMYARGUMENT); } return(result); }
static void Main(string[] args) { PurchasePrice sodaPrice = new PurchasePrice(0.35M); CanRack sodaRack = new CanRack(); 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) { // get the coin inserted string coinNameInserted = Console.ReadLine().ToUpper(); Coin coinInserted = new Coin(coinNameInserted); Console.WriteLine("You have inserted a {0} worth {1:c}", coinInserted, coinInserted.ValueOf); // 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) { Console.Write("What flavor would you like? : "); // oooh, this looks like trouble. Why? //Needs exception handling Flavor flavor = new Flavor(); bool flavorFound = false; while (flavorFound == false) //I think this will work for exception handling? { try { //ask the user for flavor string flavorName = Console.ReadLine().ToUpper(); flavor = FlavorOps.ToFlavor(flavorName); //If this parse is successful... flavorFound = true; //... this WILL execute and get us out of the loop. Maybe? } catch (System.ArgumentException e) { Console.WriteLine($"{e.Message}" + " Please try again."); } } if (!sodaRack.IsEmpty(flavor)) { sodaRack.RemoveACanOf(flavor); Console.WriteLine("Thanks, here is your can of {0}.", flavor); canDispensed = true; } else { Console.WriteLine("We are out of {0}", flavor); } } Console.Write("Exit the vending machine? (y/n): "); string response = Console.ReadLine(); timeToExit = response.Trim().ToUpper().StartsWith("Y"); } while (!timeToExit); }