diff --git a/Gemfile b/Gemfile index 0d89894..2f1b7a8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,3 @@ source 'https://rubygems.org' gem 'rspec' +gem 'pry' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index cb804a2..783d90c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,12 @@ GEM remote: https://rubygems.org/ specs: + coderay (1.1.2) diff-lcs (1.2.5) + method_source (0.9.0) + pry (0.11.3) + coderay (~> 1.1.0) + method_source (~> 0.9.0) rspec (3.1.0) rspec-core (~> 3.1.0) rspec-expectations (~> 3.1.0) @@ -19,7 +24,8 @@ PLATFORMS ruby DEPENDENCIES + pry rspec BUNDLED WITH - 1.15.3 + 1.16.1 diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..96c32f1 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,60 @@ +require 'pry' + class NoApplesError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive + attr_accessor :height, :age, :apples, :alive def initialize + @height = 1 + @age = 0 + @apples = [] + @alive = true end def age! + self.age += 1 + unless self.height >= 20 + self.height += 1 + end + self.age > 8 ? self.add_apples : false + self.dead? ? self.alive = false : self.alive = true end def add_apples + 3.times do self.apples << Apple.new("red", rand(2..4)) end end def any_apples? + self.apples.length > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + self.apples.pop end def dead? + self.age >= 100 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,8 +83,9 @@ def tree_data diameter_sum += apple.diameter end - avg_diameter = # It's up to you to calculate the average diameter for this harvest. - + # It's up to you to calculate the average diameter for this harvest. + avg_diameter = diameter_sum/basket.length + puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" puts "Harvest: #{basket.size} apples with an average diameter of #{avg_diameter} inches" @@ -77,3 +100,5 @@ def tree_data # Uncomment this line to run the script, but BE SURE to comment it before you try to run your tests! # tree_data + +# Tree.new \ No newline at end of file diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index b4f44c6..a73e56e 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -5,10 +5,80 @@ it 'should be a Class' do expect(described_class.is_a? Class).to eq true end + + let(:fuji) {Tree.new} + + it 'has a height, age, apples, alive' do + expect(fuji.height).to eq(1) + expect(fuji.age).to eq(0) + expect(fuji.apples).to be_empty + expect(fuji.alive).to be(true) + end + + it 'does not have apples when initialized' do + expect(fuji.any_apples?).to eq(false) + end + + it 'is not dead when initialized' do + expect(fuji.dead?).to eq(false) + end + + it 'ages' do + fuji.age! + expect(fuji.height).to eq(2) + expect(fuji.age).to eq(1) + expect(fuji.apples).to be_empty + expect(fuji.alive).to be(true) + end + + it 'stops growing after it is 20 feet tall' do + 21.times do fuji.age! end + expect(fuji.height).to eq(20) + end + + it 'has apples after it is 8 years old' do + 9.times do fuji.age! end + expect(fuji.any_apples?).to be(true) + end + + it 'has more than one apple after it is 9 years old' do + 10.times do fuji.age! end + expect(fuji.apples.length).to eq(6) + end + + it 'dies after it is 100 years old' do + 101.times do fuji.age! end + expect(fuji.dead?).to be(true) + expect(fuji.alive).to be(false) + end + end -describe 'Fruit' do +describe Fruit do + it 'should be a Class' do + expect(described_class.is_a? Class).to eq true + end + + let(:plum) {Fruit.new} + + it 'has seeds' do + expect(plum.has_seeds).to be(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 + + let(:gala) {Apple.new("pink", 4)} + + it 'has a color and diameter' do + expect(gala.color).to eq("pink") + expect(gala.diameter).to eq(4) + end + + it 'has seeds' do + expect(gala.has_seeds).to be(true) + end end