/* * 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; }