Last active
April 4, 2017 13:35
-
-
Save flanger001/9e895ace6430aa5f2d0fcb8fff373df8 to your computer and use it in GitHub Desktop.
Eggs in a mug
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
# An object-oriented breakfast | |
class Mug(object): | |
def __init__(self, article): | |
self.article = article | |
print('You put a', self.article.get_name().lower(), 'in the mug') | |
def heat(self, heater, time): | |
heater.heat(self.article, time) | |
def get_article(self): | |
return self.article.get_name() | |
class Food(object): | |
def __init__(self, name): | |
self.name = name | |
self.temperature = 'cold' | |
self.seasonings = [] | |
def get_name(self): | |
return self.name | |
def set_temperature(self, temperature): | |
self.temperature = temperature | |
def get_temperature(self): | |
return self.temperature | |
def add_seasoning(self, seasoning): | |
self.seasonings.append(seasoning) | |
def get_seasonings(self): | |
return ', '.join(self.seasonings) | |
def get_status(self): | |
return self.get_name() + ' is seasoned with ' + self.get_seasonings() + ', is ' + self.get_temperature() + ', and is ready' | |
class Egg(Food): | |
def __init__(self): | |
Food.__init__(self, 'Egg') | |
self.protein = 10 | |
class Microwave(object): | |
def heat(item, time): | |
print('Heating for', time, 'seconds') | |
Microwave.adjust_temperature(item) | |
print('DING!') | |
print(item.get_name(), 'is now', item.get_temperature()) | |
print() | |
def adjust_temperature(item): | |
if item.get_temperature() == 'cold': | |
item.set_temperature('warm') | |
elif item.get_temperature() == 'warm': | |
item.set_temperature('hot') | |
elif item.get_temperature() == 'hot': | |
item.set_temperature('very hot') | |
egg = Egg() | |
mug = Mug(egg) | |
mug.heat(Microwave, 45) | |
for seasoning in ['salt', 'pepper', 'cheese']: | |
egg.add_seasoning(seasoning) | |
mug.heat(Microwave, 50) | |
print(egg.get_status()) |
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
# breakfast.rb | |
require 'forwardable' | |
class Food | |
attr_accessor :name, :temperature, :status, :seasonings | |
def initialize(name) | |
@name = name | |
@seasonings = [] | |
@temperature = 1 | |
@status = 'not ready' | |
end | |
def add_seasoning(seasoning) | |
seasonings << seasoning | |
end | |
def get_temperature | |
case @temperature | |
when 1..20 then 'cold' | |
when 21..40 then 'warm' | |
when 41..60 then 'hot' | |
when 61..80 then 'very hot' | |
when 81..100 then 'toasty' | |
when 101..Float::INFINITY then 'burned' | |
end | |
end | |
def report | |
"#{name}: \n".tap do |message| | |
message << "* seasonings: #{seasonings.join(', ')}\n" if seasonings.length > 0 | |
message << "* temperature: #{get_temperature}\n" | |
message << "* status: #{status}\n\n" | |
end | |
end | |
end | |
class Egg < Food | |
def initialize | |
super('egg') | |
end | |
def status | |
if temperature >= 21 | |
'ready' | |
else | |
'not ready' | |
end | |
end | |
end | |
class Bread < Food | |
def initialize | |
super('bread') | |
end | |
def temperature=(value) | |
@temperature = value | |
if value >= 41 | |
@name = 'toast' | |
end | |
end | |
def status | |
if temperature >= 41 | |
'ready' | |
else | |
'not ready' | |
end | |
end | |
end | |
class Bacon < Food | |
def initialize | |
super('bacon') | |
end | |
def add_seasoning(*) | |
puts "Do not season bacon, it is perfect as is.\n\n" | |
end | |
def status | |
if temperature >= 41 | |
'ready' | |
else | |
'not ready' | |
end | |
end | |
end | |
class Mug | |
extend Forwardable | |
attr_accessor :article | |
def_delegators :@article, :name, :temperature, :temperature=, :get_temperature | |
def initialize(article=nil) | |
@article = article | |
end | |
end | |
class Heater | |
def self.heat(obj, time) | |
print "Heating a #{obj.get_temperature} #{obj.name} for #{time} seconds " | |
yield if block_given? | |
puts "#{obj.name} is now #{obj.get_temperature}\n\n" | |
end | |
end | |
class Microwave < Heater | |
def self.heat(obj, time) | |
x = lambda do | |
puts "in the microwave..." | |
obj.temperature += time if time > 0 | |
puts 'DING!' | |
end | |
super(obj, time, &x) | |
end | |
end | |
class Toaster < Heater | |
def self.heat(obj, time) | |
super(obj, time) do | |
puts "in the toaster..." | |
obj.temperature *= time if time > 0 | |
puts 'POP!' | |
end | |
end | |
end | |
class FryingPan < Heater | |
def self.heat(obj, time) | |
super(obj, time) do | |
puts "on the frying pan..." | |
obj.temperature += time * 2 if time > 0 | |
puts "CRACKLE!" | |
end | |
end | |
end | |
egg = Egg.new | |
bread = Bread.new | |
bacon = Bacon.new | |
mug = Mug.new(egg) | |
Microwave.heat(mug, 30) | |
egg.add_seasoning('salt') | |
egg.add_seasoning('pepper') | |
egg.add_seasoning('cheese') | |
Microwave.heat(mug, 40) | |
Toaster.heat(bread, 85) | |
bread.add_seasoning('butter') | |
bacon.add_seasoning('salt') | |
FryingPan.heat(bacon, 42) | |
puts egg.report | |
puts bread.report | |
puts bacon.report |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment