차량 대여 서비스
Coddy GDScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 65개 중 65번째.
챌린지
중급차량 대여 서비스를 만드세요. 차량 두 종류와 대여, 그리고 둘 다 관리하는 대리점으로 구성됩니다. 가격은 정수 달러입니다(int).
vehicle.gd의 Vehicle:
- 생성자는
vin,make,model및daily_rate를 받습니다.available은true로 시작합니다. start_rental() -> bool: 차량을 이용할 수 없으면false를 반환하고, 그렇지 않으면 차량을 이용할 수 없도록 표시한 후true를 반환합니다.end_rental()은 차량을 다시 이용할 수 있게 합니다._to_string()은MAKE MODEL (VIN: VIN) - Available을 반환하거나, 끝부분에- Rented를 반환합니다.
car.gd의 Car와 motorcycle.gd의 Motorcycle은 Vehicle을 확장합니다. 각 생성자는 네 가지 차량 값과 함께 seats(자동차) 또는 engine_size(오토바이)를 받습니다. 이들의 _to_string()은 부모의 텍스트를 바탕으로 구성됩니다. Car: + 차량 텍스트 + - 5 seats, 그리고 Motorcycle: + 차량 텍스트 + - 600cc입니다.
rental.gd의 Rental:
- 생성자는
rental_id,vehicle객체,customer_name및days를 받습니다.is_active는true로 시작합니다. calculate_cost() -> int: 일일 요금에 대여 일수를 곱한 값입니다.end_rental() -> bool: 이미 종료된 대여라면false를 반환하고, 그렇지 않으면 대여와 차량의 대여를 종료한 뒤true를 반환합니다._to_string():Rental ID: MAKE MODEL for CUSTOMER, D days, $COST - Active를 반환하거나- Completed를 반환합니다.
rental_agency.gd의 RentalAgency:
- 생성자는
agency_name을 받습니다.vehicles는 VIN에서 차량으로 이어지는 딕셔너리이고,rentals는 대여 ID에서 대여로 이어지는 딕셔너리입니다.next_rental_id는1로 시작합니다. add_vehicle(vehicle)은 차량을 해당 VIN을 키로 저장합니다.rent_vehicle(vin, customer_name, days) -> String: 알 수 없는 VIN,1일 미만의 대여 기간 또는 이용할 수 없는 차량이면""을 반환합니다. 그 외의 경우 차량 대여를 시작하고,R1,R2... 형식의 ID로Rental을 생성한 후 저장하고 ID를 반환합니다(카운터는 대여가 성공한 경우에만 증가합니다).return_vehicle(rental_id) -> bool: 알 수 없는 ID라면false를 반환하고, 그렇지 않으면 대여의end_rental()이 반환하는 값을 반환합니다.available_vehicles() -> Array[Vehicle]: 이용 가능한 차량을 추가된 순서대로 반환합니다.total_revenue() -> int: 진행 중이거나 완료된 모든 대여 비용의 합입니다.
대여 객체는 차량을 저장하지만 차량은 대여를 저장하지 않으므로, 두 객체가 서로를 참조하지 않습니다. 다음 입력의 경우:
Speedy Rentals
car C1|Toyota|Camry|50|5
moto M1|Honda|CBR|40|600
rent C1|Ana|3
rent C1|Ben|2
available
kind M1
rentals
revenue출력은 다음과 같습니다:
Rented C1 as R1: $150
Cannot rent C1
Available at Speedy Rentals:
- Motorcycle: Honda CBR (VIN: M1) - Available - 600cc
M1: Vehicle true, Car false, Motorcycle true
Rental R1: Toyota Camry for Ana, 3 days, $150 - Active
Revenue: $150코드는 vehicle.gd, car.gd, motorcycle.gd, rental.gd 및 rental_agency.gd에 작성하세요. main.gd에는 제공된 입력/출력 코드가 있으며 수정할 수 없습니다.
직접 해보기
extends Node
func _ready():
# 제공된 입출력 코드: 그대로 유지하세요
var agency := RentalAgency.new(OS.read_string_from_stdin())
var line := OS.read_string_from_stdin()
while line != "":
var parts := line.split(" ", true, 1)
var f: PackedStringArray = parts[1].split("|") if parts.size() > 1 else PackedStringArray()
match parts[0]:
"vehicle":
agency.add_vehicle(Vehicle.new(f[0], f[1], f[2], int(f[3])))
"car":
agency.add_vehicle(Car.new(f[0], f[1], f[2], int(f[3]), int(f[4])))
"moto":
agency.add_vehicle(Motorcycle.new(f[0], f[1], f[2], int(f[3]), int(f[4])))
"rent":
var rental_id: String = agency.rent_vehicle(f[0], f[1], int(f[2]))
if rental_id == "":
print("Cannot rent ", f[0])
else:
var rental: Rental = agency.rentals[rental_id]
print("Rented %s as %s: $%d" % [f[0], rental_id, rental.calculate_cost()])
"return":
print("Return ", f[0], ": ", agency.return_vehicle(f[0]))
"available":
print("Available at ", agency.agency_name, ":")
for vehicle in agency.available_vehicles():
print("- ", vehicle)
"fleet":
for vin in agency.vehicles:
print(agency.vehicles[vin])
"kind":
var vehicle: Vehicle = agency.vehicles[f[0]]
print("%s: Vehicle %s, Car %s, Motorcycle %s" % [f[0], vehicle is Vehicle, vehicle is Car, vehicle is Motorcycle])
"rentals":
for rental_id in agency.rentals:
print(agency.rentals[rental_id])
"revenue":
print("Revenue: $", agency.total_revenue())
line = OS.read_string_from_stdin()
객체 지향 프로그래밍의 모든 레슨
직접 연습해 보세요: 온라인 GDScript 컴파일러