Last active
August 29, 2015 13:57
-
-
Save jdubnicek/9478475 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is how you define your own custom exception classes | |
require 'pry' | |
class NoOrangesError < StandardError | |
end | |
class OrangeTree | |
attr_accessor :age, :height, :harvest | |
def initialize | |
@age = 0 | |
@height = 0 | |
@harvest = [] | |
end | |
# Ages the tree one year | |
def age! | |
unless dead? | |
self.age += 1 | |
self.height += 3 unless height >= 12 | |
end | |
grow_oranges if age > 3 | |
end | |
def age=(age) | |
@age = age | |
end | |
# Returns +true+ if there are any oranges on the tree, +false+ otherwise | |
def any_oranges? | |
harvest.any? | |
end | |
def dead? | |
age > 25 | |
end | |
# Returns an Orange if there are any | |
# Raises a NoOrangesError otherwise | |
def pick_an_orange! | |
raise NoOrangesError, "This tree has no oranges" unless self.any_oranges? | |
harvest.pop | |
end | |
private | |
def grow_oranges | |
if !dead? | |
(age * 3).times { harvest.push(Orange.new) } | |
else dead? | |
"I'm sorry to inform you that your tree is dead." | |
end | |
end | |
end | |
class Orange | |
attr_reader :diameter | |
# Initializes a new Orange with diameter +diameter+ | |
def initialize(diameter = rand(2..10) ) | |
@diameter = diameter | |
end | |
# def +(integer) | |
# self.diameter + integer | |
# end | |
end | |
tree = OrangeTree.new | |
tree.age! until tree.any_oranges? | |
puts "Tree is #{tree.age} years old and #{tree.height} feet tall" | |
until tree.dead? | |
basket = [] | |
# It places the oranges in the basket | |
# IT PLACES THE ORANGES IN THE BASKET | |
while tree.any_oranges? | |
basket << tree.pick_an_orange! | |
end | |
avg_diameter = basket.map{|orange| orange.diameter}.inject(:+) / basket.length# It's up to you to calculate the average diameter for this harvest. | |
puts "Year #{tree.age} Report" | |
puts "Tree height: #{tree.height} feet" | |
puts "Harvest: #{basket.size} oranges with an average diameter of #{avg_diameter} inches" | |
puts "" | |
# Age the tree another year | |
tree.age! | |
end | |
puts "Alas, the tree, she is dead!" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rspec" | |
require_relative "orange_tree" | |
describe OrangeTree do | |
let(:orange_tree) { OrangeTree.new("0", "0")} | |
context "#initialize" do | |
it "creates an OrangeTree object" do | |
orange_tree.should_be_an_instance_of OrangeTree | |
end | |
end | |
context "#any_oranges?" do | |
it "calls #harvest.any?" do | |
orange_tree.harvest_any?.should eq "true if harvest_any?" | |
end | |
end | |
context "#dead?" do | |
it "returns the tree.age" do | |
tree.dead == true "if tree.age > 25" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment