Good day, I have been given an assignment that gets data off a CSV file. The CSV file contains the following entries:
LName,FName,Dept,Grade,Gross Pay, Tax Paid, Net Pay.
I am able to retrieve the data and display it in the console window which is shown in my code below. However, I need help with displaying the results according to dept and getting the total net pay in each dept. There are three depts M, R, And S.
class DisplayEmpData
{
public void DisplayEmp()
{
List<string> columns;
List<Dictionary<string, string>> myData = GetData(out columns);
foreach (string column in columns)
{
Console.Write("{0,-9}", column);
}
Console.WriteLine();
foreach (Dictionary<string, string> row in myData)
{
foreach (string column in columns)
{
Console.Write("{0,-9}", row[column]);
}
Console.WriteLine();
}
Console.ReadKey();
}
private static List<Dictionary<string, string>> GetData(out List<string> columns)
{
string line;
string[] stringArray;
char[] charArray = new char[] { ',' };
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
columns = new List<string>();
try
{
FileStream aFile = new FileStream(@"..\..\EmployeeDetails.txt", FileMode.Open);
StreamReader sr = new StreamReader(aFile);
line = sr.ReadLine();
stringArray = line.Split(charArray);
for (int x = 0; x <= stringArray.GetUpperBound(0); x++)
{
columns.Add(stringArray[x]);
}
line = sr.ReadLine();
while (line != null)
{
stringArray = line.Split(charArray);
Dictionary<string, string> dataRow = new Dictionary<string, string>();
for (int x = 0; x <= stringArray.GetUpperBound(0); x++)
{
dataRow.Add(columns[x], stringArray[x]);
}
data.Add(dataRow);
line = sr.ReadLine();
}
sr.Close();
return data;
}
catch (IOException ex)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return data;
}
}
}
this is the data found in the CSV file.
Sample data:
LName,FName,Dept,Grade,HrsWorked,Gross,Tax,Net
kakvi,sakib,M,b,50,179.38,19.91,159.47
Ahmed,Shaan,R,A,50,140.50,14.08,126.43
Thomas,John,S,B,34,109.38,9.41,99.97
Link,Frank,M,E,24,78.75,5.88,72.88
Jones,Jack,R,D,34,124.00,11.60,112.40
Rich,Dan,M,C,37,145.00,14.75,130.25
However this is what I want.
Desired Output
ID Surname Forename Grade Hrs GrossPay Tax Net Pay
0001 Kakvi Sakib B 50 179.38 19.91 159.47
xxxx xxxx xxxxxxx x xxx xxx.xx xxx.xx xxx.xx
xxxx xxxx xxxxxxx x xxx xxx.xx xxx.xx xxx.xx
xxxx xxxx xxxxxxx x xxx xxx.xx xxx.xx xxx.xx
--------
Total for: Marketing xxxxx.xx
Department: Sales
ID Surname Forename Grade Hrs Gross Pay Tax Net Pay
xxxx xxxx xxxxxxx x xxx xxx.xx xxx.xx xxx.xx
xxxx xxxx xxxxxxx x xxx xxx.xx xxx.xx xxx.xx
--------
Total for: Sales xxxxx.xx
Please help me. Thank You.