From 5bbc3ade13b3bd923a2f104416c520936a7f3767 Mon Sep 17 00:00:00 2001 From: Andrew Bonner Date: Thu, 10 May 2018 16:01:41 -0400 Subject: [PATCH 1/5] Age tree. --- lib/tree.rb | 10 +++++++--- spec/tree_spec.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 962b72e..6fa6704 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,12 +1,16 @@ class NoApplesError < StandardError; end class Tree - attr_#fill_in :height, :age, :apples, :alive + attr_accessor :age + +# attr_fill_in :height, :age, :apples, :alive def initialize + @age = 0 end def age! + self.age += 1 end def add_apples @@ -29,8 +33,8 @@ def initialize end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + # attr_reader #what should go here def initialize(color, diameter) end diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index b4f44c6..2033d73 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -5,6 +5,16 @@ it 'should be a Class' do expect(described_class.is_a? Class).to eq true end + + let(:tree) { Tree.new } + + it "is initialized as a young tree" do + expect(tree.age).to eq(0) + end + + it "can age" do + expect(tree.age!).to eq(1) + end end describe 'Fruit' do From 447c133acf4e6a35eed86c9664c209243210b4a0 Mon Sep 17 00:00:00 2001 From: Andrew Bonner Date: Thu, 10 May 2018 16:16:19 -0400 Subject: [PATCH 2/5] Tree grows. --- lib/tree.rb | 12 ++++++++++-- spec/tree_spec.rb | 8 +++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 6fa6704..ea4f8a9 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,16 +1,24 @@ class NoApplesError < StandardError; end class Tree - attr_accessor :age + attr_accessor :height, :age # attr_fill_in :height, :age, :apples, :alive def initialize - @age = 0 + self.height = 0 + self.age = 0 end def age! self.age += 1 + self.grow + end + + def grow + if self.height < 20 + self.height += 2 + end end def add_apples diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 2033d73..1e5dc65 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -13,7 +13,13 @@ end it "can age" do - expect(tree.age!).to eq(1) + tree.age! + expect(tree.age).to eq(1) + end + + it "grows as it ages" do + tree.age! + expect(tree.height).to eq(2) end end From f46b52846a0bfbddcc215064908f464c54bb46be Mon Sep 17 00:00:00 2001 From: Andrew Bonner Date: Thu, 10 May 2018 16:23:49 -0400 Subject: [PATCH 3/5] Tree doesn't have apples for 3 years. --- lib/tree.rb | 4 +++- spec/tree_spec.rb | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/tree.rb b/lib/tree.rb index ea4f8a9..196a159 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,13 +1,14 @@ class NoApplesError < StandardError; end class Tree - attr_accessor :height, :age + attr_accessor :height, :age, :apples # attr_fill_in :height, :age, :apples, :alive def initialize self.height = 0 self.age = 0 + self.apples = [] end def age! @@ -25,6 +26,7 @@ def add_apples end def any_apples? + self.apples.length > 0 end def pick_an_apple! diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 1e5dc65..0eeb8c6 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -21,6 +21,11 @@ tree.age! expect(tree.height).to eq(2) end + + it "doesn't have apples for 3 years" do + 3.times { tree.age! } + expect(tree.any_apples?).to be(false) + end end describe 'Fruit' do From 73b4bbd43fbbfecb4ff21b2684285eda5c7c5a27 Mon Sep 17 00:00:00 2001 From: Andrew Bonner Date: Thu, 10 May 2018 16:50:54 -0400 Subject: [PATCH 4/5] Pass apple tests. --- lib/tree.rb | 4 +++- spec/tree_spec.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/tree.rb b/lib/tree.rb index 196a159..0bb52d0 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -44,9 +44,11 @@ def initialize end class Apple < Fruit - # attr_reader #what should go here + attr_reader :color, :diameter def initialize(color, diameter) + @color = color + @diameter = diameter end end diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 0eeb8c6..834ee56 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -26,10 +26,28 @@ 3.times { tree.age! } expect(tree.any_apples?).to be(false) end + + it "starts producing apples after 3 years" do + 4.times { tree.age! } + expect(tree.any_apples?).to be(true) + end end describe 'Fruit' do end describe 'Apple' do + let(:apple) { Apple.new("red", 3.5)} + + it "has a color" do + expect(apple.color).to eq("red") + end + + it "has a diameter" do + expect(apple.diameter).to eq(3.5) + end + + it "has seeds" do + expect(apple.has_seeds).to be(true) + end end From c1e943f8e3ee1dd963d5f6b177ecbd393f42f674 Mon Sep 17 00:00:00 2001 From: Andrew Bonner Date: Fri, 11 May 2018 09:37:36 -0400 Subject: [PATCH 5/5] All tests pass. --- lib/tree.rb | 23 ++++++++++++++++++----- spec/tree_spec.rb | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 0bb52d0..c4d4663 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,19 +1,23 @@ class NoApplesError < StandardError; end class Tree - attr_accessor :height, :age, :apples - -# attr_fill_in :height, :age, :apples, :alive + attr_accessor :height, :age, :apples, :alive def initialize self.height = 0 self.age = 0 self.apples = [] + self.alive = true end def age! self.age += 1 self.grow + self.add_apples + + if self.age > 20 + self.alive = false + end end def grow @@ -23,6 +27,9 @@ def grow end def add_apples + if self.age > 3 + 5.times { self.apples.push(Apple.new("red", rand(3.0..4.5).round(2))) } + end end def any_apples? @@ -31,15 +38,19 @@ def any_apples? def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + apples.shift end def dead? + !self.alive end end class Fruit + attr_reader :has_seeds + def initialize - has_seeds = true + @has_seeds = true end end @@ -47,6 +58,8 @@ class Apple < Fruit attr_reader :color, :diameter def initialize(color, diameter) + super() + @color = color @diameter = diameter end @@ -77,7 +90,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" diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 834ee56..a8df260 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -12,6 +12,10 @@ expect(tree.age).to eq(0) end + it "is alive" do + expect(tree.dead?).to be(false) + end + it "can age" do tree.age! expect(tree.age).to eq(1) @@ -31,9 +35,22 @@ 4.times { tree.age! } expect(tree.any_apples?).to be(true) end + + it "can be harvested for apples" do + 4.times { tree.age! } + expect(tree.pick_an_apple!).to be_instance_of(Apple) + end + + it "dies after 20 years" do + 21.times { tree.age! } + expect(tree.dead?).to be(true) + end end describe 'Fruit' do + it "has seeds" do + expect(Fruit.new.has_seeds).to be(true) + end end describe 'Apple' do