Skip to content

Commit 7ae3b45

Browse files
committed
Day 13 commit
1 parent f1e47c2 commit 7ae3b45

File tree

10 files changed

+93
-0
lines changed

10 files changed

+93
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Cocoa
2+
3+
// * Create a struct to store informatoinabout a car. Include:
4+
// * Model
5+
// * Number of seats
6+
// * Current grear
7+
// * Add a method to change gears up or down.
8+
// * What variables should be public and private.
9+
// * Don't allow invalid gears - 1..10 seems a fair range
10+
11+
enum ShiftError: Error {
12+
case gearTooHigh
13+
case gearTooLow
14+
}
15+
16+
struct Car {
17+
let model: String
18+
let seatCount: Int
19+
static let minGear: Int = 0
20+
static let maxGear: Int = 10
21+
// I am using private(set) for gear, becuase the driver needs access to the current gear.
22+
// But, the changing of the gear needs to go through the shift methdo.
23+
private(set) var currentGear: Int = 0 // 0 is for neutral
24+
25+
mutating func shift(to newGear: Int) throws {
26+
if newGear < Car.minGear {
27+
throw ShiftError.gearTooLow // I imagine these error sounding like trying to put a gear in without having the clutch pressed 😅
28+
} else if newGear > Car.maxGear {
29+
throw ShiftError.gearTooHigh
30+
}
31+
currentGear = newGear
32+
}
33+
}
34+
35+
let modelX = Car(model: "X", seatCount: 7)
36+
var q3 = Car(model: "Q3", seatCount: 5)
37+
38+
print(q3.currentGear)
39+
try! q3.shift(to: 1)
40+
print(q3.currentGear)
41+
try! q3.shift(to: 5)
42+
print(q3.currentGear)
43+
try? q3.shift(to: 11)
44+
print(q3.currentGear)
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-6.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.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>day-10 (Playground).xcscheme</key>
8+
<dict>
9+
<key>isShown</key>
10+
<false/>
11+
<key>orderHint</key>
12+
<integer>0</integer>
13+
</dict>
14+
</dict>
15+
</dict>
16+
</plist>

day-11.playground/Contents.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Cocoa
2+
3+
// Looks like 0 prefixes are ignored.
4+
008
5+
6+
7+
8+
9+
10+
11+
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-11.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)