Question: This is C# 1. Add a try catch statement to the btnCalculate_click method that catches and handles any formatException or OverflowException that might occur. These
This is C#
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 teh 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 should specificy 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 thats 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.
The 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) { 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(); }
private decimal CalculateFutureValue(decimal monthlyInvestment, decimal monthlyInterestRate, int months) { decimal futureValue = 0m; for (int i = 0; i < months; i++) { futureValue = (futureValue + monthlyInvestment) * (1 + monthlyInterestRate); } return futureValue; }
private void btnExit_Click(object sender, EventArgs e) { this.Close(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
