Link to Checkpoint 6
Dear people,
I request your constructive feedback for my solution below, for Checkpoint 6. Please feel free to include multiple POVs (for example, how an equally experience (or more) developer might differe from you in this solution).
I understand this must be quite simple for you; but at this point, I don't know what I don't know. But I'm sure this could be made to be more efficient, use lesser energy, and possibly even read (or work) better.
So here's the code; please tell me if it is good. — and also if I got it right / did what Paul intended us to do with Checkpoint 6 :)
// Checkpoint 6
struct Car {
let model: String
let seats: Int
private(set) var currentGear = 0
mutating func gearShift(_ gear: Int) {
if gear < 0 || gear > 5 {
print("Stop! That really grinds my \(model)'s gears!")
} else if gear == 0 {
print("Why are we in Neutral? Are we ready to go?")
} else {
currentGear = gear
print("Driving my \(model) in gear \(gear).")
}
}
mutating func gearUp() {
if currentGear < 5 {
currentGear += 1
} else {
"Stop! You're breaking the car!"
}
}
mutating func gearDown() {
if currentGear > 0 {
currentGear -= 1
} else {
"Stop! You're breaking the car!"
}
}
init(model: String, seats: Int) {
self.model = model
self.seats = seats
}
}
var firstCar = Car(model: "Tesla", seats: 2)
firstCar.model
firstCar.seats
firstCar.gearShift(2)
firstCar.currentGear
firstCar.gearUp()
firstCar.currentGear
firstCar.gearDown()
firstCar.gearDown()
firstCar.currentGear
firstCar.gearShift(0)
var stolenCar = Car(model: "Pinto", seats: 4)
stolenCar.gearShift(6)
stolenCar.currentGear
stolenCar.gearUp()
stolenCar.currentGear
stolenCar.gearUp()
stolenCar.gearUp()
stolenCar.gearUp()
stolenCar.gearUp()
stolenCar.currentGear
Looking forward to your replies!
Thank you very much :)
PS: My Swift knowledge is limited to this, Day 11.