Created
October 25, 2018 10:09
-
-
Save rikas/f71b07f842ba844f0a4c444bcd19b0c2 to your computer and use it in GitHub Desktop.
Advanced OOP lecture
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
# chef.rb | |
class Chef | |
attr_reader :name, :restaurant | |
def initialize(name, restaurant) | |
@name = name # String instance | |
@restaurant = restaurant # Restaurant instance | |
end | |
end |
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
# chef_test.rb | |
require_relative 'restaurant' | |
require_relative 'chef' | |
# gordon = Chef.new('Gordon Ramsay', hells_kitchen) | |
# puts "#{gordon.name} works at #{gordon.restaurant.name}" | |
hells_kitchen = Restaurant.new("Hell's Kitchen", 'London', 200, 'Gordon Ramsay') | |
# p hells_kitchen | |
puts "#{hells_kitchen.name}'s chef is #{hells_kitchen.chef.name}" | |
puts "#{hells_kitchen.name}'s chef is #{hells_kitchen.chef.restaurant.chef.restaurant.chef.name}" | |
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
# class_methods.rb | |
require_relative 'restaurant' | |
ramiro = Restaurant.new('Ramiro', 'Lisbon', 240) | |
# Restaurant#client_list - Restaurant instance method called client_list | |
# Restaurant.categories - Restaurant class method called categories | |
# THIS WON'T WORK | |
puts "The restaurant categories are: #{ramiro.categories}" | |
puts "The restaurant categories are: #{Restaurant.categories}" |
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
# fancy_restaurant.rb | |
require_relative 'restaurant' | |
class FancyRestaurant < Restaurant | |
attr_reader :stars | |
def initialize(name, city, capacity, stars) | |
# super # same as super(name, city, capacity, stars) | |
super(name, city, capacity) | |
@stars = stars | |
end | |
# overriding Restaurant#client_list | |
def client_list | |
"⚠️ YOU CAN'T SEE THE CLIENT LIST! ⚠️" | |
end | |
end |
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
# fast_food_restaurant.rb | |
require_relative 'restaurant' | |
class FastFoodRestaurant < Restaurant | |
attr_reader :prep_time | |
def initialize(name, city, capacity, prep_time) | |
# super # same as super(name, city, capacity, stars) | |
super(name, city, capacity) | |
@prep_time = prep_time | |
end | |
# overriding the super method | |
def open? | |
# (Time.now.hour >= 18 && Time.now.hour <= 22) || (Time.now.hour > 22 && Time.now.hour <= 1) | |
super && Time.now.hour > 22 && Time.now.hour <= 1 | |
end | |
end |
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
# inheritance.rb | |
require_relative 'fast_food_restaurant' | |
require_relative 'fancy_restaurant' | |
mcdonalds = FastFoodRestaurant.new('McDonalds', 'Lisbon', 200, 5) | |
sud = FancyRestaurant.new('Sud', 'Lisbon', 1000, 3) | |
puts "#{sud.name}#{'⭐️' * sud.stars} is in #{sud.city}" | |
puts "#{mcdonalds.name} is in #{mcdonalds.city} and it takes #{mcdonalds.prep_time} minutes to cook your meal" | |
puts "#{sud.name} is open? -> #{sud.open?}" | |
puts "#{mcdonalds.name} is open? -> #{mcdonalds.open?}" | |
puts "#{mcdonalds.name} is closed? -> #{mcdonalds.closed?}" | |
mcdonalds.book('Chris') | |
mcdonalds.book('Vasco') | |
sud.book('Benjamin') | |
sud.book('Buzzfeed') | |
puts '-'*20 | |
puts "Clients for #{sud.name}" | |
puts sud.client_list | |
puts | |
puts "Clients for #{mcdonalds.name}" | |
puts mcdonalds.client_list | |
puts '-'*20 |
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
# restaurant.rb | |
require_relative 'chef' | |
class Restaurant | |
attr_reader :name, :city, :chef #, :capacity | |
attr_accessor :capacity | |
def initialize(name, city, capacity, chef_name = 'Manuel') | |
@name = name | |
@city = city | |
@capacity = capacity | |
@clients = [] | |
@chef = Chef.new(chef_name, self) | |
end | |
def self.categories | |
%w[portuguese thai italian seafood] | |
end | |
def open? | |
Time.now.hour >= 18 && Time.now.hour <= 22 | |
end | |
def closed? | |
!open? # same as !self.open? | |
end | |
def book(client_name) | |
@clients << client_name | |
end | |
def client_list | |
output = '' | |
@clients.each do |client_name| | |
output << "▶️ #{client_name}\n" | |
end | |
output | |
end | |
# explicit readers / getters | |
# def name | |
# @name | |
# end | |
# explicit writer / setter | |
# def capacity=(new_capacity) | |
# @capacity = new_capacity.to_i | |
# end | |
end |
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
# restaurant_test.rb | |
require_relative 'restaurant' | |
# creating a new instance of the class Restaurant | |
ramiro = Restaurant.new('Ramiro', 'Lisbon', 240) | |
puts "#{ramiro.name} is in #{ramiro.city}" | |
puts "#{ramiro.name} can host #{ramiro.capacity} people" | |
# construction work | |
ramiro.capacity = 800 | |
puts "#{ramiro.name} can now host #{ramiro.capacity} people" | |
puts "#{ramiro.name} is now #{ramiro.open? ? 'open' : 'closed'}" | |
ramiro.book('Ricardo') | |
ramiro.book('Manuel') | |
ramiro.book('Inês') | |
# puts ramiro | |
# p ramiro |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment