From 0a7ef35da7fb5e4ffc8c30a33494804a3b74d84f Mon Sep 17 00:00:00 2001 From: Leslie Kornes Date: Fri, 11 May 2018 15:44:17 -0400 Subject: [PATCH] Fixed the tree. --- lib/tree.rb | 35 ++++++++++++++++++++++++------ spec/tree_spec.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..e589ab0 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,61 @@ class NoApplesError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive + attr_reader :height, :age, :apples, :alive def initialize + @age = 0 + @height = 0 + @apples = [] + @alive = true end def age! + @age += 1 + @height += 2 + add_apples + dead? end def add_apples + if (@age > 2 && @age < 13) && !dead? + (5 * @age).times { @apples.push(Apple.new('green', rand(1..4))) } + end + @apples.count end def any_apples? + @apples.count > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.pop end def dead? + if @age == 13 + @alive = false + true + end end end class Fruit + attr_reader :has_seeds + def initialize - has_seeds = true + @has_seeds = true end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :color, :diameter def initialize(color, diameter) + super() + @color = color + @diameter = diameter end end @@ -61,7 +84,7 @@ 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.count puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -76,4 +99,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 b4f44c6..9f55456 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -5,10 +5,65 @@ it 'should be a Class' do expect(described_class.is_a? Class).to eq true end + + let(:t) { Tree.new } + + it 'is initaialized with expected values for attributes' do + expect(t.age).to eq 0 + expect(t.height).to eq 0 + expect(t.apples).to eq [] + expect(t.alive).to be(true) + end + + it 'ages' do + t.age! + expect(t.age).to eq 1 + expect(t.apples).to eq [] + end + + it 'does not produce apples for first 2 years' do + 2.times { t.age! } + expect(t.apples).to eq [] + end + + it 'produces 5 apples when aged 3 years' do + 3.times { t.age! } + expect(t.apples.count).to eq 15 + end + + it 'can be picked for apples' do + 3.times { t.age! } + t.pick_an_apple! + expect(t.apples).not_to be_empty + end + + it 'dies when it ages beyond 12 years' do + 13.times { t.age! } + expect(t.alive).to be(false) + end end describe 'Fruit' do + it "should have seeds" do + f = Fruit.new + expect(f.has_seeds).to be(true) + end end describe 'Apple' do + + let(:a) { Apple.new("red", 2) } + + it "has super class' attributes" do + expect(a.has_seeds).to be(true) + end + + it "is red" do + expect(a.color).to eq ("red") + end + + it "has a diameter" do + expect(a.diameter).to eq 2 + end + end