Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmeridth/1067988 to your computer and use it in GitHub Desktop.
Save jmeridth/1067988 to your computer and use it in GitHub Desktop.
implementation for visitor pattern blog post
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment