Menu
CoddyTech

가격 정책 추가

Coddy의 Lua 개발자를 위한 Python 코스 레슨. 48개 중 44번째.

challenge icon

챌린지

초급

1단계의 주문 모델을 유지하고 각 주문에 가격 책정 협력 객체를 추가하세요. 소계를 변경하지 않고 반환하는 StandardPolicy.total(subtotal)과 소계가 1000센트 이상일 때만 subtotal // 10을 빼는 BulkPolicy.total(subtotal)을 도입하세요. Order.total()과 재사용 가능한 make_policy(name) 함수를 추가하세요. 입력은 1단계의 처음 세 줄을 유지하고, 네 번째 줄에 standard 또는 bulk를 추가합니다. 그 외 모든 입력은 1단계의 규칙에 따라 여전히 유효합니다. 기존의 customer:quantity:subtotal 출력을 제거하세요. customer:subtotal:total만 출력하세요. 기존 모델을 포함한 전체 프로그램을 제출하세요. 이 반올림 규칙은 할인액에서 센트 단위 미만을 버립니다. 따라서 1009센트의 합계는 908이 아니라 909입니다.

전체 프로그램에서 이름이 지정된 클래스, 메서드, 함수를 그대로 유지하세요. 정상적으로 실행된 후 잠금 처리된 드라이버가 새 객체를 사용해 이 API들을 호출하고, 추가로 출력하거나 입력을 읽지 않은 채 동작을 검증합니다.

이전에 편집한 파일은 이어서 사용할 수 있습니다. order.py에서 생성자 매개변수 policy를 추가하고, 이를 저장한 뒤 total()을 구현하세요. 두 정책 클래스의 스캐폴드와 functions.py의 make_policy를 완성하고, parse_order는 3단계에서 다루도록 남겨 두세요. main.py에서는 main() 안의 이전 입력/출력 코드를 이번 단계의 네 줄 입력과 새 출력으로 바꾸세요. 제공된 import 문과 래퍼는 유지하세요.

네 단계 모두 같은 파일을 사용합니다. Order는 order.py에서, 가격 책정 클래스는 standard_policy.py와 bulk_policy.py에서, make_policy와 parse_order는 functions.py에서, 그리고 main() 안의 입력과 출력은 main.py에서 수정하세요. driver.py는 잠금 처리되어 있습니다. 이 파일은 main()을 한 번 호출한 다음 제공된 API 검사를 실행합니다.

직접 해보기

from order import Order
from standard_policy import StandardPolicy
from bulk_policy import BulkPolicy
from functions import make_policy, parse_order
from main import main

main()

assert StandardPolicy().total(1009) == 1009
assert BulkPolicy().total(999) == 999
assert BulkPolicy().total(1009) == 909
assert make_policy("standard").total(1200) == 1200
assert make_policy("bulk").total(1200) == 1080
class ProbePolicy:
    def __init__(self):
        self.calls = []
    def total(self, subtotal):
        self.calls.append(subtotal)
        return subtotal + 17
probe_policy = ProbePolicy()
probe_order = Order("probe", 2, 11, probe_policy)
assert (probe_order.customer, probe_order.quantity, probe_order.unit_cents) == ("probe", 2, 11)
assert probe_order.subtotal() == 22 and probe_order.total() == 39
probe_order.quantity = 3
assert probe_order.total() == 50 and probe_policy.calls == [22, 33]
probe_order.policy = StandardPolicy()
assert probe_order.total() == 33

Lua 개발자를 위한 Python의 모든 레슨

직접 연습해 보세요: 온라인 Python 컴파일러