//Calculate button event private void CalculateButton_Click(object sender, EventArgs e) { try { // Weight in kg double weight = GetWeight(); // Height in cm double height = GetHeight(); double bmi = BMIUtility.calculateBmi(weight, height); bmiTextBox.Text = Convert.ToString(Math.Round(bmi, 2)); Gender gender = GetGender(); double idealWeight = BMIUtility.calculateIdealBodyWeight(gender, height); recommendationTextBox.Text = $"Your ideal weight is {idealWeight}"; List <BMIData> bmiDataList = BMIData.getBMIDataList(); BMIData resultBmiData = GetBmiData(bmi, bmiDataList); resultsTextBox.Text = resultBmiData.Category; unfocusResultInDataGrid(); focusResultInDataGrid(bmiDataList, resultBmiData); } catch (Exception ex) { MessageBox.Show("Exception occurred: " + ex.Message); } }
//this method would fetch and validate the height value entered in form private double GetHeight() { double height; try { height = Convert.ToDouble(heightTextBox.Text); } catch (Exception ex) { throw new Exception("Invalid value for height"); } if (height <= 0) { throw new Exception("Height cannot be zero or less than zero"); } if (heightComboBox.Text.Equals("inch")) { height = BMIUtility.convertInchToCentimeter(height); } return(height); }
//this method would fetch and validate the weight value entered in form private double GetWeight() { double weight; try { weight = Convert.ToDouble(weightTextBox.Text); } catch (Exception) { throw new Exception("Invalid value for weight"); } if (weight <= 0) { throw new Exception("Weight cannot be zero or less than zero"); } if (weightComboBox.Text.Equals("lb")) { weight = BMIUtility.convertPoundToKilogram(weight); } return(weight); }