private void btnSave_Click(object sender, EventArgs e) { if (_drink == null) { var mineralWater = new MineralWater() { Bottle = new Bottle() }; if (!SetMineralWaterProperties(mineralWater)) { return; } _modelService.AddDrink(mineralWater); } else { var mineralWater = (MineralWater)_drink; if (!SetMineralWaterProperties(mineralWater)) { return; } } DialogResult = DialogResult.OK; }
private bool SetMineralWaterProperties(MineralWater mineralWater) { mineralWater.Name = tbName.Text; try { mineralWater.Cost = decimal.Parse(tbCost.Text); } catch (FormatException) { MessageBox.Show("Enter the correct price!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } try { mineralWater.SugarContent = float.Parse(tbSugarContent.Text); } catch (FormatException) { MessageBox.Show("Enter the correct sugar content!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } mineralWater.WaterSource = tbWaterSource.Text; try { mineralWater.Mineralization = float.Parse(tbMineralization.Text); } catch (FormatException) { MessageBox.Show("Enter the correct mineralization!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } mineralWater.Bottle = new Bottle(); try { mineralWater.Bottle.Volume = int.Parse(tbVolume.Text); } catch (FormatException) { MessageBox.Show("Enter the correct bottle volume!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } mineralWater.Bottle.Color = tbColor.Text; return(true); }