Last active
September 6, 2016 10:15
-
-
Save varjmes/1db81a458bf55e2258aec7256922b300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Make an OrangeTree class. It should have a height method which returns its height, and a oneYearPasses method, | |
which, when called, ages the tree one year. Each year the tree grows taller (however much you think an orange | |
tree should grow in a year), and after some number of years (again, your call) the tree should die. For the | |
first few years, it should not produce fruit, but after a while it should, and I guess that older trees | |
produce more each year than younger trees... whatever you think makes most sense. And, of course, you should | |
be able to countTheOranges (which returns the number of oranges on the tree), and pickAnOrange (which reduces | |
the @orangeCount by one and returns a string telling you how delicious the orange was, or else it just tells | |
you that there are no more oranges to pick this year). Make sure that any oranges you don't pick one year | |
fall off before the next year. | |
=end | |
class OrangeTree | |
def initialize | |
@age = 0 | |
@alive = true | |
@height = 0 | |
@years_passed = 0 | |
@fruit = 0 | |
end | |
attr_reader :fruit, :height | |
def one_year_passes | |
return unless @alive | |
@years_passed += 1 | |
@age += 1 | |
@height += 10 | |
@fruit = 0 | |
status | |
fruiting | |
end | |
def pick_an_orange | |
if @fruit > 0 | |
@fruit -= 1 | |
'That orange was delicious!' | |
else | |
'There is no fruit to pick!' | |
end | |
end | |
private | |
def status | |
@alive = false unless @age <= 10 | |
unless @alive | |
puts 'Tree died' | |
end | |
end | |
def can_fruit? | |
@alive && @age > 2 | |
end | |
def fruiting | |
if can_fruit? | |
@fruit += 2 | |
end | |
end | |
end | |
tree = OrangeTree.new | |
tree.height | |
tree.one_year_passes | |
tree.one_year_passes | |
tree.one_year_passes | |
puts tree.fruit | |
tree.one_year_passes | |
puts tree.fruit | |
puts tree.pick_an_orange | |
puts tree.fruit | |
tree.one_year_passes | |
tree.one_year_passes | |
tree.one_year_passes | |
tree.one_year_passes | |
tree.one_year_passes | |
tree.one_year_passes | |
puts tree.one_year_passes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment