Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shabbir Solution #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
46 changes: 39 additions & 7 deletions lib/tree.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
class NoApplesError < StandardError; end

class Tree
attr_#fill_in :height, :age, :apples, :alive
attr_accessor :height, :age, :apples, :alive

def initialize
def initialize (height =0, age =0, apples =[], alive =true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a user of Tree provide these values sometimes?

@height = height
@age = age
@apples = apples
@alive =true
end

# Ages the tree by a year, and increments the height of the tree by a random value
# Adds the apples to the tree when it matures( at age 10)
# Kills the tree at a certain age (i.e 100)
def age!
@age +=1
@height += rand(0..5)
if @age>=10
add_apples(rand(1..50))
end
if @age==100
@alive = false
@apples = []
end
end

def add_apples
#Adds the given amount of apples to the tree with a random color and diameter
def add_apples(count)
count.times{
@apples << Apple.new(['red','green'].sample,rand(5..15))
}
end

def any_apples?
@apples.length > 0
end

#Throws an exception if tree has no apples else removes an apple from the tree
def pick_an_apple!
raise NoApplesError, "This tree has no apples" unless self.any_apples?
@apples.pop
end


def dead?
not @alive
end
end

Expand All @@ -29,10 +54,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 +88,12 @@ def tree_data
diameter_sum += apple.diameter
end

avg_diameter = # It's up to you to calculate the average diameter for this harvest.
#calculate avg diameter of the apples in basket
if diameter_sum > 0
avg_diameter = diameter_sum / basket.length
else
avg_diameter = 0
end

puts "Year #{tree.age} Report"
puts "Tree height: #{tree.height} feet"
Expand All @@ -76,4 +108,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
38 changes: 38 additions & 0 deletions spec/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,48 @@
it 'should be a Class' do
expect(described_class.is_a? Class).to eq true
end

#Creating an instance of the tree class
let (:tree) {Tree.new}

it 'should be alive initially' do
expect(tree.dead?).to eq false
end

it 'should have no apples initially' do
expect(tree.any_apples?).to eq false
end

it 'should grow and age' do
tree.age!
expect(tree.age > 0).to true
end

it 'should add apples after certain age' do
tree.add_apples(10)
expect(tree.any_apples?).to eq true
end

it 'pick an apple if available else throw an exception' do
exepct(tree.pick_an_apple!.is_a? Apple).to eq true
end

it 'should die after a certian age'
tree.age = 100
expect(tree.dead?).to eq true
end

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

describe 'Apple' do
it 'should be a class' do
expect(described_class.is_a? Class).to eq true
end
it 'Should be a fruit' do
expect(Apple.superclass).to eq Fruit
end
end