From 2e5a43f2d65959602137cbc0879a4ad3d1755dce Mon Sep 17 00:00:00 2001 From: shabbir Date: Tue, 20 Feb 2018 13:48:25 -0500 Subject: [PATCH] solution and tests added --- lib/tree.rb | 46 +++++++++++++++++++++++++++++++++++++++------- spec/tree_spec.rb | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..0b61bf6 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,25 +1,50 @@ class NoApplesError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive + attr_accessor :height, :age, :apples, :alive - def initialize + def initialize (height =0, age =0, apples =[], alive =true) + @height = height + @age = age + @apples = apples + @alive =true end + # Ages the tree by a year, and increments the height of the tree by a random value + # Adds the apples to the tree when it matures( at age 10) + # Kills the tree at a certain age (i.e 100) def age! + @age +=1 + @height += rand(0..5) + if @age>=10 + add_apples(rand(1..50)) + end + if @age==100 + @alive = false + @apples = [] + end end - def add_apples + #Adds the given amount of apples to the tree with a random color and diameter + def add_apples(count) + count.times{ + @apples << Apple.new(['red','green'].sample,rand(5..15)) + } end def any_apples? + @apples.length > 0 end + #Throws an exception if tree has no apples else removes an apple from the tree def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.pop end + def dead? + not @alive end end @@ -29,10 +54,12 @@ def initialize end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :color, :diameter def initialize(color, diameter) + @color = color + @diameter = diameter end end @@ -61,7 +88,12 @@ def tree_data diameter_sum += apple.diameter end - avg_diameter = # It's up to you to calculate the average diameter for this harvest. + #calculate avg diameter of the apples in basket + if diameter_sum > 0 + avg_diameter = diameter_sum / basket.length + else + avg_diameter = 0 + end puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -76,4 +108,4 @@ def tree_data end # Uncomment this line to run the script, but BE SURE to comment it before you try to run your tests! -# tree_data +#tree_data diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 6394020..8d37d62 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -5,10 +5,48 @@ it 'should be a Class' do expect(described_class.is_a? Class).to eq true end + + #Creating an instance of the tree class + let (:tree) {Tree.new} + + it 'should be alive initially' do + expect(tree.dead?).to eq false + end + + it 'should have no apples initially' do + expect(tree.any_apples?).to eq false + end + + it 'should grow and age' do + tree.age! + expect(tree.age > 0).to true + end + + it 'should add apples after certain age' do + tree.add_apples(10) + expect(tree.any_apples?).to eq true + end + + it 'pick an apple if available else throw an exception' do + exepct(tree.pick_an_apple!.is_a? Apple).to eq true + end + + it 'should die after a certian age' + tree.age = 100 + expect(tree.dead?).to eq true end describe 'Fruit' do + it 'should be a class' do + expect(described_class.is_a? Class).to eq true + end end describe 'Apple' do + it 'should be a class' do + expect(described_class.is_a? Class).to eq true + end + it 'Should be a fruit' do + expect(Apple.superclass).to eq Fruit + end end