diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..a19baee 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,25 +1,35 @@ class NoApplesError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive + attr_reader :height, :age, :apples, :alive - def initialize + def initialize(height, age, apples, alive) + @height = height + @age = age + @apples = apples + @alive = alive end def age! + add_apples unless @age < 3 + @age += 1 end def add_apples + rand(5..15).times { @apples.push(Apple.new) } end def any_apples? + @apples.size > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.shift() end def dead? + age > 10 end end @@ -29,19 +39,17 @@ def initialize end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :color, :diameter - def initialize(color, diameter) + def initialize() + @color = ['red','yellow', 'green'].sample + @diameter = rand(5..10) end end -#THERES ONLY ONE THING YOU NEED TO EDIT BELOW THIS LINE -# avg_diameter (line 58) will raise an error. -# it should calculate the diameter of the apples in the basket - def tree_data - tree = Tree.new + tree = Tree.new(5, 1, [], true) tree.age! until tree.any_apples? @@ -50,7 +58,6 @@ def tree_data until tree.dead? basket = [] - # It places the apple in the basket while tree.any_apples? basket << tree.pick_an_apple! end @@ -61,14 +68,13 @@ def tree_data diameter_sum += apple.diameter end - avg_diameter = # It's up to you to calculate the average diameter for this harvest. + avg_diameter = diameter_sum/basket.size puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" puts "Harvest: #{basket.size} apples with an average diameter of #{avg_diameter} inches" puts "" - # Ages the tree another year tree.age! end diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index b4f44c6..6bc483e 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -5,10 +5,28 @@ it 'should be a Class' do expect(described_class.is_a? Class).to eq true end + it 'should create an instance of Tree with correct attributes' do + tree = Tree.new(5, 1, 0, true) + expect(tree.height).to eq(5); + expect(tree.age).to eq(1); + expect(tree.apples).to eq(0); + expect(tree.alive).to eq(true); + end end -describe 'Fruit' do +describe Fruit do + it 'should be a Class' do + expect(described_class.is_a? Class).to eq true + end + end -describe 'Apple' do +describe Apple do + it 'Apple should be a subclass of Fruit' do + expect(Apple).to be < Fruit + end + it 'apple should be a an instance of Apple' do + apple = Apple.new() + expect(apple).to be_an_instance_of(Apple) + end end