Implement the following requirements: Create an Employee class with two fields:
ID: 638463 • Letter: I
Question
Implement the following requirements:
Create an Employee class with two fields: IDNum and hourlyWage.
The constructor for Employee will accept two arguments for these two fields.
When the Employee class is instantiated you will throw an ArgumentException if the hourlyWage is less than 6.00 or more than 50.00.
Handle any thrown Exceptions in the Employee class by displaying an error message.
In Main instantiate an array of five (5) Employee objects.
Prompt the user for the values of the two fields in each Employee object.
Handle any exceptions(try-catch block) that are thrown by setting the Employment
Explanation / Answer
Program :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Divya
{
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. ");
id = 999;
hw = 6.00;
}
try
{
Console.WriteLine("Enter salary:");
hw =Convert.ToDouble(Console.ReadLine());
// Salary = Convert.ToDouble(Salary);
}
catch (Exception e)
{
id = 999;
hw = 6.0;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 == 50)
throw (new Exception());
// label3.Text = "Employee Object Created Successfully";
}
}
}