C Create an Employee class with two fields idNum and hourlyW

C#

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.

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.

Solution

//EmployeeExceptionDemo.cs

using System;

namespace EmployeeExceptionDemo
{
    class Employee
    {
        String idNum;
        double hourlyWage;
        public Employee(String id, double wage)
        {
            idNum=id;
            //thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00
            if(wage<7.50 || wage>50.00)
                throw new ArgumentException();
            hourlyWage=wage;
        }
        
        public void display()
        {
            Console.WriteLine(\"Id number: \"+idNum);
            Console.WriteLine(\"Hourly wage: \"+hourlyWage);
        }
        
    }
    class EmployeeExceptionDemo
    {
        
        public static void Main(string[] args)
        {
            try{
            Console.WriteLine(\"Employee 1:\");
            Employee e1=new Employee(\"12345\",10.75);
            e1.display();
            Console.WriteLine(\"Employee 2:\");
            Employee e2=new Employee(\"12346\",6);
            e2.display();
            Console.WriteLine(\"Employee 3:\");
            Employee e3=new Employee(\"12345\",75.50);
            e3.display();
            }
            catch(ArgumentException e)
            {
                Console.WriteLine(e);
            }
            
            Console.Write(\"Press any key to continue . . . \");
            Console.ReadKey(true);
        }
    }
}

//////////////////////////////////////////////////////////////////////

//EmployeeExceptionDemo2.cs

using System;

namespace EmployeeExceptionDemo
{
    class Employee
    {
        String idNum;
        double hourlyWage;
        public Employee(String id, double wage)
        {
            idNum=id;
            //thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00
            if(wage<7.50 || wage>50.00)
            {
                idNum=\"999\";
                hourlyWage=7.50;
                throw new ArgumentException();
            }
                
            hourlyWage=wage;
        }
        
        public void display()
        {
            Console.WriteLine(\"Id number: \"+idNum);
            Console.WriteLine(\"Hourly wage: \"+hourlyWage);
        }
        
    }
    class EmployeeExceptionDemo2
    {
        
        public static void Main(string[] args)
        {
            
            Employee []employees=new Employee[5];
            for(int i=0;i<5;i++)
            {
                try{
                Console.WriteLine(\"Enter id and hourly wage for Employee # \"+(i+1));
                string id=Console.ReadLine();
                double wage=Convert.ToDouble(Console.ReadLine());
                employees[i]=new Employee(id,wage);
                }
                catch(ArgumentException e)
                {
                Console.WriteLine(e.Message);
                }
            }
            Console.WriteLine(\"Employee Details:\");
            for(int i=0;i<5;i++)
            {
                Console.WriteLine(\"Employee # \"+(i+1));
                employees[i].display();
            }
            
            
            Console.Write(\"Press any key to continue . . . \");
            Console.ReadKey(true);
        }
    }
}

C# Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an Argume
C# Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an Argume
C# Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an Argume

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site