In: Computer Science
C# Programming
1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00. Write a program that establishes, one at a time, at least three Employees with hourlyWages that are above, below, and within the allowed range. Immediately after each instantiation attempt, handle any thrown Exceptions by displaying an error message. Save the file as EmployeeExceptionDemo.cs.
2. Write an application that creates an array of five Employees. Prompt the user for values for each field for each Employee. If the user enters improper or invalid data, handle any exceptions that are thrown by setting the Employee’s ID number to 999 and the Employee’s pay rate to the $7.50. At the end of the program, display all the entered, and possible corrected, records. Save the file as EmployeeExceptionDemo2.cs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Question
{
class Program
{
static void Main(string[] args) {
int id = 0;
double hw = 0;
Employee[] emp = new Employee[5];
for (int x = 0; x <= 4; x++) {
try {
Console.Write("Enter ID: ");
id = int.Parse(Console.ReadLine()); }
catch (FormatException fe) { /*This WriteLine will only execute if a non-integer value is entered and the FormatException is thrown by the error handler. */
Console.WriteLine(fe.Message + "Input string was not in a correct format. \n");
id = 999;
hw = 7.50; }
try {
Console.WriteLine("Enter salary:");
hw = Convert.ToDouble(Console.ReadLine());
}
catch (Exception e)
{ id = 999;
hw = 7.5;
Console.WriteLine(e.ToString()); }
emp[x] = new Employee(id, hw); }
for (int x = 0; x <= 4; x++) {
Console.WriteLine("Employee#" + emp[x].IDNum + "Payrate$ " + emp[x].hourlyWage); }
Console.ReadLine(); }
}
public class Employee {
public int IDNum;
public double hourlyWage;
public Employee() { }
public Employee(int id, double hw)
{ hourlyWage = hw;
IDNum = id;
if (hw < 7.5 || hw > 50)
Console.WriteLine("Hourly wages should be greater than 7.5 or less than 50");
}
} }