/** * when clicking on "adding the bottle", check if every parameter needed is present and if the datatype is correct */ private void btnAdd_Click(object sender, EventArgs e) { //initializing variables to stock informations from the wine bottle string wineName = ""; string color = ""; string manufacturer = ""; string description = ""; string storage = ""; int number = 0; double volume = 0; int year = 0; List <string> varietal = new List <string>(); // initializing variables to compare the logic behind some values int actualYear = DateTime.Now.Year; // initializing the boolean to check if everything is correct -> all is false by default, so that if it works, it changes to true bool successName = false; bool successColor = false; bool successManu = false; bool successStorage = false; bool successDescription = false; bool successNumber = false; bool successYear = false; bool successVolume = false; bool successVarietal = false; bool successFormat = false; bool successPresence = false; bool successAdd = false; bool successLog = false; /** * verification of data type * if a field is empty and shouldn't be -> the bottle won't be added */ if (txtBottleName.Text != "") { wineName = txtBottleName.Text; successName = true; } if (comboColor.SelectedIndex != -1) { color = comboColor.SelectedItem.ToString(); successColor = true; } if (comboVolume.SelectedIndex != -1) { successVolume = Double.TryParse(comboVolume.SelectedItem.ToString(), out volume); } if (comboManufacturer.SelectedIndex != -1) { manufacturer = comboManufacturer.SelectedItem.ToString(); successManu = true; } if (txtNumber.Text != "") { successNumber = Int32.TryParse(txtNumber.Text, out number); if (number < 1) { MessageBox.Show("Le nombre de bouteilles spécifié n'est pas valide."); txtNumber.Text = ""; } } if (comboStorage.SelectedIndex != -1) { storage = comboStorage.SelectedItem.ToString(); successStorage = true; } if (rtxtDescription.Text != "" && rtxtDescription.TextLength < 200) { description = rtxtDescription.Text; successDescription = true; } if (txtYear.Text != "") { successYear = Int32.TryParse(txtYear.Text, out year); if (year < 1900 || year > actualYear) { MessageBox.Show("L'année de production n'est pas valide."); txtYear.Text = ""; } } if (comboVariety1.SelectedIndex != -1 || comboVariety2.SelectedIndex != -1 || comboVariety3.SelectedIndex != -1) { successVarietal = true; if (comboVariety1.SelectedIndex != -1) { varietal.Add(comboVariety1.SelectedItem.ToString()); } if (comboVariety2.SelectedIndex != -1) { varietal.Add(comboVariety2.SelectedItem.ToString()); } if (comboVariety3.SelectedIndex != -1) { varietal.Add(comboVariety3.SelectedItem.ToString()); } } successYear = Int32.TryParse(txtYear.Text, out year); //verify if the bottle to add already exists in database -> true : present, false : non present // occur uniquely if the volume, the name and the year have been validated if (successVolume && successName && successYear) { successPresence = req.CheckBottlePresence(wineName, year, volume); } // if all verification are ok -> will be true, otherwise, will be false successFormat = successNumber && successYear && successName && successColor && successManu && successStorage; // all checks passed, bottle not present if (successFormat == true && !successPresence) { // different requests, depending on parameters given if (successDescription && successVarietal) { successAdd = Bottles.AddBottleWithDescAndVarietal(wineName, color, number, volume, manufacturer, year, storage, varietal, description); } else if (successVarietal) { successAdd = Bottles.AddBottleWithVarietal(wineName, color, number, volume, manufacturer, year, storage, varietal); } else if (successDescription) { successAdd = Bottles.AddBottleWithDesc(wineName, color, number, volume, manufacturer, year, storage, description); } else { successAdd = Bottles.AddBottle(wineName, color, number, volume, manufacturer, year, storage); } // message box to show, depending on the result when adding the bottle if (successAdd) { int wineID = req.GetIDBottleByName(wineName); successLog = Logs.AddLog("ajoutNouvelle", wineID); MessageBox.Show("L'ajout de la bouteille a été effectué correctement."); } else { MessageBox.Show("Une erreur est survenue lors de l'ajout de la bouteille. Veuillez réessayer."); } } // all checks passed, bottle already present else if (successFormat == true && successPresence) { successAdd = Bottles.UpdateBottle(wineName, number, volume, year); if (successAdd) { int wineID = req.GetIDBottleByName(wineName); successLog = Logs.AddLog("ajoutExistante", wineID); MessageBox.Show("La mise à jour du nombre de bouteilles a été effectuée correctement."); } else { MessageBox.Show("Une erreur est survenue lors de la mise à jour du nombre de bouteilles. Veuillez réessayer."); } } // problems with at least 1 check else { MessageBox.Show("Une des valeurs spécifiées est incorrecte."); txtNumber.Text = ""; txtYear.Text = ""; } LoadData(); }