-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_inheritance.rb
More file actions
56 lines (43 loc) · 1.01 KB
/
class_inheritance.rb
File metadata and controls
56 lines (43 loc) · 1.01 KB
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
class Employee
def initialize(name,title,salary,boss)
@name, @title, @salary = name, title, salary
self.boss = boss
end
def bonus(multiplier)
self.salary * multiplier
end
def boss=(manager)
unless manager.nil?
@boss = manager
manager.employees << self
end
end
protected
attr_reader :salary
end
class Manager < Employee
attr_accessor :employees
def initialize(name,title,salary,boss)
super
@employees = []
end
def bonus(multiplier)
sum = 0
subordinates = self.employees
until subordinates.empty?
sub = subordinates.shift
subordinates += sub.employees if sub.is_a?(Manager)
sum += sub.salary
end
sum*multiplier
end
end
if __FILE__ == $PROGRAM_NAME
ned = Manager.new("Ned","Founder",1000000,nil)
darren = Manager.new("Darren","TA Manager",78000,ned)
shawna = Employee.new("Shawna","TA",12000,darren)
david = Employee.new("David","TA",10000,darren)
p ned.bonus(5)
p darren.bonus(4)
p david.bonus(3)
end