UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Checkpoint 8: protocols

Forums > 100 Days of SwiftUI

@Moghi  

Here comes my solution, I am not quite happpy about printSaleSummary() I feel like I could implement it once. I could, if I was to add another property buildingType, but it feels redundant since the information is already there in the struct that complies to the Building protocol.

What do you think?

my code:

protocol Building {
    var roomsCount: Int { get }
    var cost: Int { get set }
    var estateAgent: String { get set }
}

//extension Building {
//    func printSaleSummary() {
//        print("\(buildingType): ")  <-- maybe access struct something like type(self)?
//        print("  - Number of Rooms: \(roomsCount)")
//        print("  - Sold at \(cost)$")
//        print("  - Agent: \(estateAgent)")
//    }
//}

struct House: Building {
    let roomsCount: Int
    var cost: Int
    var estateAgent: String
    func printSaleSummary() {
        print("House:")
        print("  - Number of Rooms: \(roomsCount)")
        print("  - Sold at \(cost)$")
        print("  - Agent: \(estateAgent)\n")
    }
}

struct Office: Building {
    let roomsCount: Int
    var cost: Int
    var estateAgent: String
    func printSaleSummary() {
        print("Office Building:")
        print("  - Number of Rooms: \(roomsCount)")
        print("  - Sold at \(cost)$")
        print("  - Agent: \(estateAgent)\n")
    }
}

let myHouse = House(roomsCount: 2, cost: 200000, estateAgent: "John Smith")
let myOffice = Office(roomsCount: 3, cost: 300000, estateAgent: "Hanna Montana")

myHouse.printSaleSummary()
myOffice.printSaleSummary()

2      

For this Methode you could use a multi line string

    func printSaleSummary() {
        print("House:")
        print("  - Number of Rooms: \(roomsCount)")
        print("  - Sold at \(cost)$")
        print("  - Agent: \(estateAgent)\n")

it would then look something like this i think

func printSaleSummary() {
            print("""
            House:
            - Number of Rooms: \(roomsCount)
            - Sold at \(cost)$
            - Agent: \(estateAgent)\n
            """)
    }

you can indeed set printSaleSummery() as an extension and only use it once. here is my attempt

import Cocoa

// functions that can be used on all Building types
extension Building{
    func getSalesSummery() {
        print ("""
        The property with \(rooms) rooms
        has been sold for \(price)€
        by \(estateAgent)\n
        """)
    }

//optional method to haggle the price this was not part of the assignment    
    mutating func hagglePrice() {
        price -= 10000
    }
}
// the main Protocol for Buildings
protocol Building {
    var rooms :Int {get set}
    var price : Int {get set}
    var estateAgent : String {get}
}

// Structures build from Building protocol
struct House : Building {
    var rooms: Int
    var price: Int
    let estateAgent: String

 //optional method not part of the assignment: it is to build an extension to the house
    mutating func buildExtension(addRooms : Int ){
        rooms += addRooms
    }
}

struct Appartment : Building {
    var rooms: Int
    var price: Int
    let estateAgent: String
}

// test the struct
var myHome = House(rooms: 4, price: 500_000, estateAgent: "Tom Holland")

// let valu because you can not build an extension and the estate agent will not haggle for the price
let myAppartment = Appartment(rooms: 2, price: 350_000, estateAgent: "Taylor Swift")
myHome.buildExtension(addRooms: 2)
myHome.hagglePrice()
myHome.getSalesSummery()
myAppartment.getSalesSummery()

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.