checkpoint 8: protocol Building

그루두·2024년 4월 22일
0

100 days of SwiftUI

목록 보기
21/108

100 days of swiftui: checkpoint 8
https://www.hackingwithswift.com/quick-start/beginners/checkpoint-8

challenge

Your challenge is this: make a protocol that describes a building, adding various properties and methods, then create two structs, House and Office, that conform to it. Your protocol should require the following:

  1. A property storing how many rooms it has.
  2. A property storing the cost as an integer (e.g. 500,000 for a building costing $500,000.)
  3. A property storing the name of the estate agent responsible for selling the building.
  4. A method for printing the sales summary of the building, describing what it is along with its other properties.

solution

protocol Building {
    var name: String { get set }
    var rooms: Int { get set }
    var cost: Int { get set }
    var estateAgent: String { get set }
    func printSummary()
}

extension Building {
    func printSummary() {
        let summary = """
\(name):
- has \(rooms) rooms.
- costs \(cost).
- \(estateAgent) is responsible for selling here.

"""
        print(summary)
    }
}

struct House: Building {
    var name: String
    var rooms: Int
    var cost: Int
    var estateAgent: String
}

struct Office: Building {
    var name: String
    var rooms: Int
    var cost: Int
    var estateAgent: String
}

var greenHouse = House(name: "green house", rooms: 3, cost: 100_000, estateAgent: "Ms. Green")
var smallOffice = Office(name: "small office", rooms: 2, cost: 1_000_000, estateAgent: "Some Boss")
greenHouse.printSummary()
smallOffice.printSummary()

결과:

green house:
- has 3 rooms.
- costs 100000.
- Ms. Green is responsible for selling here.

small office:
- has 2 rooms.
- costs 1000000.
- Some Boss is responsible for selling here.

코드 파일
https://github.com/soaringwave/Ios-studying/commit/e3b26c399cc4ccaf5bb4885f74900e9edafaea4f

profile
계속 해보자

0개의 댓글

관련 채용 정보