Created
April 18, 2019 11:03
-
-
Save rikas/f559b3374db63c8d2ad978ac9676a87c to your computer and use it in GitHub Desktop.
OOP Advanced — Batch 243
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
require_relative 'chef' | |
require_relative 'fancy_restaurant' | |
belcanto = | |
FancyRestaurant.new('Belcanto', 30, 'Portuguese', 5, 'Gordon Ramsey') | |
puts "The chef of #{belcanto.name} is #{belcanto.chef.name}" | |
p belcanto |
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' | |
# class method .categories | |
puts "Available categories: #{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' | |
require_relative 'chef' | |
class FancyRestaurant < Restaurant | |
attr_reader :stars, :chef | |
def initialize(name, capacity, category, stars, chef_name) | |
super(name, nil, capacity, category) | |
# instance variables (ivars) | |
@chef = Chef.new(chef_name, self) # Chef instance | |
@stars = stars | |
end | |
# overriding the superclass instance method | |
def name | |
"~ #{@name} ~" | |
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 | |
def initialize(name, city, capacity, category, prep_time) | |
super(name, city, capacity, category) | |
# instance variables (ivars) | |
@prep_time = prep_time | |
end | |
# overriding the Superclass isntance method | |
def open? | |
hour = Time.now.hour | |
super || hour > 6 && hour <= 10 | |
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_test.rb | |
require_relative 'fast_food_restaurant' | |
require_relative 'fancy_restaurant' | |
belcanto = | |
FancyRestaurant.new('Belcanto', 30, 'Portuguese', 5, 'Gordon') | |
puts "#{belcanto.name} (#{'⭐️' * belcanto.stars})" | |
puts "Is it open? -> #{belcanto.open? ? 'Yes 😎' : 'No 🙅🏻♂️'}" | |
belcanto.book_a_table('Ricardo') | |
p belcanto | |
kfc = | |
FastFoodRestaurant.new('KFC Lisbon', 'Lisbon', 200, 'Chicken', 3) | |
puts "#{kfc.name} is in #{kfc.city}" | |
puts "Is it open? -> #{kfc.open? ? 'Yes 😎' : 'No 🙅🏻♂️'}" | |
kfc.book_a_table('Marisa') | |
p kfc |
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 | |
class Restaurant | |
attr_reader :name, :city, :capacity | |
attr_writer :capacity | |
def initialize(name, city, capacity, category) | |
# instance variables (ivars) | |
@name = name | |
@city = city | |
@capacity = capacity | |
if self.class.categories.include?(category) | |
@category = category | |
end | |
@reservations = [] | |
end | |
def self.categories | |
%w[Portuguese Burguers Indian Japanese] | |
end | |
def open? | |
hour = Time.now.hour | |
hour > 11 && hour < 23 | |
end | |
def closed? | |
!open? | |
end | |
def book_a_table(client_name) | |
return unless available_space? | |
@reservations << client_name | |
end | |
private | |
def available_space? | |
@reservations.size < @capacity | |
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' | |
mcdonalds = | |
Restaurant.new('McDonalds', 'Lisbon', 300, 'Fast Food') | |
# using implicit to_s from Restaurant | |
puts mcdonalds | |
puts "#{mcdonalds.name} is in #{mcdonalds.city}" | |
puts "#{mcdonalds.name} can host #{mcdonalds.capacity}" | |
# lets add some seats | |
mcdonalds.capacity = 400 | |
puts "#{mcdonalds.name} can host #{mcdonalds.capacity}" | |
puts "Is it open? -> #{mcdonalds.open? ? 'Yes 😎' : 'No 🙅🏻♂️'}" | |
mcdonalds.book_a_table('Marisa') | |
mcdonalds.book_a_table('Mark') | |
p mcdonalds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment