Reference no: EM132197653
How do you run an array through a file in C#?
THIS IS THE ASSIGNMENT INVOLVING THE QUESTION
Code a C# project (using Visual Studio) to solve the following problem:
A company wants to see a printout of the gross pay for each of its 7 departments. The output should be a list of the seven departments and the total gross pay (the sum of the gross pay for all the employees in that department) for each department. Be sure to validate the input data. If a line of data is invalid print out an error message along with the invalid data, then continue processing with the next record.
The file containing the employee data that will be used for the calculations is structured as follows:
FIELD DESCRIPTION DATA TYPE VALID VALUES
Employee ID Character Non blank
Department Number Numeric 1 - 7
Hourly Salary Numeric at least 10.00
Hours Worked Numeric greater than 0
The fields in each record will be separated by a comma (e.g. EID001, 2, 15.50, 40).
THIS IS THE FAILED CODE I HAVE NOW
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApp12
{
class Program
{
static void Main(string[] args)
{
const int DEPARTMENT = 7;
const int SIZE = 4;
FileStream fstream = new FileStream("Exam3text.txt", FileMode.Open, FileAccess.Read);
StreamReader infile = new StreamReader(fstream);
string inputRecord = "";
int department = 0;
int[] departmentCount = new int[DEPARTMENT];
double hourlySalary = 0.0;
double totalHoursWorked = 0.0;
double hoursWorked = 0.0;
double grossPay = 0.0;
double totalHourlySalary = 0.0;
inputRecord = infile.ReadLine();
string[] employee = new string[SIZE];
double[] totalGrossPay = new double[SIZE];
while (inputRecord != null)
{
employee = inputRecord.Split(',');
if (int.TryParse(employee[1], out department) && department >= 0 && department <= DEPARTMENT)
{
departmentCount[department - 1] = departmentCount[department - 1] + 1;
}
else
Console.WriteLine(inputRecord);
if (double.TryParse(employee[2], out totalHourlySalary) && hourlySalary >= 10.0)
{
totalHourlySalary = totalHourlySalary + hourlySalary;
}
if (double.TryParse(employee[3], out totalHoursWorked) && hoursWorked > 0)
{
totalHoursWorked = totalHoursWorked * hoursWorked;
//Console.WriteLine(totalHoursWorked);
}
if (department >= 1 && department <= SIZE)
{
totalGrossPay[department - 1] = totalGrossPay[department - 1] + (totalHoursWorked * totalHourlySalary);
totalGrossPay[department - 1] = grossPay;
//Console.WriteLine(grossPay);
}
inputRecord = infile.ReadLine();
grossPay = totalHourlySalary * totalHoursWorked;
}//end while
showOutput(departmentCount, totalHourlySalary, totalHoursWorked, totalGrossPay);
}
static void showOutput(int[] departmentCount, double totalHourlySalary, double totalHoursWorked, double[] totalGrossPay)
{
Console.WriteLine("Grosspay for each department");
Console.WriteLine("Departmentt Employeest Hourly Salaryt Hours Workedt Gross Payt ");
for (int i = 0; i < departmentCount.Length; i++)
{
if (departmentCount[i]>= 0)
{
Console.WriteLine("{0}tt {1}tt {2}tt {3}tt {4}tt", i + 1, departmentCount[i], totalHourlySalary, totalHoursWorked, totalGrossPay[]);
}//end if
}//end for
}//end showOutput
}
}