public class RecordChecker { public static Collection<int> FindEmployees(EmployeeData dataSource, int department) { Collection<int> storage = new Collection<int>(); Console.WriteLine("Record checker: beginning search.");
for (int i = 0; i < dataSource.Employees.Length; i++) { if (dataSource.Employees[i].Department == department) { Console.WriteLine("Record checker: found match at {0}.", i); storage.Add(dataSource.Employees[i].EmployeeId); Console.WriteLine("Record checker: stored match at {0}.", i); } else { Console.WriteLine("Record checker: no match at {0}.", i); } } return storage; } }
using System; using System.Collections.ObjectModel; namespace Examples.DesignGuidelines.Properties { public class EmployeeRecord { private int employeeId; private int department;
public EmployeeRecord() { }
public EmployeeRecord (int id, int departmentId) { EmployeeId = id; Department = departmentId; }
public int Department { get {return department;} set {department = value;} }
public int EmployeeId { get {return employeeId;} set {employeeId = value;} }
public EmployeeRecord Clone() { return new EmployeeRecord(employeeId, department); } }
public class EmployeeData { EmployeeRecord[] data;
public EmployeeData(EmployeeRecord[] data) { this.data = data; }
public EmployeeRecord[] Employees { get { EmployeeRecord[] newData = CopyEmployeeRecords(); return newData; } }
public class RecordChecker { public static Collection<int> FindEmployees(EmployeeData dataSource, int department) { Collection<int> storage = new Collection<int>(); Console.WriteLine("Record checker: beginning search.");
for (int i = 0; i < dataSource.Employees.Length; i++) { if (dataSource.Employees[i].Department == department) { Console.WriteLine("Record checker: found match at {0}.", i); storage.Add(dataSource.Employees[i].EmployeeId); Console.WriteLine("Record checker: stored match at {0}.", i); }
else { Console.WriteLine("Record checker: no match at {0}.", i); } } return storage; } }
public class Tester { public static void Main() { EmployeeRecord[] records = new EmployeeRecord[3]; EmployeeRecord r0 = new EmployeeRecord(); r0.EmployeeId = 1; r0.Department = 100; records[0] = r0; EmployeeRecord r1 = new EmployeeRecord(); r1.EmployeeId = 2; r1.Department = 100; records[1] = r1; EmployeeRecord r2 = new EmployeeRecord(); r2.EmployeeId = 3; r2.Department = 101; records[2] = r2; EmployeeData empData = new EmployeeData(records); Collection<int> hits = RecordChecker.FindEmployees(empData, 100); foreach (int i in hits) { Console.WriteLine("found employee {0}", i); } } } }