/// <summary> /// Method for updating a specific element in either stock or menu lists /// </summary> /// <param name="table">Updatable table name. Can be STOCK or MENU only.</param> static public void UpdateElement(string table) { try { if (table.Equals("STOCK")) { Console.Write(" Enter Id of the element you want to update: "); int updatableID = int.Parse(Console.ReadLine()); if (stock.DoesItemExistsByID(updatableID)) { Console.Write(" Input new items name: "); string name = Console.ReadLine(); Console.Write(" Input new items portion count in stock: "); double portionCount = double.Parse(Console.ReadLine()); Console.Write(" Input new items unit: "); string unit = Console.ReadLine(); Console.Write(" Input new items portion size: "); double portionSize = double.Parse(Console.ReadLine()); StockItem newItem = new StockItem(updatableID, name, portionCount, unit, portionSize); UpdateElement(stock, newItem, StockCsv); } else { Console.WriteLine(" Element with specified ID does not exist!"); } } else if (table.Equals("MENU")) { Console.Write(" Enter Id of the element you want to update: "); int updatableID = int.Parse(Console.ReadLine()); if (menu.DoesItemExistsByID(updatableID)) { Console.Write(" Input new items name: "); string name = Console.ReadLine(); Console.Write(" Input new items products IDs, separated by space: "); string[] products = Console.ReadLine().Split(' '); /// Parse product list List <int> productList = new List <int>(); foreach (string i in products) { productList.Add(int.Parse(i)); } MenuItem item = new MenuItem(updatableID, name, productList); UpdateElement(menu, item, MenuCsv); } else { Console.WriteLine(" Element with specified ID does not exist!"); } } } catch (FormatException) { Console.WriteLine("\r\n Incorrect numeric input! Please try again!"); Console.WriteLine(" Stopping update...\r\n"); } catch (IOException) { Console.WriteLine("\r\n One of the files is open! Close the .csv files and try again!"); Console.WriteLine(" Stopping update...\r\n"); } }
/// <summary> /// Method for adding new elements to corresponding objects (Stock, Menu, Orders) /// </summary> /// <param name="table">Addable object name, given by switch case statement</param> static public void AddNewElement(string table) { try { /// Check if adding to stock table if (table.Equals("STOCK")) { /// Read entry Console.WriteLine(" Beginning input of new stock item..."); RetryStock: Console.Write(" Enter new items ID: "); int newId = int.Parse(Console.ReadLine()); /// Check if there is an item with the same Id if (stock.DoesItemExistsByID(newId)) { Console.WriteLine(" Item with this ID exists. Enter a new ID."); goto RetryStock; } Console.Write(" Input new items name: "); string name = Console.ReadLine(); Console.Write(" Input new items portion count in stock: "); double portionCount = double.Parse(Console.ReadLine()); Console.Write(" Input new items unit: "); string unit = Console.ReadLine(); Console.Write(" Input new items portion size: "); double portionSize = double.Parse(Console.ReadLine()); StockItem newItem = new StockItem(newId, name, portionCount, unit, portionSize); AddElement(stock, newItem, StockCsv); } /// Check if adding to menu table else if (table.Equals("MENU")) { /// Read entry Console.WriteLine(" Beginning input of new menu item..."); RetryMenu: Console.Write(" Enter new items ID: "); int newId = int.Parse(Console.ReadLine()); /// Check if there is an item with the same Id if (menu.DoesItemExistsByID(newId)) { Console.WriteLine(" Item with this ID exists. Enter a new ID."); goto RetryMenu; } Console.Write(" Input new items name: "); string name = Console.ReadLine(); Console.Write(" Input new items products IDs, separated by space: "); string[] products = Console.ReadLine().Split(' '); /// Parse product list List <int> productList = new List <int>(); foreach (string i in products) { productList.Add(int.Parse(i)); } MenuItem item = new MenuItem(newId, name, productList); AddElement(menu, item, MenuCsv); } /// Check if adding to orders table else if (table.Equals("ORDERS")) { Console.WriteLine(" Beginning input of new order item..."); RetryOrder: Console.Write(" Enter new order ID: "); int newId = int.Parse(Console.ReadLine()); /// Check if there is an item with the same Id if (orders.DoesItemExistsByID(newId)) { Console.WriteLine(" Item with this ID exists. Enter a new ID."); goto RetryOrder; } Console.Write(" Input date time, format YYYY-MM-DD HH:mm:ss, " + "or leave blank for current time: "); string line = Console.ReadLine(); DateTime time; if (line.Equals("")) { time = DateTime.Now; } else { time = DateTime.Parse(line); } RetryOrderItems: /// Parse entered menu items List <MenuItem> orderedItems = new List <MenuItem>(); Console.Write(" Input ordered menu items, separated by space: "); string[] items = Console.ReadLine().Split(' '); /// Checking all items... foreach (string i in items) { int index = int.Parse(i); MenuItem addable = menu.GetItemByID(index); if (addable != null) { orderedItems.Add(addable); } else { Console.WriteLine(" Error parsing menu item! Please check if it exists."); goto RetryOrderItems; } } OrderItem item = new OrderItem(newId, time, orderedItems); /// Check if possible to add an order or shall it be cancelled AddElement(item, OrdersCsv, StockCsv, stock, orders); } } catch (FormatException) { Console.WriteLine("\r\n Incorrect numeric input! Please try again!"); Console.WriteLine(" Stopping insertion...\r\n"); } catch (IOException) { Console.WriteLine("\r\n One of the files is open! Close the .csv files and try again!"); Console.WriteLine(" Stopping insertion...\r\n"); } catch (NullReferenceException) { Console.WriteLine("\r\n An order contains menu items with no items in stock!"); Console.WriteLine(" Stopping insertion...\r\n"); } }