/// <summary> /// Creates DishInOrder instance for Order object /// </summary> /// <param name="dishId">Id of the dish to be added</param> /// <param name="quantity">Quantity of the dish to added</param> /// <returns>instance of DishInOrder</returns> public static DishInOrder AddDish(int dishId, int quantity) { DishInOrder dishInOrder = new DishInOrder(); dishInOrder.DishID = dishId; dishInOrder.Quantity = quantity; return(dishInOrder); }
private void buttonAddDish_Click(object sender, RoutedEventArgs e) { if (comboBoxProducts.SelectedValue != null && textBoxProductQuantity.Text != null && int.TryParse(textBoxProductQuantity.Text, out int j) && j > 0) { try { Dish selectedDish = availableDishes.SingleOrDefault (d => d.Id == int.Parse(comboBoxProducts.SelectedValue.ToString())); var dishForAdding = new DishInOrder(); var dishForDisplaying = new DishInOrder(); dishForAdding.Quantity = int.Parse(textBoxProductQuantity.Text); dishForAdding.DishID = selectedDish.Id; dishForAdding.OrderID = currentOrder.Id; dishForDisplaying.Dish = selectedDish; dishForDisplaying.Order = currentOrder; dishForDisplaying.Quantity = int.Parse(textBoxProductQuantity.Text); if (_orderDishes.SingleOrDefault(od => od.DishID == selectedDish.Id) == null) { _orderDishes.Add(dishForAdding); for (int i = 0; i < dishForDisplaying.Quantity; i++) { _displayedDishes.Add(dishForDisplaying.Dish); } } else { _orderDishes.SingleOrDefault(od => od.DishID == selectedDish.Id).Quantity += int.Parse(textBoxProductQuantity.Text); for (int i = 0; i < int.Parse(textBoxProductQuantity.Text); i++) { _displayedDishes.Add(dishForDisplaying.Dish); } } } catch (InvalidOperationException ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("Specify correct input for a dish and its quantity.", "Warning", MessageBoxButton.OK, MessageBoxImage.Asterisk); } }