From 75984b6d8a861399f71a160e0bb99deb75a4b5a0 Mon Sep 17 00:00:00 2001 From: Drew Valentine Date: Fri, 2 Feb 2018 13:11:33 -0500 Subject: [PATCH 1/3] finished up the realy basic tree --- lib/tree.rb | 25 +++++++++++++++++++++---- spec/tree_spec.rb | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..a299c59 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,18 +1,32 @@ class NoApplesError < StandardError; end -class AppleTree - attr_#fill_in :height, :age, :apples, :alive +class Tree + attr_accessor :height, :age, :apples, :alive def initialize + @height=0 + @age=0 + @apples=[] + @alive=true end def age! + @age += 1 + if @age > 50 + @alive=false + return + end + + @height += 1 + self.add_apples end def add_apples + @apples.push(Apple.new('red', 2)) end def any_apples? + @apples.size > 0 end def pick_an_apple! @@ -20,6 +34,7 @@ def pick_an_apple! end def dead? + not @alive end end @@ -29,10 +44,12 @@ def initialize end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + 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 99c9184..0a76e00 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -2,8 +2,39 @@ require 'tree' describe 'Tree' do + let(:tree) { Tree.new } + it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true + expect(tree.class).to be Tree + end + + it 'should age' do + current_age = tree.age + tree.age! + new_age = tree.age + expect(new_age == current_age + 1).to be true + end + + it 'should grow apples' do + current_apples = tree.apples.size + tree.add_apples + new_apples = tree.apples.size + expect(current_apples < new_apples).to be true + end + + it 'should report if the tree has any apples' do + tree.add_apples + expect(tree.any_apples?).to be true + end + + it 'should die after 50 years' do + i=0 + while i<51 + tree.age! + i +=1 + end + + expect(tree.dead?).to be true end end From 6543e339184dfbfc506474d788736c9e941101ce Mon Sep 17 00:00:00 2001 From: Drew Valentine Date: Fri, 2 Feb 2018 13:40:22 -0500 Subject: [PATCH 2/3] making the tree more realistic and finishing some tests --- lib/tree.rb | 12 +++++++++--- spec/tree_spec.rb | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index a299c59..63869ee 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -22,7 +22,12 @@ def age! end def add_apples - @apples.push(Apple.new('red', 2)) + num_apples = rand(1..5) * @age + i=0 + while i < num_apples + @apples.push(Apple.new('red', rand(1..3))) + i += 1 + end end def any_apples? @@ -31,6 +36,7 @@ def any_apples? def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.pop end def dead? @@ -78,7 +84,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 = basket.size > 0 ? diameter_sum / basket.size : 0 puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -93,4 +99,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 0a76e00..1dbb1fa 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -4,7 +4,7 @@ describe 'Tree' do let(:tree) { Tree.new } - it 'should be a Class' do + it 'should be a Tree' do expect(tree.class).to be Tree end @@ -27,19 +27,48 @@ expect(tree.any_apples?).to be true end + it 'should be able to have apples picked' do + i=0 + while i<10 + tree.add_apples + i += 1 + end + current_apples = tree.apples.size + tree.pick_an_apple! + new_apples = tree.apples.size + expect(new_apples == current_apples - 1).to be true + end + it 'should die after 50 years' do i=0 while i<51 tree.age! i +=1 end - expect(tree.dead?).to be true end end describe 'Fruit' do + let(:fruit){Fruit.new} + + it 'should be a Fruit' do + expect(fruit.class).to be Fruit + end end describe 'Apple' do + let(:apple){Apple.new('red', 2)} + + it 'should be an Apple' do + expect(apple.class).to be Apple + end + + it 'should be red' do + expect(apple.color == 'red').to be true + end + + it 'should have diameter 2' do + expect(apple.diameter == 2).to be true + end end From 4de9e2d48323588a9a918c75df8d4720a353ae61 Mon Sep 17 00:00:00 2001 From: Drew Valentine Date: Mon, 5 Feb 2018 10:05:18 -0500 Subject: [PATCH 3/3] using the integer times method instead of while loop --- lib/tree.rb | 6 ++---- spec/tree_spec.rb | 8 ++------ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 63869ee..ab4a054 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -22,11 +22,9 @@ def age! end def add_apples - num_apples = rand(1..5) * @age - i=0 - while i < num_apples + num_apples = rand(1..5) * @age + 1 + num_apples.times do @apples.push(Apple.new('red', rand(1..3))) - i += 1 end end diff --git a/spec/tree_spec.rb b/spec/tree_spec.rb index 1dbb1fa..9b27057 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -28,10 +28,8 @@ end it 'should be able to have apples picked' do - i=0 - while i<10 + 10.times do tree.add_apples - i += 1 end current_apples = tree.apples.size tree.pick_an_apple! @@ -40,10 +38,8 @@ end it 'should die after 50 years' do - i=0 - while i<51 + 51.times do tree.age! - i +=1 end expect(tree.dead?).to be true end