/// <summary> /// Method that controls the refilling, in which it both handle the refill and also ensures the correct refilling /// </summary> /// <param name="fillGums"></param> /// <returns>String which indicates the status of the filling</returns> public string FillMachine(List <Gum> fillGums) { if (storageBowl.GetNumberOfGums().Count == 55) { return("Machine is already full"); } Random rand = new Random(); while (storageBowl.GetNumberOfGums().Count != 55 && fillGums.Count > 0) { Gum bufferGum = fillGums[rand.Next(0, fillGums.Count)]; int availableSlots = SortGum(bufferGum, storageBowl.GetNumberOfGums()); List <Gum> sortedGums = new List <Gum>(); foreach (Gum item in fillGums) { if (item.Flavour == bufferGum.Flavour) { sortedGums.Add(item); } } List <Gum> finalList = new List <Gum>(); for (int i = 0; i < availableSlots; i++) { finalList.Add(sortedGums[i]); fillGums.Remove(sortedGums[i]); } storageBowl.FillMachine(finalList); } return("The machine has been filled"); }
/// <summary> /// Retrieves a random piece of gum from the list /// </summary> /// <returns>The randomly chosen piece of gum</returns> public Gum GetGum() { Random rand = new Random(); Gum gum = gums[rand.Next(0, gums.Count)]; gums.Remove(gum); return(gum); }
/// <summary> /// Simulate that the user takes the gum from the machine /// </summary> static void TakeTheGum() { Gum gum = household.gumballMachine.TakeGum(); if (gum != null) { Console.WriteLine("Allright, you just got a delicious {0} gum", gum.Flavour); } else { Console.WriteLine("I'm sorry, but there is no gum.."); } }
/// <summary> /// Handles the part where the gum is taken from the machine /// </summary> /// <returns></returns> public Gum TakeGum() { if (outputDrawer.Gum != null) { Gum gum = outputDrawer.Gum; outputDrawer.Gum = null; return(gum); } else { return(null); } }
/// <summary> /// Create the new batch, with the correct amount /// </summary> void GetNextBatchReady() { nextGumBatch.Clear(); for (int i = 0; i < 14; i++) { Gum gum = new Gum(); gum.Flavour = "Blueberry"; nextGumBatch.Add(gum); } for (int i = 0; i < 6; i++) { Gum gum = new Gum(); gum.Flavour = "Blackberry"; nextGumBatch.Add(gum); } for (int i = 0; i < 12; i++) { Gum gum = new Gum(); gum.Flavour = "TuttiFrutti"; nextGumBatch.Add(gum); } for (int i = 0; i < 10; i++) { Gum gum = new Gum(); gum.Flavour = "Orange"; nextGumBatch.Add(gum); } for (int i = 0; i < 8; i++) { Gum gum = new Gum(); gum.Flavour = "Strawberry"; nextGumBatch.Add(gum); } for (int i = 0; i < 5; i++) { Gum gum = new Gum(); gum.Flavour = "Apple"; nextGumBatch.Add(gum); } }
/// <summary> /// Priavte method used to sort the gum, and calculate the possible correct fillings /// </summary> /// <param name="gum">The gum trying to be filled</param> /// <param name="holding">The list of gum that is currently in the machine</param> /// <returns>An integer which indicates how many of the specific flavour there can be in the machine</returns> int SortGum(Gum gum, List <Gum> holding) { int minHolding = 0; int maxHolding = 0; List <Gum> currentHolding = new List <Gum>(); foreach (Gum gumItem in holding) { if (gumItem.Flavour == gum.Flavour) { currentHolding.Add(gumItem); } } switch (gum.Flavour) { case "Blueberry": minHolding = 12; maxHolding = 14; break; case "Blackberry": minHolding = 5; maxHolding = 7; break; case "TuttiFrutti": minHolding = 10; maxHolding = 12; break; case "Orange": minHolding = 9; maxHolding = 11; break; case "Strawberry": minHolding = 6; maxHolding = 8; break; case "Apple": minHolding = 4; maxHolding = 6; break; default: break; } if (currentHolding.Count == maxHolding) { return(0); } else if (currentHolding.Count < minHolding) { return(minHolding - currentHolding.Count); } else { return(1); } }