forked from guofan/coursework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch9-3.rb
57 lines (45 loc) · 786 Bytes
/
ch9-3.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
# orange tree
class OrangeTree
def initialize
@age = 0
@maxAge = 20
@height = 0
@heightFactor = 0.6
@orangeCount = 0
end
def height
@height
end
def oneYearPasses
@age = @age + 1
if @age > @maxAge
@orangeCount = 0
return nil
end
@orangeCount = getOrangeCount
@height = getHeight
end
def countTheOranges
@orangeCount
end
def pickAnOrange
if @orangeCount < 1
return "No orange left for you to taste."
end
@orangeCount = @orangeCount - 1
"Sweet and fresh!!"
end
private
def getHeight
@heightFactor * @age
end
def getOrangeCount
(@age - 1) * (@age - 2)
end
end
#a = OrangeTree.new
#24.times do
# a.oneYearPasses
# print "#{a.countTheOranges} "
#end
#puts