Hi all, welcome to another coding blog!
Today we are going to talk about things you can define, like a car, a dog, a person,.. All these “things” can be called objects. Object-oriented programming(OOP) is a very important programming model and one of the harder parts to get your head around.
Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic.
I won’t explain everything into to much detail as I am very tired from coding 8hours+ the last four days. But here are the basics of today.
Want more information about classes and objects? Click here
Example of a Class, and the creation of an object of that class. And a more complex Class that uses the initialize method. Including the creation of an object with passing the required parameters.
### Create a class Car### class Car end ### Create an object of the Car class ### car = Car.new --------------------------------- ### Create a class Car which needs two parameters, defined in the initialize method. ### ### The initialize method gets called whenever an object is created. ### class Car def initialize(make, wheels) @make= make @no_of_wheels= wheels end end ### Create an object of the Car class with two parameters. ### car = Car.new("Porsche",4)
For the most part of the day we had one big exercise, namely creating a program for a pizzeria. Every step of the exercise we got less and less explanation in order to challenge us to use everything we learned. And I can tell you the steps got harder and harder to complete. There were quite a few moments I really didn’t see a solution and had to redo parts and reread chapters of the reader. Below you can see my final version of the pizzeria and all the different files needed to make the program.
Click on the different tabs to see to different files:
require "./waiter" require "./menu" require "./kitchen" menu = Menu.new kitchen = Kitchen.new waiter = Waiter.new(menu, kitchen) waiter.greet_guest while(waiter.still_serving) do waiter.serve_guest waiter.order_food(gets.chomp.to_i) end
require "./check" class Waiter def initialize(menu, kitchen) @menu = menu @kitchen = kitchen @check = Check.new @serving = true end def greet_guest puts "Good day. Welcome to our lovely restaurant." end # run method take_order and pass user input as the order_number argument def serve_guest puts "How can I be of service?" puts "1. Would you like to order a pizza?" puts "2. Would you like to leave?" take_order(gets.chomp.to_i) end def take_order(order_number) case order_number when 1 puts "Let me get the menu" list_menu when 2 @check.calculate_check puts "Thank you for you visit" @serving = false else puts "I really don't understand" end end def list_menu @menu.contents.each_with_index do |dish, index| puts "#{index}. #{dish.name} \t € #{dish.price}" end end def order_food(choice) dish = @menu.contents[choice] if @kitchen.order(dish) @check.add(dish) puts "Dish is on its way" else puts "Sorry this dish is not available" end end def still_serving @serving end end
class Menu require "./dish" require "./ingredient" def initialize @menu = [] @menu << Dish.new("Margherita", [ Ingredient.new(Ingredient::TOMATO, 3), Ingredient.new(Ingredient::DOUGH, 0.25), Ingredient.new(Ingredient::MOZZARELLA, 0.2) ], 9) @menu << Dish.new("Napoletana", [ Ingredient.new(Ingredient::TOMATO, 3), Ingredient.new(Ingredient::DOUGH, 0.25), Ingredient.new(Ingredient::MOZZARELLA, 0.2), Ingredient.new(Ingredient::ANCHOVIES, 0.05) ], 11) @menu << Dish.new("Peperoni",[ Ingredient.new(Ingredient::TOMATO, 3), Ingredient.new(Ingredient::DOUGH, 0.25), Ingredient.new(Ingredient::MOZZARELLA, 0.2), Ingredient.new(Ingredient::PEPERONI, 0.1) ], 12) end def contents @menu end end
class Dish def initialize(dish_name, ingredients, price) @name = dish_name @ingredients = ingredients @price = price end def name @name end def ingredients @ingredients end def price @price end end
require './storage' class Kitchen def initialize @storage = Storage.new end def order(dish) puts "KITCHEN: Order received for #{dish.name}" puts "I'm gonna need some:" dish.ingredients.each do |ingredient| puts "#{ingredient.amount} _ #{ingredient.name}" end @storage.fetch(dish.ingredients) end end
class Ingredient TOMATO = "Tomato" DOUGH = "Dough" MOZZARELLA = "Mozzarella" ANCHOVIES = "Anchovies" PEPERONI = "Peperoni" def initialize(name,amount) @name = name @amount = amount end def name @name end def amount @amount end def use(amount) @amount -= amount end end
class Storage def initialize @items = [ Ingredient.new(Ingredient::TOMATO, 8), Ingredient.new(Ingredient::DOUGH, 2), Ingredient.new(Ingredient::MOZZARELLA, 1), Ingredient.new(Ingredient::PEPERONI, 0.3) ] end def fetch(ingredients) ingredients.each do |ingredient| item = @items.detect{|item| item.name == ingredient.name} if item item.use ingredient.amount else return false end end end end
class Check def initialize @items = [] end def add(item) @items << item end def calculate_check puts "Your check" puts "-" * 20 sum = 0 @items.each do |item| p "#{item.name} € #{item.price}" sum += item.price end puts "-" * 20 puts "Total price: € #{sum}" puts "-" * 20 end end