NEW: Learn to build the incredible iOS 15 Weather app today! >>

SOLVED: Checkpoint 8 | Is this complete?

Forums > 100 Days of SwiftUI

Hello this is my solution to checkpoint 8, but I am not sure if I covered everything and I don´t have HWS+. Is there something I misunterstood or else? Sometimes I have problems to understand the task (I am German btw).

The extension was just a experiment.

protocol Building {
    var rooms: Int { get }
    var price: Int { get }
    var agent: String { get }
    func salesSummary()
}

struct House: Building {
    var rooms: Int
    var price: Int
    var agent: String

    func salesSummary() {
        print("Here we have a beautiful House with \(rooms) rooms. The price will be \(price) Dollars and your agent is \(agent)")
    }
}

extension House {
    init(rooms: Int, agent: String) {
        self.rooms = rooms
        self.price = rooms * 50_000
        self.agent = agent
    }
}

struct Office: Building {
    var rooms: Int
    var price: Int
    var agent: String

    func salesSummary() {
        print("Here we have a beautiful Office with \(rooms) rooms. The price will be \(price) Dollars and your agent is \(agent)")
    }
}

let house = House(rooms: 6, price: 150_000, agent: "Maxi")
house.salesSummary()

let office = Office(rooms: 2, price: 50_000, agent: "Maxi")
office.salesSummary()

let extHoues = House(rooms: 5, agent: "Maxi")
extHoues.salesSummary()

   

I think your solution ist wunderbar.

But you ask if you covered everything. I changed your solution a bit, so you might have a deeper understanding.

protocol Building {
    var rooms: Int { get }  // Why is this a var, if you specify you can only GET this value? Should it be a let?
    var price: Int { get }
    var agent: String { get }
    func salesSummary()
}

Your protocol says when you implement this protocol, your struct MUST have a var that a programmer can get. In your House and Office struct, you implement this as a var rooms: Int

I changed the Office struct to make rooms a computed property. This still satisfies the protocol. Because you can certainly GET the value of rooms (the contract), but you cannot write a value for rooms.

struct Office: Building {
    var rooms: Int { // Number of rooms is computed based on number of seats in the office building.
        seats / 2     // Example only.  Each office contains 2 seats.
    }

    var seats: Int  // added this variable to report number of seats in the office building.
    var price: Int
    var agent: String

    func salesSummary() {
        print("Here we have a beautiful building with \(seats) seats in \(rooms) offices. ")
    }
}

The deeper understanding is that the protocol can be implemented as a standard var or as a computed property. Your choice!

2      

I'm done for today and already put my laptop away so I can't paste any code, but my first solution looked very similar to the OP, just maybe less dynamic for the lack of a better word, and without the cost calculations. I did think about calculating the price based on the number of rooms but was too tired (long day at work!) so maybe I'll have a shot at it tomorrow.

I've also misunderstood or missed something in the checkpoints a couple of times. My advice would be to read through them very carefully and at least twice before writing any code. And when your program is ready, read through the assignment again and compare the different requirements to different parts of your code.

1      

Here's my new take on this checkpoint:

protocol RealEstate {
    var type:   String   { get }
    var rooms:  Int      { get }
    var roomPrice: Int   { get }
    var cost:   Int      { get }
    var agent:  String   { get }

    func summarize()
}

struct House: RealEstate {
    var type  = "residential"
    var rooms = 5
    var roomPrice: Int
    var cost: Int { get { rooms * roomPrice } }
    var agent = "Liz Ard"

    func summarize() {
        print("This house is a \(type) building with \(rooms) rooms. The price is \(cost) euros. If interested, please contact sales agent \(agent).")
    }
}

struct Office: RealEstate {
    var type  = "commercial"
    var rooms = 8
    var roomPrice: Int
    var cost: Int { get { rooms * roomPrice } }
    var agent = "Rep Tilian"

    func summarize() {
        print("AVAILABLE: \(type) space with \(rooms) rooms. The price is \(cost) euros. Contact sales agent \(agent) for more information.")
    }
}

let newHome = House(roomPrice: 12500)
newHome.summarize()

let newOffice = Office(roomPrice: 33000)
newOffice.summarize()

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Learn the most up-to-date techniques and strategies for testing new and legacy Swift code in this free practical course for iOS devs who want to become complete Senior iOS Developers.

Learn more

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

Reply to this topic…

You need to create an account or log in to reply.

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.