Swift

Solution - Car Rentals

Our solution to the mini-project

Our Solution

Here is our solution. Your solution does not have to look like ours. If it works and is concise, then you’ve done it right!

import UIKit

class Car {
    let wheels = 4
    var cost: Double
    var year: Int
    init(cost: Double, year: Int) {
        self.cost = cost
        self.year = year
    }
    func description() {
        print("I have \(wheels) wheels!")
    }
}
class Coupe: Car {
    var seats = 2
    override init(cost: Double, year: Int) {
        super.init(cost: cost, year: year)
    }
    override func description() {
        print("I have \(wheels) wheels and \(seats) seats. I cost $\(cost) per day and am from \(year)!")
    }
}
class Sedan: Car {
    var seats = 4
    override init(cost: Double, year: Int) {
        super.init(cost: cost, year: year)
    }
    override func description() {
        print("I have \(wheels) wheels and \(seats) seats. I cost $\(cost) per day and am from \(year)!")
    }
}
let civiccoupe = Coupe(cost: 50.00, year: 2019)
let mercsedan = Sedan(cost: 70.00, year: 2014)
let bmw3series = Sedan(cost: 80.00, year: 2019)
let ferrari = Coupe(cost: 500.00, year: 2011)
let cars = [civiccoupe, mercsedan, bmw3series, ferrari]

for car in cars {
    car.description()
}

Feel free to contact us if you have any questions.

chevron_up