private void btnCheckout_Click(object sender, EventArgs e) { Student student = null; bool error = false; //get the student and the drink by the name in the combo box if (cmbOrdStudents.SelectedIndex >= 0) { student = studentService.GetByName(cmbOrdStudents.SelectedItem.ToString()); } else { MessageBox.Show("You need to select a student", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); error = true; } Drink drink = null; if (cmbOrdDrinks.SelectedIndex >= 0) { drink = drinkService.GetByName(cmbOrdDrinks.SelectedItem.ToString()); } else { MessageBox.Show("You need to select a drink", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); error = true; } if (!System.Text.RegularExpressions.Regex.IsMatch(txtQuantity.Text, "[0-9]")) { MessageBox.Show("Wrong input format for quantity!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { int quantityT = int.Parse(txtQuantity.Text); if (quantityT <= 0) { MessageBox.Show("Quantity can't be equal or lower than 0!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (!error) { //check if there are enough drinks available if (drink.Stock - quantityT >= 0) { drink.Stock -= quantityT; drinkService.UpdateDrink(drink, drink.Stock, drink.Name, drink.Price); decimal totalPriceT = (decimal)quantityT * drink.Price; //create new order CashRegister order = new CashRegister() { student = student, drink = drink, quantity = quantityT, totalPrice = totalPriceT, orderDate = DateTime.Now }; //get the years difference from now to the birth date int age = DateTime.Now.Year - order.student.BirthDate.Year; //if his birthdate was already this year, add 1 year to the age if (DateTime.Now.Month > order.student.BirthDate.Month) { age++; } if (age < 18 && drink.drinkType) { MessageBox.Show("You can't buy alcohol if you are underage!", "Underage", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { DialogResult Result = MessageBox.Show("Total Price: " + totalPriceT + "\nAre you sure?", "Proceed", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (Result == DialogResult.Yes) { cashRegisterService.AddOrder(order); } } } } else { MessageBox.Show("Out of Stock ", "Out of Stock", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
private void btnCheckout_Click(object sender, EventArgs e) { try { Student student = null; if (cmbStudents.SelectedIndex >= 0) { student = studentService.GetByName(cmbStudents.SelectedItem.ToString()); } Drink drink = null; if (cmbDrinks.SelectedIndex >= 0) { drink = drinkService.GetByName(cmbDrinks.SelectedItem.ToString()); } //check for stock if (drink.StockAmount >= 0) { //create new order Transaction transaction = new Transaction() { student = student, drink = drink, transactionDate = DateTime.Now }; //check for birthdate (alcoholic) int age = DateTime.Now.Year - transaction.student.BirthDate.Year; if (DateTime.Now.Day > transaction.student.BirthDate.Day) { age += 1; } if (age < 18 && drink.VATID == 2) { MessageBox.Show("You cannot buy alcohol yet!"); } else { DialogResult print = MessageBox.Show($"Price (incl. VAT): {drink.PriceInclVAT} \nProceed?", "", MessageBoxButtons.YesNo); if (print == DialogResult.Yes) { transactionService.AddTransaction(transaction); drink.StockAmount--; drink.SalesCount++; drinkService.updateDrink(drink); } } } else { MessageBox.Show("This product is currently out of stock."); } } catch (Exception exc) { MessageBox.Show(exc.Message); } Close(); }