diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..c78a6db 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,62 @@ class NoApplesError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive + attr_reader :height, :age, :apples, :alive def initialize + @height = 0 + @age = 0 + @apples = [] + @alive = true end def age! + if @age.between?(0,5) + @age += 1 + @height += rand(1..3) + elsif @age.between?(5,10) + @age+=1 + @height += rand(1..3) + add_apples + else + @alive = false + end end def add_apples + rand(1..50).times do + @apples << Apple.new("red", rand(1..3)) + end end def any_apples? + @apples.length > 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 -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :color, :diameter#what should go here def initialize(color, diameter) + super() + @color = color + @diameter = diameter end end @@ -61,7 +85,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.length# It's up to you to calculate the average diameter for this harvest. 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..357e68d 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,13 +2,70 @@ require_relative '../lib/tree' describe Tree do + let (:tree) {Tree.new} + it 'should be a Class' do - expect(described_class.is_a? Class).to eq true + expect(Tree.is_a? Class).to eq true + end + + it 'should initalize empty' do + expect(tree.apples).to eq [] + expect(tree.alive).to eq true + expect(tree.height).to eq 0 + expect(tree.age).to eq 0 + end + + it 'should grow when it ages' do + tree.age! + expect(tree.height > 0).to eq true + expect(tree.age).to eq 1 + end + + it 'should gorw apples' do + 7.times {tree.age!} + expect(tree.any_apples?).to eq true + end + + it 'should be able to pick an apple' do + 7.times {tree.age!} + expect(tree.pick_an_apple!).to be_a Apple + end + + it 'should die of old age' do + 12.times {tree.age!} + expect(tree.alive).to eq false end end describe 'Fruit' do + let(:fruit) {Fruit.new} + + + it 'should be a Class' do + expect(Fruit.is_a? Class).to eq true + end + + it 'should have seeds' do + expect(fruit.has_seeds).to eq true + end end describe 'Apple' do + let(:apple) {Apple.new('Green', 2)} + + it 'should be a Class' do + expect(Apple.is_a? Class).to eq true + end + + it 'should have seeds' do + expect(apple.has_seeds).to eq true + end + + it 'should have color' do + expect(apple.color).to eq 'Green' + end + + it 'should have a diameter' do + expect(apple.diameter).to eq 2 + end end