-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharge_more.rb
executable file
·98 lines (79 loc) · 2.35 KB
/
charge_more.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
96
97
98
#!/usr/bin/ruby
class Employee
attr_reader :name
def name=(name)
if name == ""
raise "Name cannot be blank!"
end
@name = name
end
def print_name
puts "Name: #{name}"
end
def initialize(name = "Anonymous")
self.name = name
end
end
class SalariedEmployee < Employee
attr_reader :salary
def salary=(salary)
if salary < 0
raise "A salary of #{salary} is not 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
puts format("Pay This Period: $%.2f", pay_for_period)
end
end
class HourlyEmployee < Employee
attr_reader :hourly_wage, :hours_per_week
def hourly_wage=(hourly_wage)
if hourly_wage < 0
raise "A hourly wage of #{hourly_wage} is not valid"
end
@hourly_wage = hourly_wage
end
def hours_per_week=(hours_per_week)
if hours_per_week < 0 or hours_per_week > 24 * 7
raise "#{hours_per_week} is not a valid amount of hours per week"
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
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
end
employees = [
HourlyEmployee.cashier("Ivan Stokes"),
HourlyEmployee.cashier("harold Nguyen"),
HourlyEmployee.cashier("Tamara Wells"),
HourlyEmployee.cashier("Susie Powell"),
HourlyEmployee.janitor("Edwin Burgess"),
HourlyEmployee.janitor("Ethel Harris"),
HourlyEmployee.security_guard("Angela Matthews"),
HourlyEmployee.security_guard("Stewart Sanchez")]
employees.each { |employee| employee.print_pay_stub }