Created
October 17, 2019 09:27
-
-
Save arthur-littm/d6bc333767b939b39b235c5f12e9f434 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
class Chef | |
attr_reader :name, :restaurant | |
def initialize(name, restaurant) | |
@name = name # String | |
@restaurant = restaurant # Instance of Restaurant (not a STRING !!!) | |
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 "restaurant" | |
class FastFoodRestaurant < Restaurant | |
# STATE (instance variables / data) | |
def initialize(name, city, capacity, prep_time) | |
super(name, city, capacity) | |
@prep_time = prep_time | |
end | |
def open? | |
# call the Restaurant#open? method | |
return Time.now.hour >= 9 && Time.now.hour <= 14 || super() | |
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 "restaurant" | |
class GastroRestaurant < Restaurant | |
# STATE (instance variables / data) | |
def initialize(name, city, capacity, stars) | |
@name = name | |
@city = city | |
@capacity = capacity | |
@stars = stars | |
@clients = [] | |
end | |
def print_clients | |
puts "Welcome dear guests" | |
super() | |
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" | |
class Restaurant | |
# Encapsulation | |
attr_reader :name, :city, :capacity, :chef | |
attr_writer :capacity | |
# STATE (instance variables / data) | |
def initialize(name, city, capacity, chef_name) | |
@name = name | |
@city = city | |
@capacity = capacity | |
@chef = Chef.new(chef_name, self) # Instance of Chef | |
@clients = [] | |
end | |
# BEHAVIOR | |
def open? | |
p self | |
return Time.now.hour >= 18 && Time.now.hour <= 23 | |
end | |
def add_reservation(client_name) | |
@clients << client_name | |
end | |
def print_clients | |
@clients.each do |client_name| | |
puts "- #{client_name}" | |
end | |
end | |
# you can call Restaurant.categories | |
def self.categories | |
return ["burgers", "sushi", "lebanese", "italian", "vegan"] | |
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 "restaurant" | |
leon = Restaurant.new("Leon", "London", 50) | |
# READ / WRITE ABOUT STATE OF INSTANCE | |
puts "#{leon.name} is in #{leon.city}" | |
puts "#{leon.name} has a capacity of #{leon.capacity} guests" | |
puts "Construction happening 👷♂️" | |
leon.capacity = 60 | |
puts "#{leon.name} has now a capacity of #{leon.capacity} guests" | |
# APPLY INSTANCE METHODS TO AN INSTANCE | |
puts "#{leon.name} is now #{leon.open? ? "open" : "closed"}" | |
leon.add_reservation("phelim") | |
leon.add_reservation("alice") | |
leon.add_reservation("arthur") | |
p leon |
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 "restaurant" | |
leon = Restaurant.new("Leon", "London", 50, "Jamie Oliver") | |
# print all the categories of restaurants | |
# p Restaurant.categories | |
p leon.open? | |
# the chef of XX is XX | |
# puts "The chef of #{leon.name} is #{leon.chef.name}" | |
# class User | |
# def initialize(name) | |
# @name = name | |
# @friends = [] | |
# end | |
# def add_friends(friend) | |
# @friends << friend | |
# end | |
# end | |
# phelim = User.new("phelim") | |
# arthur = User.new("arthur") | |
# phelim.add_friends(arthur) | |
# p phelim | |
# p arthur |
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 "fast_food_restaurant" | |
require_relative "gastro_restaurant" | |
patty = FastFoodRestaurant.new("Patty & Bun", "London", 30, 10) | |
p patty.open? | |
ritz = GastroRestaurant.new("The Ritz", "London", 120, 4) | |
p ritz.open? | |
ritz.add_reservation("Ben") | |
ritz.add_reservation("Katherine") | |
ritz.add_reservation("Alex") | |
ritz.print_clients | |
puts "--------------" | |
patty.add_reservation("Alice") | |
patty.add_reservation("Arthur") | |
patty.add_reservation("Lucien") | |
patty.print_clients | |
# p ritz | |
# class Parent | |
# end | |
# class Child < Parent | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment