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
34 changes: 29 additions & 5 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
class NoApplesError < StandardError; end

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

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

def age!
if @age < 100
@age += 1
@height += 1
else
@alive = false
end

if @age >= 5
add_apples
else
# too young for apples
end
end

def add_apples
rand(100).times do
@apples.push(Apple.new('red', rand(2..4)))
end
end

def any_apples?
[email protected]?
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,10 +51,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

Expand Down Expand Up @@ -61,7 +85,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

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand Down
56 changes: 55 additions & 1 deletion spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,66 @@

describe 'Tree' do
it 'should be a Class' do
expect(described_class.is_a? 'Class').to be_true
tree = Tree.new
expect(tree.is_a? Tree).to be true
end

it 'should start with correct values' do
tree = Tree.new
expect(tree.age).to eq 1
expect(tree.height).to eq 1
expect(tree.apples).to eq []
expect(tree.alive).to be true
end

it 'should get older' do
tree = Tree.new
tree.age!
expect(tree.age).to eq 2
end

it 'should begin growing apples on year 5' do
tree = Tree.new
tree.age!
expect(tree.any_apples?).to be false
3.times { tree.age! }
expect(tree.any_apples?).to be true
end

it 'should be able to pick apples' do
tree = Tree.new
tree.add_apples
current_count = tree.apples.count
tree.pick_an_apple!
expect(tree.apples.count).to eq (current_count - 1)
end

it 'should grow 1 foot per year' do
tree = Tree.new
tree.age!
expect(tree.height).to eq 2
3.times { tree.age! }
expect(tree.height).to eq 5
end

it 'should die at age 100' do
tree = Tree.new
101.times { tree.age! }
expect(tree.dead?).to be true
end
end

describe 'Fruit' do
it 'should be a Class' do
fruit = Fruit.new
expect(fruit.is_a? Fruit).to be true
end
end

describe 'Apple' do
it 'should have color and diameter' do
apple = Apple.new('red', 3)
expect(apple.color).to eq 'red'
expect(apple.diameter).to eq 3
end
end