Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Report problem (http://www.go4expert.com/forums/report-problem-t20712/)

Ghadoor 20Jan2010 21:32

Report problem
 
I have a problem, and I really need your help!
I have this project and I'm done with it .. But I have a logical error .. When it comes to the print part it prints only the last name and the last calculated average .. I couldn't figure out the error

The program is created to enter employee name and their worked hours during the year and then calculating their averages ,. And at the end it should print a report with the name I entered and the averages ..

The program goes this way:
Pleas enter employee name: hfgjd
Please enter the worked hours: 2
Please enter worked hours:2
please enter worked hours: 0
The average is:2
Would you like to continue? (Y/N) .. N
"The report part"

The class:
Code:

classEmployee
{
privatestring employeeName;
privatedouble[] workHours = newdouble[12];
privatebool validIndex = true;
privatedouble avgWorkedHours;
 
 
publicstring getEmployeeName()
{
return employeeName;
}
publicvoid setEmployeeName(string name)
{
employeeName = name;
}
publicdouble getWorkHours(int index)
{
return workHours[index];
}
publicvoid setWorkHours(double hours, int index)
{
if (index < 0 || index > 12)
{
Console.WriteLine("Wrong Entry!");
validIndex = false;
}
else
{
workHours[index] = hours;
}
}
publicbool getValidIndex()
{
 
return validIndex;
}
publicdouble getAvgWorkedHours()
{
return avgWorkedHours;
}
publicvoid setAvgWorkedHours(double average)
{
 
int max = 0;
double sum = 0;
for (int i = 0; i < 12; i++)
{
 
if (workHours[i] == 0)
{
max = i;
break;
}
else
{
sum += workHours[i];
max = i;
}
}
if (max > 0)
average = sum / max;
avgWorkedHours = average;
 
}
}


Ghadoor 20Jan2010 21:33

Re: Report problem
 
The program:

Code:

classProgram

{

staticvoid Main(string[] args)

{

 

Employee[] employeeInfo = newEmployee[25];

for (int i = 0; i < employeeInfo.Length; i++)

employeeInfo[i] = newEmployee();

 

int employeeCount = 0;

// Puting an Array into a Class:

try

{

 

Console.WriteLine("Welcome To The Program!");

Console.WriteLine("Please inform, that you have to enter a 0, after entering the whole worked hours ..!");

string input = "";

do

{

int counter = 0;

int hours = 0;

// To enter the employee information:


Console.Write("Please Enter The Employee Name: ");

 

employeeInfo[counter].setEmployeeName(Console.ReadLine());

int hourCounter = 0;

do

{

Console.Write("Please Enter The Employee Worked Hours For The Month:");

hours = (Int32.Parse(Console.ReadLine()));

if (hours == 0)

break;

employeeInfo[counter].setWorkHours(hours, hourCounter);

hourCounter++;

} while (hourCounter < 12);

double avg = 0;

employeeInfo[counter].setAvgWorkedHours(avg);

Console.WriteLine("The Average For The Employee Worked Hours is : " + employeeInfo[counter].getAvgWorkedHours());

Console.Write("Would you like to continue ? (Y/N) .. ");

input = Console.ReadLine();

Console.WriteLine("_______________________________________");

employeeCount++;

} while (employeeCount < 25 && input == "Y" || input == "y");

Console.Clear();

 

// To print the report:

for (int n = 0; n < employeeInfo.Length; n++)

{

Console.WriteLine("The Employee Name is: {0} and His Average for the Worked Hours is: {1} ", employeeInfo[n].getEmployeeName(), employeeInfo[n].getAvgWorkedHours());

}

 

}

 

// To catch the errors while running the program:

catch (DivideByZeroException expOne)

{

Console.WriteLine("An Error Occurred " + expOne.Message);

}

catch (FormatException expTwo)

{

Console.WriteLine("An Error Occurred " + expTwo.Message);

}

finally

{

Console.WriteLine("Thank You!");

}

 

 

Console.ReadLine();



All times are GMT +5.5. The time now is 04:43.