Skip to content

Commit

Permalink
R: Extract new Employee class
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgu committed Mar 19, 2016
1 parent d34d7ba commit da3abd3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion EmployeeCollection.cs
Expand Up @@ -18,7 +18,7 @@ public List<Employee> Items

public void AddEmployeeIfMatch(EmployeeFilter employeeFilter, string name, int age, bool isSalaried, int id, Employee employee)
{
if (employeeFilter.Matches(name, age, isSalaried))
if (employeeFilter.Matches(new EmployeeFilter.Employee(name, age, isSalaried)))
{
Items.Add(employee);
}
Expand Down
35 changes: 32 additions & 3 deletions EmployeeFilter.cs
Expand Up @@ -17,15 +17,44 @@ public EmployeeFilter(EmployeeFilterType employeeFilterType, string filter)
}
}

public bool Matches(string name, int age, bool isSalaried)
public class Employee
{
private string _name;
private int _age;
private bool _isSalaried;

public Employee(string name, int age, bool isSalaried)
{
_name = name;
_age = age;
_isSalaried = isSalaried;
}

public string Name
{
get { return _name; }
}

public int Age
{
get { return _age; }
}

public bool IsSalaried
{
get { return _isSalaried; }
}
}

public bool Matches(Employee employee)
{
switch (_employeeFilterType)
{
case EmployeeFilterType.ByName:
if (!name.StartsWith(_filter)) return false;
if (!employee.Name.StartsWith(_filter)) return false;
break;
case EmployeeFilterType.ExemptOnly:
if (age < 40 || !isSalaried) return false;
if (employee.Age < 40 || !employee.IsSalaried) return false;
break;
}
return true;
Expand Down
10 changes: 5 additions & 5 deletions designIssueExampleTests/EmployeeFilter_Tests.cs
Expand Up @@ -25,38 +25,38 @@ public void When_I_create_a_named_filter_and_pass_values_that_dont_match__Matche
{
EmployeeFilter employeeFilter = new EmployeeFilter(EmployeeFilterType.ByName, "A");

Assert.IsFalse(employeeFilter.Matches("Fred", 42, false));
Assert.IsFalse(employeeFilter.Matches(new EmployeeFilter.Employee("Fred", 42, false)));
}

[TestMethod]
public void When_I_create_a_named_filter_and_pass_values_that_do_match__Matches_returns_true()
{
EmployeeFilter employeeFilter = new EmployeeFilter(EmployeeFilterType.ByName, "A");

Assert.IsTrue(employeeFilter.Matches("Alan", 42, false));
Assert.IsTrue(employeeFilter.Matches(new EmployeeFilter.Employee("Alan", 42, false)));
}

[TestMethod]
public void When_I_create_a_exempt_filter_with_an_age_that_is_too_young_and_salaried__Matches_returns_false()
{
EmployeeFilter employeeFilter = new EmployeeFilter(EmployeeFilterType.ExemptOnly, null);

Assert.IsFalse(employeeFilter.Matches("Alan", 39, true));
Assert.IsFalse(employeeFilter.Matches(new EmployeeFilter.Employee("Alan", 39, true)));
}

[TestMethod]
public void When_I_create_a_exempt_filter_with_an_age_that_is_old_enough_and_not_salaried__Matches_returns_false()
{
EmployeeFilter employeeFilter = new EmployeeFilter(EmployeeFilterType.ExemptOnly, null);

Assert.IsFalse(employeeFilter.Matches("Alan", 40, false));
Assert.IsFalse(employeeFilter.Matches(new EmployeeFilter.Employee("Alan", 40, false)));
}
[TestMethod]
public void When_I_create_a_exempt_filter_with_an_age_that_is_old_enough_and_is_salaried__Matches_returns_true()
{
EmployeeFilter employeeFilter = new EmployeeFilter(EmployeeFilterType.ExemptOnly, null);

Assert.IsTrue(employeeFilter.Matches("Alan", 40, true));
Assert.IsTrue(employeeFilter.Matches(new EmployeeFilter.Employee("Alan", 40, true)));
}
}
}

0 comments on commit da3abd3

Please sign in to comment.