diff --git a/Gemfile.lock b/Gemfile.lock index 90f2f48..6025109 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.16.1 diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..b8fc94e 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,57 @@ 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 = [] + @alive = true end def age! + @age += 1 + @height += rand(1..(2*@age)) + self.add_apples end def add_apples + rand(1..(4*@age)).times do + @apples << Apple.new + end end def any_apples? + @apples.empty? ? false : true end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.slice!(rand(0...@apples.length)) end def dead? + @height > 60 || @age > 13 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) + def initialize + colors = ['red', 'green', 'yellow'] + @color = colors.sample + @diameter = rand(1..5).to_f end end @@ -41,7 +60,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? @@ -61,7 +80,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).round(2) puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -76,4 +95,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..bba07ee 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,58 @@ 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 + let(:tree) { AppleTree.new } + + it { expect(tree.class).to be AppleTree } + + it 'will age' do + tree.age = rand(1..10) + previous_tree_age = tree.age + tree.age! + expect(tree.age).to eq (previous_tree_age + 1) + end + + it 'can add apples' do + tree.age = 4 + tree.add_apples + expect(tree.any_apples?).to be true + end + + context 'when picking apples' do + + it 'raises an error for no apples' do + expect { tree.pick_an_apple! }.to raise_error(NoApplesError, "This tree has no apples") + end + + it 'removes an apple when present' do + tree.age = rand(1..12) + tree.add_apples + remaining_apples_count = tree.apples.length - 1 + tree.pick_an_apple! + expect(tree.apples.length).to eq remaining_apples_count + end + end end -describe 'Fruit' do +describe Fruit do + let(:fruit) { Fruit.new } + + it { expect(fruit.class).to be Fruit } + + it { expect(fruit.has_seeds).to be true } end -describe 'Apple' do +describe Apple do + let(:apple) { Apple.new } + + it "should have a color" do + colors = ['red', 'green', 'yellow'] + expect(colors).to include apple.color + end + + it 'should have a diameter' do + expect(apple.diameter).to be_between(1,5).inclusive + end end