Created
April 23, 2023 19:27
-
-
Save dokun1/45520618b78f120f1f6d39fa1839a69e 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
import Foundation | |
struct Ingredient { | |
var name: String | |
var amount: Double | |
} | |
protocol Cookable { | |
func cook() | |
} | |
protocol Edible { | |
func eat() | |
} | |
struct Pizza: Cookable, Edible { | |
func cook() { | |
print("do something to combine your ingredients") | |
} | |
func eat() { | |
print("Make sure you let me cool down first!") | |
} | |
var bread: Ingredient | |
var sauce: Ingredient | |
var cheese: Ingredient | |
} | |
struct Lasagna: Cookable { | |
func cook() { | |
print("I am using my \(onions)") | |
} | |
var onions: Ingredient | |
var cheese: Ingredient | |
var pasta: Ingredient | |
var sauce: Ingredient | |
} | |
let onions = Ingredient(name: "Onions", amount: 2) | |
let tomatoSauce = Ingredient(name: "Tomato Sauce", amount: 1) | |
let cheese = Ingredient(name: "Mozzarella Cheese", amount: 4) | |
let pastaSheets = Ingredient(name: "Pasta Sheets", amount: 32) | |
struct PicnicBasket { | |
var ingredients: [Ingredient] | |
func getIngredient(named name: String) -> Ingredient? { | |
ingredients.filter { $0.name == name }.first | |
} | |
var lasagna: Lasagna? { | |
if let onions = getIngredient(named: "Onions"), | |
let cheese = getIngredient(named: "Mozzarella Cheese"), | |
let pasta = getIngredient(named: "Pasta Sheets"), | |
let sauce = getIngredient(named: "Tomato Sauce") { | |
return Lasagna( | |
onions: onions, | |
cheese: cheese, | |
pasta: pasta, | |
sauce: sauce | |
) | |
} else { | |
return nil | |
} | |
} | |
} | |
let myBasket = PicnicBasket(ingredients: [onions, tomatoSauce, cheese, pastaSheets, onions]) | |
myBasket.getIngredient(named: "Tomato Sauce") | |
print(myBasket.lasagna) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment