-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemployees_4.rb
95 lines (74 loc) · 2.02 KB
/
employees_4.rb
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
class Employee
attr_reader :name
def name= (name)
if name == ""
raise "Name can't be blank!"
end
@name = name
end
def initialize (name = "Anonymous")
self.name = name
end
def print_name
puts "Name: #{name}"
end
class SalariedEmployee < Employee
attr_reader :salary
def salary= (salary)
if salary < 0
raise "A salary of #{salary} isn't valid!"
end
@salary = salary
end
def initialize (name = "Anonymous", salary = 0.0)
super (name)
self.salary = salary
end
def print_pay_stub
print_name
pay_for_period = (salary / 365.0) * 14
formatted_pay = format ("$%.2f", pay_for_period)
puts "Pay This Period: #{formatted_pay}"
end
end
class HourlyEmployee < Employee
def self.security_guard (name)
HourlyEmployee.new (name, 19.25, 30)
end
def self.cashier (name)
HourlyEmployee.new (name, 12.75, 25)
end
def self.janitor (name)
HourlyEmployee.new (name, 10.50, 20)
end
attr_reader :hourly_wage, :hours_per_week
def hourly_wage= (hourly_wage)
if hourly_wage < 0
raise "An hourly_wage of #{hourly_wage} isn't valid"
end
@hourly_wage = hourly_wage
end
def hours_per_week= (hours_per_week)
if hours_per_week < 0
raise "#{hours_per_week} hours per week isn't valid"
end
@hours_per_week = hours_per_week
end
def initialize (name = "Anonymous", hourly_wage = 0.0, hours_per_week = 0.0)
super(name)
self.hourly_wage = hourly_wage
self.hours_per_week = hours_per_week
end
def print_pay_stub
print_name
pay_for_period = hourly_wage * hours_per_week * 2
formatted_pay = format ("¢%.2f", pay_for_period)
puts "Pay This Period: #{formatted_pay}"
end
end
jane = SalariedEmployee.new("Jane Doe", 50000)
jane.print_pay_stub
angela = HourlyEmployee.security_guard("Angela Matthews")
ivan = HourlyEmployee.cashier("Ivan Stokes")
angela.print_pay_stub
ivan.print_pay_stub