diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..32c1e51 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,64 @@ class NoApplesError < StandardError; end +class DeadTreeError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive + attr_reader :height, :age, :apples, :alive - def initialize + DEATH_AGE = 60 + FRUIT_AGE = 5 + + def initialize(height = 0, age = 0, apples = [], alive = true) + @height = height + @age = age + @apples = apples + @alive = alive end def age! - end + raise DeadTreeError, "This tree is dead" unless self.alive - def add_apples + @age += 1 + @height += rand(2..4) + @alive = @age < DEATH_AGE + + add_apples if @alive && @age >= FRUIT_AGE end def any_apples? + @apples.any? end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + + @apples.pop end def dead? + !@alive + end + + def add_apples + apples_to_add = rand(10..15) + + apples_to_add.times do + @apples << Apple.new('red', rand(1..4)) + end end end class Fruit 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) + @color = color + @diameter = diameter end end @@ -61,7 +87,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.to_f / basket.length puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index b4f44c6..d43c534 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -5,10 +5,86 @@ it 'should be a Class' do expect(described_class.is_a? Class).to eq true end + + describe '#age!' do + subject { tree } + + context 'new tree' do + let(:tree) { Tree.new } + + it 'should age 1 years' do + tree.age! + expect(tree.age).to eq 1 + end + + it 'should grow at least 2 feet a year' do + tree.age! + expect(tree.height).to be >= 2 + end + end + + context 'a fruit bearing tree' do + let(:tree) { Tree.new(0, Tree::FRUIT_AGE, [], true) } + + it 'should add apples every year' do + tree.age! + expect(tree.any_apples?).to eq true + end + end + + context 'almost dead tree' do + let(:tree) { Tree.new(0, Tree::DEATH_AGE - 1, [], true) } + + it "should die when it when hits #{Tree::DEATH_AGE} years old" do + tree.age! + expect(tree.dead?).to eq true + end + end + + context 'a dead tree' do + let(:tree) { Tree.new(0, Tree::DEATH_AGE, [], false) } + + it 'should throw an error if aged after tree is dead' do + expect { tree.age! }.to raise_error DeadTreeError + end + end + end + + describe '#pick_an_apple!' do + subject { tree } + + context 'tree without apples' do + let(:tree) { Tree.new } + + it 'should throw an error' do + expect { tree.pick_an_apple! }.to raise_error NoApplesError + end + end + + context 'tree with one apple' do + let(:tree) { Tree.new(0, Tree::FRUIT_AGE, [Apple.new('red', 2)], true) } + + it 'should return the last apple' do + picked_apple = tree.pick_an_apple! + expect(picked_apple.color).to eq 'red' + end + + it 'should remove the apple from the tree' do + tree.pick_an_apple! + expect(tree.any_apples?).to eq false + end + end + 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 'should be a Class' do + expect(described_class.is_a? Class).to eq true + end end