Direct from page:
To check your knowledge, here’s a small task for you: create a struct to store information about a car, including its model, number of seats, and current gear, then add a method to change gears up or down. Have a think about variables and access control: what data should be a variable rather than a constant, and what data should be exposed publicly? Should the gear-changing method validate its input somehow?
Here’s my solution thus far. Going to make a note to remind myself to revise/update to meet the goals of the question. Main problem is now that the checkpoints are a little more complicated, I don’t really have a way to know if I’m going about them correctly without joining the subscription. Not complaining, because I get it but thats where we are at.
struct Car {
let numOfSeats: Int
let make: String
let model: String
var currentGear: String
private enum Gears: String {
case first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth
}
func changeGears(model: String, currentGear: String) -> Void {
print("The \(model) is now in \(currentGear) gear.")
}
init(make: String, model: String, numOfSeats: Int, currentGear: String) {
self.make = make
self.model = model
self.numOfSeats = numOfSeats
self.currentGear = currentGear
self.changeGears(model: model, currentGear: currentGear)
}
}
var myCar = Car(make: "Chevy", model: "Avalanche", numOfSeats: 4, currentGear: "first")
var myCar2 = Car(make: "Mazda", model: "Miata", numOfSeats: 2, currentGear: "second")
myCar.changeGears(model: myCar.model, currentGear: "third")