Skip to content

Instantly share code, notes, and snippets.

@nmbr73
Last active September 28, 2022 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmbr73/a19ea4ed44af7f4816e0d5fdbdb1948b to your computer and use it in GitHub Desktop.
Save nmbr73/a19ea4ed44af7f4816e0d5fdbdb1948b to your computer and use it in GitHub Desktop.
Day 13, Checkpoint 8 of #100DaysOfSwiftUI ... thanks @twostraws - I really enjoy the course so far!
/// Any kind of building on the estate market.
///
protocol Building {
/// Number of rooms the building has.
///
/// Note that the numebr of rooms can't change heavy construction work.
var rooms: Int { get }
/// The building's cost.
///
/// Cost may vary depending on the current market situation.
var cost: Int { get set }
/// Name of the estate agent selling the building.
///
/// The estate agent may change from time to time.
var seller: String { get set }
}
extension Building {
var salesSummary: String {
"This \(type(of: self)) has \(rooms) rooms and is sold by '\(seller)' for \(cost)."
}
func printSalesSummary() {
print (self.salesSummary)
}
}
struct House: Building
{
let rooms: Int
var cost: Int
var seller: String
let garage: Bool
// That errror is hard to spot - or do you see why the summary of our House instance
// says nothing about the garage?!?
func printSalesSunmary() {
print (self.salesSummary + (garage ? "" : " Please note, that there is no garage included."))
}
}
struct Office: Building
{
let rooms: Int
var cost: Int
var seller: String
var canteen: Bool
// If you have a typo in the function name, then you have a problem - this kind
// of defeats the purpose of what we got with the `override`for classes 😕
func printSalesSummary() {
print (self.salesSummary + (canteen ? " Canteen included." : ""))
}
}
let house = House(rooms: 3, cost: 250_000, seller: "Paul von nebenan", garage: false)
let office = Office(rooms: 20, cost: 600_000, seller: "Halsabschneider GmbH", canteen: true )
house.printSalesSummary()
office.printSalesSummary()
// Structs, classes, protocols, extensions, opaque return types - phew! Don't
// know yet if this is so much more consistent and consise compared to abstract
// base classes and multiple inheritance?!?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment