가격 정책 추가하기
Coddy의 Dart 개발자를 위한 Python 코스 레슨. 48개 중 44번째.
챌린지
초급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
Dart 개발자를 위한 Python의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러