Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this C# program I have most of it done I just need to know how

ID: 3836554 • Letter: I

Question

I need help with this C# program

I have most of it done I just need to know how to Validate this simple program.

It must be done in C# and must be a form. Also I am a beginner so please make it super easy to understand. I would also love if you could explain what you did as you do this.

Directions below

--------------------------------------------------------------------------------------------------

In this exercise, you’ll add data validation to the Simple Calculator form of extra exercise A5-E1.

1.Open the SimpleCalculator project in the Assignment5SimpleCalculatorValidation directory.

2.     Code methods named

IsPresent,

IsDecimal, and

IsWithinRange

that work like the methods described in Murach’s chapter 7 of the book.

3.     Code a method named IsOperator that checks that the text box that’s passed to it contains a value of +, -, *, or /.

4.     Code a method named IsValidData that checks that the Operand 1 and Operand 2 text boxes contain a decimal value between 0 and 1,000,000 (non-inclusive) and that the Operator text box contains a valid operator.

5.     Delete all of the catch blocks from the try-catch statement in the btnCalculate_Click event handler except for the one that catches any exception. Then, add code to this event handler that performs the calculation and displays the result only if the values of the text boxes are valid.

6.     Test the application to be sure that all the data is validated properly.

------------------------------------------------------------------------------------------------------------------------

The code that needs to be modified below

private void btnCalculate_Click(object sender, EventArgs e)
{
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
string operator1 = txtOperator.Text;
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
decimal result = Calculate(operand1, operator1, operand2);

result = Math.Round(result, 4);
this.txtResult.Text = result.ToString();
txtOperand1.Focus();
}

private decimal Calculate(decimal operand1, string operator1,
decimal operand2)
{
decimal result = 0;
if (operator1 == "+")
result = operand1 + operand2;
else if (operator1 == "-")
result = operand1 - operand2;
else if (operator1 == "*")
result = operand1 * operand2;
else if (operator1 == "/")
result = operand1 / operand2;
return result;
}

private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

-------------------------------------------------------------------------------------------------------

THANKS!

Entry Error Operator is not valid. OK

Explanation / Answer

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace Program

{

    public partial class Calc_Simple : Form

    {

        public Calc_Simple()

        {

            InitializeComponent();

        }

        private void btnCalculate_Click(object sender, EventArgs e)

        {

            try

            {

                if (IsValidData())

                {

                    decimal operand1 = Convert.ToDecimal(txtOperand1.Text);

                    string operator1 = Convert.ToString(txtOperator1.Text);

                    decimal operand2 = Convert.ToDecimal(txtOperand2.Text);

                    decimal result = Calculate(operand1, operator1, operand2);

                   txtResult.Text = result.ToString("f4");

                    txtOperand1.Focus();

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message + " " + ex.GetType().ToString() + " " + ex.StackTrace, "Exception");

            }

        }

        public bool IsValidData()

        {

            return

                IsPresent(txtOperand1, "Operand 1") &&

                IsDecimal(txtOperand1, "Operand 1") &&

                IsWithinRange(txtOperand1, "Operand 1", 0, 1000000) &&

                IsPresent(txtOperator1, "Operator 1") &&

                IsOperator(txtOperator1, "Operator 1", "+", "-", "*", "/") &&

                IsPresent(txtOperand2, "Operand 2") &&

                IsDecimal(txtOperand2, "Operand 2") &&

                IsWithinRange(txtOperand2, "Operand 2", 0, 1000000);

        }

        public bool IsPresent(TextBox textBox, string name)

        {

            if (txtOperand1.Text == "" && txtOperand2.Text == "")

            {

                MessageBox.Show(name + " is a required field.", "Entry Error");

                txtOperand1.Focus();

                txtOperand2.Focus();

                return false;

            }

            return true;

        }

        public bool IsDecimal(TextBox textBox, string name)

        {

            try

            {

                Convert.ToDecimal(textBox.Text);

                return true;

            }

            catch (FormatException)

            {

                MessageBox.Show(name + " must be a decimal value.", "Entry Error");

                textBox.Focus();

                return false;

            }

        }

        public bool IsOperator(TextBox textBox, string name)

        {

            try

            {

                Convert.ToString(textBox.Text);

                return true;

            }

            catch (FormatException)

            {

                MessageBox.Show(name + " must be an operator.", "Entry Error");

                txtOperator1.Focus();

                return false;

            }

        }

        public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max)

        {

            decimal number = Convert.ToDecimal(textBox.Text);

            if (number < min || number > max)

            {

                MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error");

                textBox.Focus();

                return false;

            }

            return true;

        }

        private decimal Calculate(decimal operand1, string operator1, decimal operand2)

        {

            if (operator1 == "+")

            {

                decimal result = operand1 + operand2;

            }

            else if (operator1 == "-")

            {

                decimal result = operand1 - operand2;

            }

            else if (operator1 == "*")

            {

                decimal result = operand1 * operand2;

            }

            else

            {

                decimal result = operand1 / operand2;

            }

            return result;

        }

        private void btnExit_Click(object sender, EventArgs e)

        {

            this.Close();

        }

    }

}