Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey, I've completed some, but am struggling with coding validation methods. This is the Microsoft visual studio (C#) windows forms. I've posted what I have

Hey, I've completed some, but am struggling with coding validation methods. This is the Microsoft visual studio (C#) windows forms. I've posted what I have left to do and my code below. I've added the problem if it helps. I've finished up steps 1-3. I need help with 4, 5, and 6. Any help appreciated. Thanks.

1. Add a try-catch statement to the btnCalculate_click method that catches and handles any formatException or OverflowException that might occur. These catch blocks should display dialog boxes with appropriate messages.

2. Ann another catch block to the try-catch statement that will catch any other exception that might occur. This catch block should display a dialog box that displays the message contained in the exception object along with the exception type and the stack trace.

3. Add a throw statement to the calculateFutureValue method that throws a new exception of the Exception class regardless of the result of the calculation. This statement will be used to test the enhancements of step 4, and it shoulspecifycy a generic message that indicates an unknown error. Then, test the application by entering valid values in the three text boxes and clicking the calculate button. If the exception is thrown and the last catch block works correctly, end the application and comment out the throw statement.

4. Code four generic validation methods named IsPresent, IsDecimal, IsInt32, and IsWithinRange that test whether a text box contains an entry, a valid decimal value, a valid int value, and a value within a given range. If the validation is unsuccessful, each method should display a dialog box with a message that includes the name of the textbox that's being validated.

5. Code an IsValidData method that calls the four generic methods that were created to validate the data the user enters into the three text boxes. Each text box should be tested for three types of invalid data: nothing entered, invalid format, invalid range.

6. Modify the code in the event handler for the Calculate button so it uses the IsValidData method to validate the data before it processes it. Then, test the application to be sure it works correctly. If it does, end the application and comment out the FormatException and OverflowException catch blocks.

Design:

image text in transcribed

My Code:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace FutureValue { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void btnCalculate_Click(object sender, EventArgs e) { try { decimal monthlyInvestment = Convert.ToDecimal(txtMonthlyInvestment.Text); decimal yearlyInterestRate = Convert.ToDecimal(txtInterestRate.Text); int years = Convert.ToInt32(txtYears.Text);

int months = years * 12; decimal monthlyInterestRate = yearlyInterestRate / 12 / 100;

decimal futureValue = this.CalculateFutureValue( monthlyInvestment, monthlyInterestRate, months); txtFutureValue.Text = futureValue.ToString("c"); txtMonthlyInvestment.Focus(); } catch (FormatException ex) { //statements to be executed when invalid operands are entered MessageBox.Show("Operands must be numeric." + " " + ex.StackTrace, "Entry Error"); //MessageBox.Show("Operands must be numeric.", "Entry Error"); } catch (OverflowException ex) { //statements to be executed when invalid operator is entered MessageBox.Show("Operator is not valid." + " " + ex.StackTrace, "Entry Error"); // MessageBox.Show("Operator is not valid.", "Entry Error"); } catch (DivideByZeroException ex) { //statements to be executed when invalid 0 is entered for operand2 MessageBox.Show("Division with zero is not possible." + " " + ex.StackTrace, "Entry Error"); // MessageBox.Show("Division with zero is not possible.", "Entry Error"); } //All exceptions catch (Exception ex) { //Error Message MessageBox.Show(ex.ToString(), "Error Occured"); } }

private decimal CalculateFutureValue(decimal monthlyInvestment, decimal monthlyInterestRate, int months) { decimal futureValue = 0m; for (int i = 0; i

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

Future Value Monthly Investment: Number of Years Future Value Calculate Exit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions