Skip to content

Commit 074819f

Browse files
committed
Day 15 commit
1 parent 66282de commit 074819f

File tree

9 files changed

+142
-0
lines changed

9 files changed

+142
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import Cocoa
2+
3+
// 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:
4+
//
5+
// A property storing how many rooms it has.
6+
// A property storing the cost as an integer (e.g. 500,000 for a building costing $500,000.)
7+
// A property storing the name of the estate agent responsible for selling the building.
8+
// A method for printing the sales summary of the building, describing what it is along with its other properties.
9+
10+
11+
protocol Building {
12+
var numberOfRooms: Int { get }
13+
var cost: Int { get set }
14+
var agent: String { get set }
15+
16+
func salesSummary()
17+
}
18+
19+
20+
struct House: Building {
21+
let numberOfRooms: Int
22+
var cost: Int
23+
var agent: String
24+
25+
func salesSummary() {
26+
print("This is a house.")
27+
print("It has \( self.numberOfRooms ) bedrooms.")
28+
print("You can buy it from \( agent ) for $\( cost )")
29+
}
30+
}
31+
32+
33+
struct Office: Building {
34+
let numberOfRooms: Int
35+
var cost: Int
36+
var agent: String
37+
38+
func salesSummary() {
39+
print("This is an office building.")
40+
print("It has an open floor plan with space for \( self.numberOfRooms ) offices.")
41+
print("Contact \( agent ) for pricing details.")
42+
}
43+
}
44+
45+
46+
47+
let home = House(numberOfRooms: 3, cost: 800_000, agent: "BayView Realtors")
48+
home.salesSummary()
49+
50+
print()
51+
52+
let loft = Office(numberOfRooms: 20, cost: 8_000_000, agent: "Enterprise Sales")
53+
loft.salesSummary()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

checkpoint-8.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

day-13.playground/Contents.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Cocoa
2+
3+
protocol Vehicle {
4+
func estimateTime(for distance: Int) -> Int
5+
func travel(distance: Int)
6+
}
7+
8+
struct Car: Vehicle {
9+
func estimateTime(for distance: Int) -> Int {
10+
distance / 50
11+
}
12+
13+
func travel(distance: Int) {
14+
print("I'm driving \(distance)km.")
15+
}
16+
17+
func openSunroof() {
18+
print("It's a nice day!")
19+
}
20+
}
21+
22+
23+
let twopointzero = 2.0
24+
let two = 2
25+
twopointzero == twopointzero
26+
two == two
27+
// This does not work, because of the different types.
28+
//two == twopointzero
29+
30+
31+
func trim(_ string: String) -> String {
32+
return string.trimmingCharacters(in: .whitespacesAndNewlines)
33+
}
34+
35+
extension String {
36+
func trimmed() -> String {
37+
return self.trimmingCharacters(in: .whitespacesAndNewlines)
38+
}
39+
40+
mutating func trim() -> Void {
41+
self = self.trimmed()
42+
}
43+
}
44+
45+
let quote = " There is whitespace around. "
46+
47+
quote.trimmed()
48+
quote
49+
50+
var otherQuote = " There is also whitespace here "
51+
otherQuote.trim()
52+
otherQuote
53+
54+
55+
protocol SuperHeroMovie {
56+
func writeScript() -> String
57+
}
58+
extension SuperHeroMovie {
59+
func makeScript() -> String {
60+
return """
61+
Lots of special effects,
62+
some half-baked jokes,
63+
and a hint of another
64+
sequel at the end.
65+
"""
66+
}
67+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

day-13.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)