Last active
January 21, 2021 11:06
-
-
Save Oceantidote/53a658d674029dc6afc436a692aa1c12 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
require_relative "restaurant" | |
class FastfoodRestaurant < Restaurant | |
attr_reader :prep_time | |
def initialize(name, city, capacity, category, prep_time) | |
super(name, city, capacity, category) | |
@prep_time = prep_time | |
end | |
end | |
kfc = GastroRestaurant.new("kfc", "London", 30, "Fancy", 5) | |
p kfc.prep_time |
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" | |
require_relative "waiter" | |
class GastroRestaurant < Restaurant | |
attr_reader :stars | |
def initialize(name, city, capacity, category, stars, waiter_name) | |
super(name, city, capacity, category) | |
@waiter = Waiter.new(waiter_name, self) | |
@stars = stars | |
end | |
def print_clients | |
puts "You cannot access this list" | |
end | |
def open? | |
puts super | |
puts "but book quick as spots are running out" | |
end | |
def waiter | |
@waiter.name | |
end | |
def more_info | |
puts "#{name} is in #{city}, it has #{stars} stars" | |
# using the attr_readers (really just instance methods) to return information | |
# about the given instance (the restaurant we have just called more info on) | |
end | |
def set_name_and_city(name, city) | |
# example of using self to acces the instance on which we called the instance method | |
# unlikely you will have to use this | |
# here self returns the instance upon which we have just called set_name_and_city | |
self.name = name | |
self.city = city | |
# @name = name | |
# @city = city | |
end | |
end | |
# dishoom = Restaurant.new("Dishoom", "London", 30, "Fancy") | |
# dishoom.reserve("Hillary") | |
# dishoom.reserve("Joe") | |
# dishoom.print_clients | |
# gavroche = GastroRestaurant.new("gavroche", "London", 30, "Fancy", 5, "henry") | |
# gavroche.reserve("Hillary") | |
# gavroche.reserve("Joe") | |
# gavroche.print_clients | |
# p gavroche.open? | |
# p gavroche.waiter | |
# gavroche.more_info | |
# gavroche.set_name_and_city("McD's", "Oxford") | |
# p gavroche |
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 Restaurant | |
attr_accessor :name, :capacity, :city | |
attr_reader :clients | |
def initialize(name, city, capacity, category) | |
@name = name | |
@city = city | |
@capacity = capacity | |
@category = category | |
@clients = [] | |
end | |
def self.categories | |
# using self to define a class method (a method we call on the class) | |
# i.e. Restaurant.new, Restaurant.categories | |
["french", 'fastfood', 'fine dining'] | |
end | |
def self.price_for(category) | |
case category | |
when 'french' | |
20 | |
when 'fastfood' | |
5 | |
else | |
30 | |
end | |
end | |
# showing that you can have a class method and instance method of | |
# the same name and both will work independently | |
def price_for(category) | |
"none of your business" | |
end | |
def reserve(name) | |
@clients << name | |
end | |
def open? | |
Time.now.hour >= 16 && Time.now.hour < 22 | |
end | |
def print_clients | |
puts "Here are this evening's reservations" | |
@clients.each do |client| | |
puts "- #{client}" | |
end | |
end | |
end | |
# Using class methods | |
# Call categories method to return all the categories | |
# puts Restaurant.categories | |
# returns ["french", 'fastfood', 'fine dining'] | |
# Find out price for a given category (french) | |
# puts Restaurant.price_for('french') | |
# dishoom = Restaurant.new("Dishoom", "London", 30, "Fancy") | |
# p dishoom.price_for('french') | |
# p Restaurant.categories | |
# dishoom = Restaurant.new("Dishoom", "London", 30, "Fancy") | |
# # p dishoom.name | |
# # dishoom.name = "Kfc" | |
# # p dishoom.name | |
# # p dishoom.capacity | |
# # tony makes a booking | |
# dishoom.reserve("Tony") | |
# # see all of the bookings | |
# p dishoom.clients | |
# # see if the restaurant is open now | |
# p "#{dishoom.name} in now #{dishoom.open? ? 'open' : 'closed'}" | |
# examples of some Class methods you have already used from external classes | |
# require 'json' | |
# require 'nokgiri' | |
# JSON.parse('{}') | |
# Nokogiri::HTML:Document.parse('<h1>hello</h1>') |
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 Waiter | |
attr_reader :name | |
def initialize(name, restaurant) | |
@name = name | |
@restaurant = restaurant | |
end | |
def upcase | |
@name.upcase | |
end | |
def upcase! | |
@name = @name.upcase | |
end | |
end | |
bob = Waiter.new("bob", Restaurant.new("dasd","hg", 30, "hdga")) | |
# non-destructive use of the upcase instance method does not permanently upcase bob's name | |
p bob.upcase | |
p bob.name | |
# destructive upcase! does permanently upcase his name | |
p bob.upcase! | |
p bob.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment