Last active
September 28, 2022 09:13
-
-
Save nmbr73/537765de5720100c0d1cb6008b7c73b3 to your computer and use it in GitHub Desktop.
My attempt to solve 'Checkpoint 6' of #100DaysOfSwiftUI (https://www.hackingwithswift.com/100/swiftui)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bear with me - sure, exceptions, custom initializers, observers, static methods, | |
// computed properties, etc. are really not all needed to solve the problem, but I | |
// wanted to try some of the things we learned so far and thereby the code got a bit | |
// bloated maybe. | |
struct NonSense: Error { | |
} | |
struct Car { | |
// Okay, I do allow -1 as a valid value ... I read the hyphen as a minus | |
// and "-1...10 seems a fair maximum range" made perfect sense to me 😂 | |
static let minGear = -1 | |
static let maxGear = 10 | |
let model: String | |
let numberOfSeats: Int | |
// I liked the concept of that `private(set)` - did always want something like | |
// this in other languages. But now that I see how it turned out, I'm not so | |
// sure - maybe doing the range check using obeservers might have been the better | |
// approach?!? | |
private(set) var currentGear: Int = 0 { | |
didSet { | |
print("[engine makes some noise and now we are at currentGear=\(currentGear)]") | |
} | |
} | |
init(model: String, numberOfSeats: Int = 5) throws { | |
self.model = model | |
if numberOfSeats < 1 { | |
throw NonSense() | |
} | |
self.numberOfSeats = numberOfSeats | |
} | |
mutating func gearUp() -> Bool { | |
if currentGear >= Self.maxGear { | |
print("[what are you trying to do here?]") | |
return false | |
} | |
currentGear += 1 | |
return true | |
} | |
mutating func gearDown() -> Bool { | |
if currentGear <= Self.minGear { | |
print("[that gear stick will hardly move any further!]") | |
return false | |
} | |
currentGear -= 1 | |
return true | |
} | |
func gearAsString() -> String { | |
switch currentGear { | |
case -1: | |
return "R" | |
case 0: | |
return "N" | |
default: | |
return "D"+String(currentGear) | |
} | |
} | |
var string: String { | |
"I'm a \(model) with \(numberOfSeats) seats and my gear lever in the '\(gearAsString())' position" | |
} | |
static func crash() { | |
print("Outch, that went terribly wrong!") | |
} | |
} | |
do { | |
var car: Car | |
car = try Car(model: "Citroën DS", numberOfSeats: 4) | |
print(car.string) | |
car.gearDown() | |
print(car.string) | |
car.gearDown() | |
print(car.string) | |
car.gearUp() | |
print(car.string) | |
car.gearUp() | |
print(car.string) | |
car = try Car(model: "Citroën DS", numberOfSeats: -1) | |
print(car.string) | |
} catch { | |
Car.crash() | |
} | |
/* OUTPUT: | |
I'm a Citroën DS with 4 seats and my gear lever in the 'N' position | |
[engine makes some noise and now we are at currentGear=-1] | |
I'm a Citroën DS with 4 seats and my gear lever in the 'R' position | |
[that gear stick will hardly move any further!] | |
I'm a Citroën DS with 4 seats and my gear lever in the 'R' position | |
[engine makes some noise and now we are at currentGear=0] | |
I'm a Citroën DS with 4 seats and my gear lever in the 'N' position | |
[engine makes some noise and now we are at currentGear=1] | |
I'm a Citroën DS with 4 seats and my gear lever in the 'D1' position | |
That went terribly wrong! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment