checkpoint 6: Car struct

그루두·2024년 4월 20일
0

100 days of SwiftUI

목록 보기
17/108

100 days of swiftui: checkpoint 6
https://www.hackingwithswift.com/quick-start/beginners/checkpoint-6

challenge

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?

solution

struct Car {
    let model: String
    let seatCount: Int
    private var currentGear = 0
    
    init(model: String, seatCount: Int) {
        self.model = model
        self.seatCount = seatCount
        print("Initial gear is 0.")
    }
    mutating func changeGear(isUp: Bool) {
        if isUp {
            if currentGear == 10 {
                print("You can't change gear up. This is maximum!")
            } else {
                currentGear += 1
                print("Now current gear is \(currentGear)!")
            }
        } else {
                if currentGear == 0 {
                    print("You can't change gear down. This is manimum!")
                } else {
                    currentGear -= 1
                    print("Now current gear is \(currentGear)!")
                }
        }
    }
}
  • 차의 모델이나 좌석 수는 변경되지 않으니 let으로 설정했다.
  • 기어는 처음에 0으로 설정하고 changegear를 통해 한 단계씩 변경할 수 있도록 private를 이용했다.
  • 그리고 기어가 최소이거나 최대일 때 더이상 기어의 가능한 범위를 벗어나지 않도록 조건문을 사용해서 경고문을 print한다.
var newCar = Car(model: "bmw something", seatCount: 4)
newCar.changeGear(isUp: false)
newCar.changeGear(isUp: true)

결과:

Initial gear is 0.
You can't change gear down. This is manimum!
Now current gear is 1!

코드 파일
https://github.com/soaringwave/Ios-studying/commit/d5691ec51d719a5cc159c26b38e0ab61d6024bcd

profile
계속 해보자

3개의 댓글

comment-user-thumbnail
2024년 7월 30일

When considering Car struct, it refers to the structural integrity and design of a vehicle, which is crucial for safety and performance. A well-built structure provides better handling, durability, and protection in case of accidents. For truck enthusiasts, the choice of a vehicle with a solid structure is essential, especially for heavy-duty tasks. At the New and Used RAM Truck Sale Cheyenne WY you can find a variety of RAM trucks known for their robust build and reliable performance. Whether you're looking for a brand-new model or a pre-owned one, the selection offers options that cater to different needs and preferences. Investing in a well-structured vehicle ensures a safer and more enjoyable driving experience.

답글 달기
comment-user-thumbnail
2024년 10월 15일

AJ Kenya Safaris is committed to promoting environmentally sustainable tourism. Their tours minimize the impact on the natural environment while still offering an unforgettable safari experience. We rented a vehicle through Car hire in Ruiru, and were pleased to see their commitment to low-impact travel. Their eco-friendly lodges and practices made it clear that they care about preserving Kenya’s natural beauty for future generations. It’s great to see a company that balances tourism with environmental responsibility.

답글 달기
comment-user-thumbnail
2024년 12월 13일

Cars play a crucial role in modern transportation, offering convenience and freedom to travel. In Georgia, a significant advancement in urban mobility has been the introduction of carsharing services. Carsharing started operating in Georgia providing residents and tourists with an affordable and eco-friendly transportation option. This innovative service allows users to rent vehicles for short trips, reducing traffic congestion and promoting sustainable practices. Carsharing has revolutionized the way people move, especially in urban areas, by minimizing the need for private car ownership. With its growing popularity, carsharing is set to enhance mobility and convenience for Georgians in the coming years.

답글 달기

관련 채용 정보