Skip to content

Instantly share code, notes, and snippets.

@makersacademy
Created February 26, 2013 12:51
Show Gist options
  • Save makersacademy/5038209 to your computer and use it in GitHub Desktop.
Save makersacademy/5038209 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
properties = []
class Property
def initialize(number, postcode)
@number, @postcode = number, postcode
@vacant = false
end
def vacant?
@vacant
end
def vacant=(val)
@vacant = val
end
end
class CommercialProperty < Property
BUSINESS_RATE_PER_FOOT = 10
def initialize(number, postcode, square_footage)
super(number, postcode)
@square_footage = square_footage
end
def business_rates
@square_footage * BUSINESS_RATE_PER_FOOT
end
def tax
business_rates
end
end
class ResidentialProperty < Property
BAND_RATES = {
:a => 100,
:b => 200,
:c => 300
}
def initialize(number, postcode, no_of_people, band)
super(number, postcode)
self.vacant = no_of_people == 0 # self.vacant=(no_of_people == 0)
@no_of_people, @band = no_of_people, band
end
def council_tax
@no_of_people * BAND_RATES[@band]
end
def tax
return 0 if vacant?
council_tax
end
end
epworth_house = CommercialProperty.new(25, "EC1Y 1AA", 50000)
flat = ResidentialProperty.new(1, "EC1Y 1AA", 0, :b)
# flat.no_of_people = 2
# flat.vacant = true
properties << epworth_house # properties.push(epworth_house)
properties << flat
# calculates cumulative tax from all properties
overall_tax = properties.inject(0) {|memo, property| memo += property.tax; memo}
puts "We have #{properties.length} property, paying #{overall_tax} in taxes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment