private void ClearOrderFiles() { OrderedDrink.ClearFile(); OrderedPizza.ClearFile(); Order.ClearFile(); ClearOrder(); }
// methods /// <summary> /// saves the order data to a file /// </summary> /// <param name="drink"> ordered drink</param> /// <param name="quantity"> quantity of the ordered drinks</param> /// <returns></returns> public static int AddOrderedDrinkToFile(Drink drink, int quantity) { using (StreamWriter sw = File.AppendText(filename)) { sw.WriteLine(string.Join(", ", OrderedDrink.DrinkOrderToCSV(drink, quantity))); sw.Close(); } return(1); }
private void ReloadDrinks() { orderedDrinksLbx.Items.Clear(); List <OrderedDrink> drinks = OrderedDrink.GetAllDrinks(); foreach (OrderedDrink d in drinks) { orderedDrinksLbx.Items.Add(d); } }
/// <summary> /// Convert the order drinks to cSV /// </summary> /// <returns> csv string</returns> public string DrinksToCSV() { string result = "DRINKS"; result += Environment.NewLine; foreach (OrderedDrink d in drinks) { result += OrderedDrink.DrinkOrderToCSV(d.Drink, d.Quantity); result += Environment.NewLine; } return(result); }
/// <summary> /// Get all the saved drinks from file /// </summary> /// <returns> list of drinks </returns> public static List <OrderedDrink> GetAllDrinks() { List <OrderedDrink> drinks = new List <OrderedDrink>(); if (!File.Exists(filename)) { Utils.CreateEmptyFile(filename); } using (StreamReader file = new StreamReader(filename)) { string line; while ((line = file.ReadLine()) != null) { List <String> data = line.Split(',').ToList(); Drink drink = new Drink(1, data[0], float.Parse(data[1])); OrderedDrink ordered = new OrderedDrink(1, drink, Convert.ToInt32(data[2])); drinks.Add(ordered); } file.Close(); } return(drinks); }
/// <summary> /// Get all orders from a file /// </summary> /// <param name="filename"> the name of the file </param> /// <returns></returns> public static Shop OrdersFromFile(string filename) { Shop s = null; try { if (!File.Exists(filename)) { Utils.CreateEmptyFile(filename); } using (StreamReader file = new StreamReader(filename)) { string shopName = file.ReadLine(); s = new Shop(shopName); string line; List <Order> orders = new List <Order>(); while ((line = file.ReadLine()) != null) { if (line == "ORDER") { // get base information for the order line = file.ReadLine(); List <String> data = line.Split(',').ToList(); bool IsCancelled = Convert.ToBoolean(data[0]); DateTime createdAt = Convert.ToDateTime(data[1]); Customer c = null; if (data.Count == 4) { c = new Customer(1, data[2], data[3]); } else { c = new Customer(1, data[2]); } line = file.ReadLine(); if (line == "PIZZAS") { List <OrderedDrink> orderedDrinks = new List <OrderedDrink>();; List <OrderedPizza> orderedPizzas = new List <OrderedPizza>(); while ((line = file.ReadLine()) != "DRINKS") { List <String> pizzas = line.Split(',').ToList(); Pizza pizza = new Pizza(1, pizzas[0], float.Parse(pizzas[1]), float.Parse(pizzas[2])); OrderedPizza p = new OrderedPizza(pizza, Convert.ToInt32(pizzas[3]), Convert.ToBoolean(pizzas[4]), Convert.ToBoolean(pizzas[5]));; orderedPizzas.Add(p); } while ((line = file.ReadLine()) != "ENDORDER" && line != "") { List <String> drinks = line.Split(',').ToList(); Drink drink = new Drink(1, drinks[0], float.Parse(drinks[1])); OrderedDrink d = new OrderedDrink(drink, Convert.ToInt32(drinks[2])); orderedDrinks.Add(d); } Order o = new Order(c, IsCancelled, createdAt, orderedPizzas, orderedDrinks, true); orders.Add(o); } } } file.Close(); foreach (Order o in orders) { s.AddOrder(o); } } } catch (Exception) { if (filename != null) { using (File.Create(filename)); } } return(s); }