diff --git a/lib/tree.rb b/lib/tree.rb index 6c54019..64576cb 100644 --- a/lib/tree.rb +++ b/lib/tree.rb @@ -1,38 +1,75 @@ class NoApplesError < StandardError; end -class AppleTree - attr_#fill_in :height, :age, :apples, :alive +class Tree + attr_reader :height + attr_reader :age + attr_reader :apples + attr_reader :alive + attr_reader :treeAppleColor def initialize + @age = 0 + @height = 1 + @apples = Array.new + @alive = true + @treeAppleColor = "Red" end def age! + @age += 1 + + self.add_apples + self.grow + + if @age >= 50 + @alive = false + end + end + + def grow + @height += Random.new.rand(1..10) end def add_apples + numApples = Random.new.rand(1..20) * @height + + numApples.times do + diameter = Random.new.rand(2.0..4.0) + apple = Apple.new(@treeAppleColor, diameter) + @apples << apple + end end def any_apples? + return @apples.count > 0 end def pick_an_apple! raise NoApplesError, "This tree has no apples" unless self.any_apples? + + apple = @apples.delete_at(rand(@apples.length)) + return apple end def dead? + return !@alive end end class Fruit - def initialize - has_seeds = true - end + attr_reader :has_seeds + def initialize + @has_seeds = true + end end -class Apple < - attr_reader #what should go here +class Apple < Fruit + attr_reader :color + attr_reader :diameter def initialize(color, diameter) + @color = color + @diameter = diameter end end @@ -61,7 +98,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.count.to_f puts "Year #{tree.age} Report" puts "Tree height: #{tree.height} feet" @@ -76,4 +113,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..8604379 100644 --- a/spec/tree_spec.rb +++ b/spec/tree_spec.rb @@ -1,14 +1,102 @@ require 'rspec' require 'tree' -describe 'Tree' do - it 'should be a Class' do - expect(described_class.is_a? 'Class').to be_true - end +describe Tree do + + it 'should be a Class' do + expect(described_class.is_a? Class).to be true + end + + it 'should be a newborn when planted' do + tree = Tree.new + + expect(tree.age).to eq 0 + end + + it 'should have zero apples when planted' do + tree = Tree.new + + expect(tree.any_apples?).to eq false + end + + it 'should be alive when planted' do + tree = Tree.new + + expect(tree.dead?).to eq false + end + + it 'should age a year' do + tree = Tree.new + tree.age! + + expect(tree.age).to eq 1 + end + + it 'should die after 50 years' do + tree = Tree.new + 50.times do tree.age! end + + expect(tree.dead?).to eq true + end + + it 'should add at least one apple each times it ages' do + tree = Tree.new + startApples = tree.apples.count + tree.age! + endApples = tree.apples.count + + expect(endApples).to be > startApples + end + + it 'should decrease by one apple when an apple is picked' do + tree = Tree.new + tree.age! + startApples = tree.apples.count + tree.pick_an_apple! + endApples = tree.apples.count + + expect(endApples).to eq (startApples-1) + end + + it 'should give an apple when one is picked' do + tree = Tree.new + tree.age! + apple = tree.pick_an_apple! + + expect(apple).to be_instance_of(Apple) + end + + it 'should grow a positive value each year' do + tree = Tree.new + startHeight = tree.height + tree.age! + endHeight = tree.height + + expect(endHeight).to be > startHeight + end end -describe 'Fruit' do +describe Fruit do + it 'should be a Class' do + expect(described_class.is_a?(Class)).to be true + end + + it 'should have seeds' do + fruit = Fruit.new + expect(fruit.has_seeds).to eq true + end end -describe 'Apple' do +describe Apple do + it 'should be a Class' do + expect(described_class.is_a?(Class)).to be true + end + + it 'should set the color and diameter properties' do + color = "blue" + diameter = 25.3 + apple = Apple.new(color, diameter) + expect(apple.color).to eq color + expect(apple.diameter).to eq diameter + end end