diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..8874240 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,55 @@ class NoApplesError < StandardError; end class AppleTree - attr_#fill_in :height, :age, :apples, :alive + attr_accessor :height, :age, :apples, :alive def initialize + @height = 0 + @age = 0 + @apples = 0 + @alive = true end def age! + @age+=1 + @alive = false unless @age < 9 + @height+=rand(20) + self.add_apples + @age end def add_apples + rand(25..100).times{@apples+=1} end def any_apples? + @apples != 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples-=1 + Apple.new('Red', rand(3.0..5.0)) end def dead? + !@alive 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 #what should go here def initialize(color, diameter) + @color = color + @diameter = diameter end end @@ -41,7 +58,7 @@ def initialize(color, diameter) # it should calculate the diameter of the apples in the basket def tree_data - tree = Tree.new + tree = AppleTree.new tree.age! until tree.any_apples? @@ -55,13 +72,14 @@ def tree_data basket << tree.pick_an_apple! end + diameter_sum = 0 basket.each do |apple| 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" diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 99c9184..bdf80ac 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,80 @@ require 'rspec' require 'tree' -describe 'Tree' do - it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true +describe AppleTree do + context 'when AppleTree exists' do + subject(:tree) { AppleTree.new } + it 'should be a Class' do + expect(described_class.is_a? Class).to be true + end + + it 'should be a newborn tree' do + expect(tree.age).to eq 0 + end + + it 'should have no apples' do + expect(tree.any_apples?).to eq false + end + + it 'should not be able to have an apple picked' do + expect{tree.pick_an_apple!}.to raise_error NoApplesError + end + + it 'should be alive' do + expect(tree.alive).to eq true + end + + it 'should age when told to' do + expect(tree.age!).to eq 1 + end + + it 'should get taller as it ages' do + tree.age! + expect(tree.height).to be > 0 + end + + it 'should die after 10 years' do + 10.times { + tree.age! + } + expect(tree.dead?).to eq true + end + + it 'should produce fruit after 1 year' do + tree.age! + expect(tree.any_apples?).to eq true + end + + it 'should let me pick an apple' do + tree.age! + expect(tree.pick_an_apple!).to respond_to :diameter + end end + end -describe 'Fruit' do +describe Fruit do + subject(:fruit) {Fruit.new} + it 'should be a Class' do + expect(described_class.is_a? Class).to be true + end + + it 'should have seeds' do + expect(fruit.has_seeds).to eq true + end end -describe 'Apple' do +describe Apple do + subject(:apple){Apple.new('Red', 5)} + it 'should be a Class' do + expect(described_class.is_a? Class).to be true + end + + it 'should have a color' do + expect(apple.color).to eq 'Red' + end + + it 'should have a diameter' do + expect(apple.diameter).to eq 5 + end end