Skip to content

Instantly share code, notes, and snippets.

@ayoformayo
Created June 11, 2013 00:05
Show Gist options
  • Save ayoformayo/5753557 to your computer and use it in GitHub Desktop.
Save ayoformayo/5753557 to your computer and use it in GitHub Desktop.
class TreeGrove
def initialize(array)
end
def age!
p "Hello!"
end
def trees
end
def mature_trees
end
def dead_trees
end
end
class FruitTree
attr_reader :age, :height
def initialize(fruit_type, max_age, yearly_growth, maturity, num_fruit_born)
@age = 0
@dead = false
@height = 0
@number_of_fruit = []
@@fruit_type = fruit_type
@@max_age = max_age
@@yearly_growth = yearly_growth
@@maturity = maturity
@@num_fruit_born = num_fruit_born
end
def age!
@age += 1
@height += @@yearly_growth
@dead = true if @age > @@max_age
if @age >= @@maturity
@@num_fruit_born.times do
@number_of_fruit << @@fruit_type
end
end
end
def any_fruit?
@number_of_fruit.length > 0
end
def dead?
@dead
end
def pick_a_fruit!
if any_fruit?
@number_of_fruit.pop
else
puts "No fruit for you!"
end
end
end
class Fruit
attr_reader :diameter
end
class PearTree < FruitTree
end
class BananaTree < FruitTree
end
class CoconutTree < FruitTree
end
class Banana < Fruit
def initialize
@diameter = 1 + rand(11)
end
end
class Coconut < Fruit
def initialize
@diameter = 1 + rand(7)
end
end
class Pear < Fruit
def initialize
@diameter = 1 + rand(7)
end
end
class OrangeTree
@@trees = 0
attr_reader :age, :height
def initialize
@age = 0
@dead = false
@height = 0
@@trees += 1
@number_of_oranges = []
end
def age!
@age += 1
@height += 5
@dead = true if @age >= 12
if @age >= 2
2.times do
@number_of_oranges << Orange.new
end
end
end
def any_oranges?
@number_of_oranges.length > 0
end
def dead?
@dead
end
def pick_an_orange!
if any_oranges?
@number_of_oranges.pop
else
puts "No oranges!"
end
end
end
class Orange
attr_reader :diameter
def initialize
@diameter = 1 + rand(6)
end
end
class AppleTree
@@trees = 0
attr_reader :age, :height
def initialize
@age = 0
@dead = false
@height = 0
@@trees += 1
@number_of_apples = []
end
def age!
@age += 1
@height += 2
@dead = true if @age >= 12
if @age >= 1
6.times do
@number_of_apples << Apple.new
end
end
end
def any_apples?
@number_of_apples.length > 0
end
def dead?
@dead
end
def pick_an_apple!
if any_apples?
@number_of_apples.pop
else
puts "No apples!"
end
end
end
class Apple
attr_reader :diameter
def initialize
@diameter = 1 + rand(3)
end
end
typical_pear = Pear.new
typical_banana = Banana.new
typical_coconut = Coconut.new
typical_pear_tree = PearTree.new(typical_pear, 10,4,2,10)
coconut_tree = CoconutTree.new(typical_coconut, 12, 15, 6, 4)
banana_tree = BananaTree.new(typical_banana, 4, 15, 1, 20)
my_tree_grove = TreeGrove.new([typical_pear_tree, coconut_tree, banana_tree])
my_tree_grove.age!
# # tree = OrangeTree.new
# typical_pear_tree.age! until typical_pear_tree.any_fruit?
# puts "Tree is #{typical_pear_tree.age} years old and #{typical_pear_tree.height} feet tall"
# until typical_pear_tree.dead?
# basket = []
# #It places the oranges in the basket
# # IT PLACES THE ORANGES IN THE BASKET
# while typical_pear_tree.any_fruit?
# basket << typical_pear_tree.pick_a_fruit!
# end
# sum = 0
# basket.each do |x|
# sum += x.diameter
# end
# avg_diameter = sum / basket.size
# puts "Year #{typical_pear_tree.age} Report"
# puts "Tree height: #{typical_pear_tree.height} feet"
# puts "Harvest: #{basket.size} pieces of fruit with an average diameter of #{avg_diameter} inches" #
# puts ""
# # Age the tree another year
# typical_pear_tree.age!
# end
#puts "Alas, the tree, she is dead!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment