-Using the Employee class created in Exercise 5a, write an application that creates an array of fi ve Employees. Prompt the user for values for each fi eld 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 $6.00 minimum. At the end of the program, display all the entered, and possibly corrected, records.-
This is my task and it has been kicking my butt all week. My code so far, and it seems to me it should work. Not sure what I am doing wrong.
namespace EmployeeException
{
class EmployeeExceptionDemo
{
static void Main(string[] args)
{
Employee emp1 = CreateEmployee();
Employee emp2 = CreateEmployee();
Employee emp3 = CreateEmployee();
Employee emp4 = CreateEmployee();
Employee emp5 = CreateEmployee();
Console.ReadKey();
}
static Employee CreateEmployee()
{
Employee emp = null;
try
{
emp = new Employee();
}
catch (EmployeeException e)
{
Console.WriteLine(e.Message);
}
return emp;
}
}
class EmployeeException : Exception
{
public EmployeeException(string idAndPayRate)
: base(idAndPayRate + " is invalid")
{
}
}
class EmployeeIDException : Exception
{
public EmployeeIDException(string employeeIDAndpayRate) : base(employeeIDAndpayRate + " is invalid")
{
}
}
class Employee
{
string employeeIDNum;
int payRate;
public Employee()
{
this.employeeIDNum = GetID();
this.payRate = GetRate();
}
public int GetID()
{
Console.Write("Enter ID number for Employee : ");
int idNumber;
int.TryParse(Console.ReadLine(), out idNumber);
if (idNumber > 999)
throw new EmployeeException(String.Format("Employee ID number {0}", idNumber));
return idNumber;
}
public int GetRate()
{
Console.Write("Enter Employee's hourly rate : ");
int wage;
int.TryParse(Console.ReadLine(), out wage);
if (wage < 6.00)
throw new EmployeeException(String.Format("Employee's hourly wage {0}", wage));
return wage;
}
}
}
Loading
Paul EPosted Sep 20, 2013, 9:11 PM
VulpesPosted Sep 20, 2013, 2:25 PM
Paul EPosted Sep 20, 2013, 2:13 PM
Hemant SrivastavaPosted Sep 20, 2013, 2:12 PM
this.employeeIDNum = GetID(); by this.employeeIDNum = GetID().ToString();
As far as problem's expectation, you not not doing following things:
(1) Not creating any array of objects (Currently you are just declaring five different objects)
(2) Not displaying any output
(3) Your setting exception condition is somewhat not correct. I think, you need to set ID= 999 whenever user enter any wrong ID (say -8 or xyq or 00 or $%^ etc)
and similerly his wages to $6 whenever user enters any wrong amount (say -18 or xyq or 00 or $%^ or 000000. etc)
Hope it could simplyfy you.
Hemant SrivastavaPosted Sep 20, 2013, 1:55 PM