Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
class NoApplesError < StandardError; end

class AppleTree
attr_#fill_in :height, :age, :apples, :alive

def initialize
class Tree
#fill_in :height, :age, :apples, :alive
attr_reader :apples, :height, :age, :alive

def initialize
@apples = []
@height = 0
@age = 0
@alive = true
end

def age!
@age += 1

# Randomly decide if a tree lives or dies after it reaches 10 years old.
# The older it gets, the more likely it is to die.
# A tree cannot live longer than 50 years.
if (rand(10..50) < @age) then
@alive = false
elsif (@alive)
if (@age < 5) then
# Tree grows a random height every year
@height += rand(1..10)
else
# Produce a random number for apples
rand(5).times { add_apples }
end
end
end

def add_apples
@apples.push Apple.new 'red', rand(2..6)
end

def any_apples?
@apples.count > 0
end

def pick_an_apple!
raise NoApplesError, "This tree has no apples" unless self.any_apples?
@apples.pop
end

def dead?
!@alive
end
end

Expand All @@ -29,17 +54,21 @@ def initialize
end
end

class Apple <
attr_reader #what should go here

class Apple < Fruit
#what should go here
attr_reader :diameter
attr_reader :color

def initialize(color, diameter)
@color = color
@diameter = diameter
end
end

#THERES ONLY ONE THING YOU NEED TO EDIT BELOW THIS LINE
# avg_diameter (line 58) will raise an error.
# it should calculate the diameter of the apples in the basket

def tree_data
tree = Tree.new

Expand All @@ -60,8 +89,9 @@ def tree_data
basket.each do |apple|
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 = basket.count > 0 ? diameter_sum / basket.count : 0

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand Down
136 changes: 131 additions & 5 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,140 @@
require 'rspec'
require 'tree'

describe 'Tree' do
it 'should be a Class' do
expect(described_class.is_a? 'Class').to be_true
shared_examples 'sanity check' do |tree_class|
it 'is a class' do
expect(tree_class.is_a? Class).to eq true
end
end

describe 'Fruit' do
describe Tree do
before :each do
@tree = Tree.new
end
it 'should be a class' do
expect(described_class).to eq Tree
end

it 'should have no apples initially' do
expect(@tree.apples.count).to eq 0
end

it 'should have a height of 0 initially' do
expect(@tree.height).to eq 0
end

it 'should have age of 0 initially' do
expect(@tree.age).to eq 0
end

it 'should initially be alive' do
expect(@tree.alive).to eq true
end

it 'should increase age by 1 when age is called' do
@tree.age!
expect(@tree.age).to eq 1
end

it 'should grow by atleast 1 unit every every time it ages and is less than 4 years old' do
@tree.age!
expect(@tree.height).to be > 0
end

it 'should not grow after 4 years' do
for i in 0..5
@tree.age!
end
height_before = @tree.height
@tree.age!
expect(@tree.height).to eq height_before
end

it 'should start producing apples when its 5 years old' do
for i in 0..6
@tree.age!
end
expect(@tree.apples.count).to be > 0
end

it 'should not produce apples if its younger than 5 years old' do
@tree.age!
expect(@tree.apples.count).to eq 0
end

it 'should have no apples if younger than 5 years old' do
@tree.age!
expect(@tree.any_apples?).to eq false
end

it 'should have apples if it is older than 5 years old' do
for i in 0..5
@tree.age!
end
expect(@tree.any_apples?).to eq true
end
it 'should not be alive longer than 50 years' do
# Since a tree can't live past 50 years, this will ensure its dead.
for i in 0..51
@tree.age!
end
expect(@tree.alive).to eq false
end

it 'should not produce apples if the tree is dead' do
# Since a tree can't live past 50 years, this will ensure its dead.
for i in 0..51
@tree.age!
end
number_of_apples = @tree.apples.count
@tree.age!
expect(@tree.apples.count).to eq number_of_apples
end

it 'should be able to pick an apple' do
basket = []
while @tree.apples.count == 0 do
@tree.age!
end
basket << @tree.pick_an_apple!
expect(basket.count).to eq 1
end

it 'should decrease the amount of apples on the tree when an apple is picked' do
basket = []
while @tree.apples.count == 0 do
@tree.age!
end
num_apples_before = @tree.apples.count
basket << @tree.pick_an_apple!
expect(@tree.apples.count).to be < num_apples_before
end
end

describe Fruit do
it 'should be a class' do
expect(described_class).to eq Fruit
end
end

describe 'Apple' do
describe Apple do
before :each do
@apple = Apple.new 'red', 3
end

it 'should be a class' do
expect(described_class).to eq Apple
end

it 'should inherit from Fruit' do
expect(described_class).to be < Fruit
end

it 'should have a color' do
expect(@apple.color).to eq 'red'
end

it 'should have a diameter' do
expect(@apple.diameter).to eq 3
end
end