From 8758d3cb122e8efcdd3881f1e4612f54ed020f17 Mon Sep 17 00:00:00 2001 From: John Chester Date: Tue, 6 Jun 2017 10:04:15 -0400 Subject: [PATCH] Jack Chester Solutions --- lib/tree.rb | 36 ++++++++++++++++++++++++------ spec/tree_spec.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..8906d1e 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,60 @@ 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 = 0 end def age! + @age += 1 + @height += 1 + @alive = false if @age > 15 + add_apples if @age % 3 == 0 end def add_apples + (1..10).each do |idx| + apple = Apple.new("Red", idx) + @apples.push(apple) + end end def any_apples? + return @apples.length > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + @apples.pop end def dead? + return !@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 :diameter + attr_reader :color def initialize(color, diameter) + @diameter = diameter + @color = color + + super() end end @@ -61,7 +83,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 = if basket.size > 0; diameter_sum / basket.size else 0 end # It's up to you to calculate the average diameter for this harvest. puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -76,4 +98,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..8d657f9 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,66 @@ require 'rspec' require 'tree' +testTree = Tree.new + describe 'Tree' do - it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true + it 'is a Class' do + expect(Tree.is_a? Class).to be true + end + + it 'grows one foot' do + testTree.age! + expect(testTree.height).to eq(1) + end + + it 'ages one year' do + expect(testTree.age).to eq(1) + end + + it 'has no fruit' do + expect(testTree.any_apples?).to be false + end + + + it 'has apples after 3 years' do + testTree.age! + testTree.age! + expect(testTree.any_apples?).to be true + end + + it 'dies after 16 years' do + (1..13).each{ |idx| testTree.age!} + expect(testTree.dead?).to be true end end describe 'Fruit' do + it 'is a Class' do + expect(Fruit.is_a? Class).to be true + end end +testApple = Apple.new("Red", 30) + describe 'Apple' do + it 'is a Class' do + expect(Apple.is_a? Class).to be true + end + + it 'is a Fruit' do + expect(Apple < Fruit).to be true + end + + + it 'sets a color' do + expect(testApple.color).to eq("Red") + end + + it 'sets a diameter' do + expect(testApple.diameter).to eq(30) + end + + it 'calls super()' do + expect(testApple.has_seeds).to be true + end end