From 99bbaecd27de2e86a8ecaad448d1770b2d5bfc23 Mon Sep 17 00:00:00 2001 From: Justin Holm Date: Fri, 12 May 2017 10:16:12 -0400 Subject: [PATCH] Justin Holm solution --- Gemfile.lock | 4 +++ lib/tree.rb | 51 +++++++++++++++++++++++------ spec/tree_spec.rb | 82 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 126 insertions(+), 11 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 90f2f48..1ac1490 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,4 +1,5 @@ GEM + remote: https://rubygems.org/ specs: diff-lcs (1.2.5) rspec (3.1.0) @@ -19,3 +20,6 @@ PLATFORMS DEPENDENCIES rspec + +BUNDLED WITH + 1.14.6 diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..5e4c1b2 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,69 @@ class NoApplesError < StandardError; end -class AppleTree - attr_#fill_in :height, :age, :apples, :alive - - def initialize +class Tree + attr_reader :height, :age, :apples, :alive + + def initialize(height = 0, age = 0, apples = [], alive = true) + @height = height + @age = age + @apples = apples + @alive = alive end def age! + # Age our tree by one year + @age += 1 + + # Grow our tree a rand height between 1-3 feet per year + @height += rand(1..3) + + # Wait for our tree to age until 5, grow some fruit, then steal it + if @age >= 5 + rand(100..300).times{ add_apples } + end + + # Our poor tree dies exactly on its 30th birthday, assuming its fruit has been picked + if @age >= 30 + @alive = false + end end def add_apples + # Push a new red apple. Choose a random diameter size between 5-10 inch + @apples.push Apple.new('red', rand(4..10)) 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? + !@alive end end class Fruit + attr_reader :has_seeds + def initialize - has_seeds = true + @has_seeds = true end -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 + + # Call our Fruit initialize method + super() end end @@ -61,7 +92,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 = basket.count > 0 ? diameter_sum / basket.count : 0 puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -76,4 +107,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 99c9184..cbbc2c9 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,13 +2,93 @@ require 'tree' describe 'Tree' do + before :each do + @tree = Tree.new + end + it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true + expect(@tree.is_a? Tree).to eq true + end + + it 'should have no (0) age initially' do + expect(@tree.age).to eq 0 + end + + it 'should have no (0) height initially' do + expect(@tree.height).to eq 0 + end + + it 'should have no apples initially' do + expect(@tree.apples.count).to eq 0 + end + + it 'should be alive' do + expect(@tree.alive).to eq true + end + + it 'should increase age by 1 when age is called' do + @tree.age! + expect(@tree.age).to eq 1 + end + + it 'should increase height every year after its first 5 years' do + @tree.age! + expect(@tree.height).not_to eq 0 + end + + it 'should have apples after the first 5 years' do + 5.times{ @tree.age! } + expect(@tree.apples.count).not_to eq 0 + end + + it 'should die after 30 years' do + 30.times{ @tree.age! } + expect(@tree.alive).to eq false + end + + it 'should be able to pick an apple' do + basket = [] + while @tree.apples.count == 0 + @tree.age! + end + + basket << @tree.pick_an_apple! + expect(basket.count).to eq 1 + end + + it 'should decrease the total amount of apples when they are picked' do + basket = [] + while @tree.apples.count == 0 + @tree.age! + end + + num_before = @tree.apples.count + basket << @tree.pick_an_apple! + expect(@tree.apples.count).not_to eq num_before end end describe 'Fruit' do + it 'should be a class' do + @fruit = Fruit.new + expect(@fruit.is_a? Fruit).to eq true + end end describe 'Apple' do + before :each do + @apple = Apple.new('red', 5) + end + + it 'should be a class' do + expect(@apple.is_a? Apple).to eq true + end + + it 'should have a color' do + expect(@apple.color).to eq 'red' + end + + it 'should have a diameter of 5' do + expect(@apple.diameter).to eq 5 + end end