implementation for visitor pattern blog post

  • Download Gist
visitor_pattern_implementation_20081130_blogpost.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
using System;
using System.Text;
namespace visitor.pattern
{
public abstract class Employee
{
protected int totalSickDaysUsed;
protected int totalVacationDaysUsed;
public abstract void accept(EmployeePaycheckVisitor visitor);
public void addSickDaysUsed(int numberOfDays)
{
totalSickDaysUsed += numberOfDays;
}
public void addVacationDaysUsed(int numberOfDays)
{
totalVacationDaysUsed += numberOfDays;
}
public int SickDaysUsed
{
get { return totalSickDaysUsed; }
}
public int VacationDaysUsed
{
get { return totalVacationDaysUsed; }
}
}
public class HourlyEmployee : Employee
{
private int totalTimeWorked;
public HourlyEmployee(string name, decimal hourlyRate)
{
totalTimeWorked = totalSickDaysUsed = totalVacationDaysUsed = 0;
Name = name;
HourlyRate = hourlyRate;
}
public override void accept(EmployeePaycheckVisitor visitor)
{
visitor.visit(this);
}
public string Name { get; set; }
public decimal HourlyRate { get; private set; }
public void addTimeWorked(int hoursWorked)
{
totalTimeWorked += hoursWorked;
}
public int TimeWorked
{
get { return totalTimeWorked; }
}
}
public class FulltimeEmployee : Employee
{
private int totalBusinessDaysWorked;
public FulltimeEmployee(string name, decimal annualSalary)
{
Name = name;
AnnualSalary = annualSalary;
totalBusinessDaysWorked = 0;
}
public string Name { get; private set; }
public decimal AnnualSalary { get; private set; }
public override void accept(EmployeePaycheckVisitor visitor)
{
visitor.visit(this);
}
public void addBusinessDaysWorked(int businessDaysWorked)
{
totalBusinessDaysWorked += businessDaysWorked;
}
public int BusinessDaysWorked
{
get { return totalBusinessDaysWorked; }
}
}
public class EmployeePaycheckVisitor
{
private const int FULL_WORK_DAY_HOURS_COUNT = 8;
private const int NUMBER_OF_BUSINESS_DAYS_IN_A_YEAR = 52*5;
public string PaycheckSummaryLine{get; private set;}
public decimal EarnedWages{ get; private set; }
public decimal SickDayDeductions { get; private set; }
public void visit(HourlyEmployee employee)
{
SickDayDeductions = (employee.SickDaysUsed * FULL_WORK_DAY_HOURS_COUNT) * employee.HourlyRate;
EarnedWages = (employee.TimeWorked * employee.HourlyRate) (SickDayDeductions);
StringBuilder paycheckSummaryLine = new StringBuilder();
paycheckSummaryLine.Append(string.Format({0} has worked {1} hours as an hourly employee, employee.Name, employee.TimeWorked));
paycheckSummaryLine.Append(string.Format( and has earned {0:C}, EarnedWages));
paycheckSummaryLine.Append(string.Format( (Sick Days: {0}/{1:C}, employee.SickDaysUsed, SickDayDeductions));
paycheckSummaryLine.Append(string.Format( Vacation Days: {0}, employee.VacationDaysUsed));
PaycheckSummaryLine = paycheckSummaryLine.ToString();
Console.WriteLine(PaycheckSummaryLine);
}
public void visit(FulltimeEmployee employee)
{
decimal dailyWages = employee.AnnualSalary/NUMBER_OF_BUSINESS_DAYS_IN_A_YEAR;
SickDayDeductions = Decimal.Round(employee.SickDaysUsed * dailyWages);
EarnedWages = Decimal.Round((employee.BusinessDaysWorked * dailyWages) (SickDayDeductions), 2);
StringBuilder paycheckSummaryLine = new StringBuilder();
paycheckSummaryLine.Append(string.Format({0} has worked {1} business days as a fulltime employee, employee.Name, employee.BusinessDaysWorked));
paycheckSummaryLine.Append(string.Format( and has earned {0:C}, EarnedWages));
paycheckSummaryLine.Append(string.Format( (Sick Days: {0}/{1:C}, employee.SickDaysUsed, SickDayDeductions));
paycheckSummaryLine.Append(string.Format( Vacation Days: {0}, employee.VacationDaysUsed));
PaycheckSummaryLine = paycheckSummaryLine.ToString();
Console.WriteLine(PaycheckSummaryLine);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.