/* * The brewDrink method handles the logic-side details of ingredients and calls * brew if the ingredients exist to create the drink. * * Drink newDrink: is the drink to be made, defined by the Drink class * Returns: a display string for customer feedback */ public string brewDrink(Drink newDrink) { //Checks first that water is connected to the machine if (waterConnected) { //Checks the sugar and milk levels, if the drink requires them if(newDrink.SugarCount >= 1) { if (checkSugar(newDrink.SugarCount)) { } else return "Not enough sugar. Please contact administration to refill this ingredient."; } if (newDrink.MilkCount >= 1) { if (checkMilk(newDrink.MilkCount)) { } else return "Not enough milk. Please contact administration to refill this ingredient."; } //Coffee only needs to be checked for coffee drinks, which are given the type 1 to distinguish //them from other drinks if (newDrink.DrinkType == 1) { //In case a non-Coffee class drink is wrongly defined as coffee, we have //a try-catch try { Coffee coffee = (Coffee)newDrink; if (coffee.CoffeeCount >= 1) { if (checkCoffee(coffee.CoffeeCount)) { } else return "Not enough coffee. Please contact administration to refill this ingredient."; } } catch (Exception e) { return "Error in drink definition. Please contact the service helpdesk."; } } //Upon reaching this point, all ingredients can be assured if (newDrink.SugarCount >= 1) currentSugar -= 1; if (newDrink.MilkCount >= 1) currentMilk -= newDrink.MilkCount; if (newDrink.DrinkType == 1) { Coffee coffee = (Coffee)newDrink; currentCoffee -= coffee.CoffeeCount; } //The payment goes through, or the coins are moved to main storage machineTotal = paymentReceived - newDrink.DrinkPrice; paymentReceived = 00.00m; //Imaginary code to return excess customer funds //Calls brew, to actually brew the drink brew(newDrink); return "Success!"; } else return "Water not connected. Please contact the service helpdesk."; }
/* * brewDrink checks if the drink is paid for (according to payment received successfully * by the machine) and then call upon the machine to brew the drink. On failure an error * message is returned, other the message "Success!" is returned. */ public bool brewDrink(Drink newDrink) { string message = ""; if (myMachine.isPaidFor(newDrink)) { message = myMachine.brewDrink(newDrink); if (message.Equals("Success!")) { myCoffeeMachineForm.displayMessage("Your " + newDrink.DrinkName + " is ready. Enjoy!", "Message", false); return true; } else { myCoffeeMachineForm.displayMessage(message, "Important", true); } } else myCoffeeMachineForm.displayMessage("Additional payment is required.", "Message", false); return false; }
/* * The initialise method fills the drink list with drinks. It first attempts to do this * using a database, but failing that, uses hardcoded default drinks */ private void initialise() { bool success = false; //Test code for entering values into the database: //string query = "INSERT INTO drinks (idDrinks, DrinksName, DrinksDesc, DrinksType,DrinksPrice, UsesWater, IsCarbonated, CountSugar, CountMilk, CountCoffee) VALUES('001', 'Carbonated Water', 'A nice cold glass of carbonated water.', '0', '02.00', '1', '1', '0', '0', '0')"; //drinkDatabase.Insert(query); //Fill the drink list from the database if (drinkDatabase.fillDictionary()) { myDrinkList = drinkDatabase.DrinkList; } //If the drink list contains values, the read from database is assumed to have been a success if (myDrinkList != null) success = true; //If initialising from the database failed, this fills myDrinkList with default values if (!success) { myDrinkList = new Dictionary<int, Drink>(); //Drink: id, name, desc, type, price, water use, carbonation, sugar amount, milk amount Drink coldWater = new Drink (000, "Cold Water", "A nice cold glass of water.", 0, 01.00m, 1, false, 0, 0); myDrinkList.Add(000, coldWater); Drink carbonatedWater = new Drink (001, "Carbonated Water", "A nice cold glass of carbonated water.", 0, 02.00m, 1, true, 0, 0); myDrinkList.Add(001, carbonatedWater); Drink hotWater = new Drink (002, "Tea", "A nice hot cup of tea water.", 0, 05.00m, 2, false, 0, 0); myDrinkList.Add(002, hotWater); Drink milk = new Drink (003, "Milk", "A nice cold glass of milk.", 0, 05.00m, 0, false, 0, 1); myDrinkList.Add(003, milk); //Coffee: id, name, desc, type, price, water use, carbonation, sugar amount, milk amount, coffee amount Coffee blackCoffee = new Coffee (010, "Black Coffee", "A steaming cup of black coffee.", 1, 10.00m, 2, false, 0, 0, 1); myDrinkList.Add(010, blackCoffee); Coffee sugarCoffee = new Coffee (011, "Coffee with Sugar", "A steaming cup of coffee with added sugar.", 1, 15.00m, 2, false, 1, 0, 1); myDrinkList.Add(011, sugarCoffee); Coffee milkCoffee = new Coffee (012, "Coffee with Milk", "A steaming cup of coffee with added milk.", 1, 15.00m, 2, false, 0, 1, 1); myDrinkList.Add(012, milkCoffee); Coffee milkSugarCoffee = new Coffee (013, "House Coffee", "A steaming cup of coffee with added sugar and milk.", 1, 20.00m, 2, false, 1, 1, 1); myDrinkList.Add(013, milkSugarCoffee); } }
/* * The fillDictionary method fills the Dictionary myDrinkList with data from the * database. */ public bool fillDictionary() { string query = "SELECT * FROM drinks"; //Open connection if (this.OpenConnection() == true) { myDrinkList = new Dictionary<int, Drink>(); //Create Command MySqlCommand cmd = new MySqlCommand(query, connection); //Create a data reader and Execute the command MySqlDataReader dataReader = cmd.ExecuteReader(); DataTable tableDrinks = new DataTable(); tableDrinks.Load(dataReader); foreach (DataRow row in tableDrinks.Rows) { int id = (int)row["idDrinks"]; string name = (string)row["DrinksName"]; string desc = (string)row["DrinksDesc"]; int type = (int)row["DrinksType"]; decimal price = (decimal)row["DrinksPrice"]; int water = (int)row["UsesWater"]; int carbo = (int)row["IsCarbonated"]; int sugar = (int)row["CountSugar"]; int milk = (int)row["CountMilk"]; int coffee = (int)row["CountCoffee"]; bool bCarbo = false; Drink tempDrink = null; Coffee tempCoffee = null; if (carbo > 0) bCarbo = true; if (type == 0) { tempDrink = new Drink(id, name, desc, type, price, water, bCarbo, sugar, milk); myDrinkList.Add(id, tempDrink); } else { tempCoffee = new Coffee(id, name, desc, type, price, water, bCarbo, sugar, milk, coffee); myDrinkList.Add(id, tempCoffee); } } //close Data Reader dataReader.Close(); //close Connection this.CloseConnection(); return true; } return false; }
/* * Brews the drink */ private bool brew(Drink newDrink) { //Imaginary machine code for actually brewing the drink return true; }
/* * The checkPayment method checks that enough payment has been received by the * machine for the current drink. * * Drink newDrink: is the drink to be made, defined by the Drink class * Returns: a display string for customer feedback */ public bool isPaidFor(Drink newDrink) { if (paymentReceived >= newDrink.DrinkPrice) return true; else return false; }
/* * On selecting an item in the combo box, the price is displayed for the customer, and * a description appears on the info label. */ private void comboBoxDrinkList_SelectedIndexChanged(object sender, EventArgs e) { selectedDrinkName = comboBoxDrinkList.Items[comboBoxDrinkList.SelectedIndex].ToString(); //Searching for a matching drink name from the list - requiring names to be unique. foreach (Drink drink in drinkCollection) { if(drink.DrinkName.Equals(selectedDrinkName)) { labelPriceAmount.Text = drink.DrinkPrice.ToString(); selectedDrinkPrice = drink.DrinkPrice; labelInfo.Text = drink.DrinkDesc; selectedDrink = drink; } } }