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/1067996 to your computer and use it in GitHub Desktop.
Save jmeridth/1067996 to your computer and use it in GitHub Desktop.
ruby implementation for visitor pattern blog post
class Employee
attr_reader :name
attr_accessor :sickDaysUsed
attr_accessor :vacationDaysUsed
def initialize(name)
@name = name
@sickDaysUsed = 0
@vacationDaysUsed = 0
end
def addSickDaysUsed(sickDaysUsed)
@sickDaysUsed += sickDaysUsed
end
def addVacationDaysUsed(vacationDaysUsed)
@vacationDaysUsed += vacationDaysUsed
end
end
class HourlyEmployee < Employee
attr_reader :hourlyRate
attr_reader :hoursWorked
def initialize(name, hourlyRate)
super(name)
@hourlyRate = hourlyRate
end
def addHoursWorked(hoursWorked)
@hoursWorked = hoursWorked
end
def accept(visitor)
visitor.HourlyVisit(self)
end
end
class FulltimeEmployee < Employee
attr_reader :salary
attr_accessor :businessDaysWorked
def initialize(name, salary)
super(name)
@salary = salary
end
def addBusinessDaysWorked(businessDaysWorked)
@businessDaysWorked = businessDaysWorked
end
def accept(visitor)
visitor.FulltimeVisit(self)
end
end
class EmployeePaycheckVisitor
attr_reader :earnedWages
attr_reader :sickDayDeductions
attr_reader :paycheckInfo
def HourlyVisit(hourlyEmployee)
name = hourlyEmployee.name
hours = hourlyEmployee.hoursWorked
@sickDayDeductions = hourlyEmployee.sickDaysUsed * 8 * hourlyEmployee.hourlyRate
@earnedWages = hours * hourlyEmployee.hourlyRate – @sickDayDeductions
@paycheckInfo = %Q{Hourly employee #{name} worked #{hours} hours, earned
$#{“%0.2f” % @earnedWages} with #{hourlyEmployee.sickDaysUsed} sick day(s)
($#{“%0.2f” % @sickDayDeductions} deducted) and
#{hourlyEmployee.vacationDaysUsed} vacation day(s)}
end
def FulltimeVisit(fulltimeEmployee)
name = fulltimeEmployee.name
businessDaysWorked = fulltimeEmployee.businessDaysWorked
dailyRate = (fulltimeEmployee.salary / 52) / 5
@sickDayDeductions = fulltimeEmployee.sickDaysUsed * dailyRate
@earnedWages = businessDaysWorked * dailyRate – @sickDayDeductions
@paycheckInfo = %Q{Fulltime employee #{name} worked #{businessDaysWorked}
business days, earned $#{“%0.2f” % @earnedWages} with
#{fulltimeEmployee.sickDaysUsed} sick day(s) ($#{“%0.2f” %
@sickDayDeductions} deducted) and #{fulltimeEmployee.vacationDaysUsed}
vacation day(s)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment