简体   繁体   中英

How do I send only specified members of an array to a ToString override?

My current C# homework assignment requires using a predefined array of 7 employee records, each consisting of a set of attributes. These records must use a foreach loop to pull all the employee records from the array and then use a public override of ToString to display the records with specific columnar formatting. I've been able to implement this phase without a problem.

Then the next set of requirements wants us to display only the employee records that have less than one year of service, or between 1 and 5 years, and that's where I get stuck.

We are required to use this class to represent the current date (copied and pasted from the assignment) :

public static class GlobalVars
{
    public static DateTime reportDate = DateTime.Parse("02/28/2017");
}

We are also required to create an original method ourselves called YearsOfService to calculate how many years of service an employee has, which compares the Year of the employee hireDate to the Year of the current date in the GlobalVars class, which I've done here:

public int YearsOfService()
{
    DateTime current = GlobalVars.reportDate;
    int yearsOfService = current.Year - hireDate.Year;
    return yearsOfService;
}

And we're required to use this syntax to print the records onscreen (copied and pasted from the assignment):

Employee emp = new Employee();
Employee[] employees = Employee.GetEmployeeData();

Console.WriteLine(Employee.EmployeeReportTitle("Employee Report Data Dump"));
Console.WriteLine(Employee.ColumnHeader());

foreach (Employee employee in employees)
{
    Console.WriteLine(employee.ToString());
}

And here's my formatted override string output for the column formatting:

public override string ToString()
{
    return String.Format("{0,-10:D5}   {1, -20}{2, -16}${3, -11}{4:MM/dd/yyyy} {5, 16:D2}",
    empNumber, empName, empStatus, hourlyWage, hireDate, YearsOfService());
}

This works fine to print every record in the array, but how can I insert requirements at the foreach level? I need to implement a filter so the only records that are printed via the ToString are ones where yearsOfService is higher than a specific value, or between two values.

I've tried if/else loops and switch cases, but I can't get the syntax structured in a way that the correct records are excluded/included.

You can do it in one of these two ways...

var minYoS = 3;
var maxYoS = 5;

foreach (Employee employee in employees.Where(x => x.YearsOfService() >= minYoS && x.YearsOfService() <= maxYoS))
{
    Console.WriteLine(employee.ToString());
}

or

var minYoS = 3;
var maxYoS = 5;

foreach (Employee employee in employees)
{
    if(minYoS <= employee.YearsOfService() && maxYoS >= employee.YearsOfService()){
        Console.WriteLine(employee.ToString());
    }
}

The first examples uses a LINQ projection to iterate only through the employees that meet the specified requirement. The second one iterates through all employees and then inside the foreach loop the code evaluates whether the employee record should be printed to the console or not

Your filter could be the Where method, one the most useful methods of the great family of LINQ.

var employeesFilter = employees.Where(employee => employee.YearsOfService > 5);
foreach (Employee employee in employeesFilter)
{
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM