← 返回 apple 的题目列表Calculate Final Price of Ice Cream Order
类型:online_judge
Problem Statement
Develop a program for processing orders from a new automated ice cream machine at an ice cream shop. The machine allows customers to create custom ice cream orders by selecting flavors, add-ons, and specifying allergy options. Some flavors and add-ons are mutually exclusive. You need to write a function that calculates the final price of an ice cream order based on customer selections.
[Flavors]
The machine offers the following ice cream flavors, each with a base price:
Vanilla: $2.00
Chocolate: $2.50
[Add-ons]
Customers can choose any number of the following add-ons, each with an additional cost:
Sprinkles: $0.50
Hot Fudge: $0.75
[Mutually Exclusive Options]
Some flavors and add-ons cannot coexist in the ice cream:
For example, Mango flavor cannot be combined with Hot Fudge.
[Allergy Options]
Customers can specify the following allergies. If an allergy is specified, the corresponding add-on must not be included in the order:
Nut Allergy: Cannot include Nuts.
Dairy Allergy: Cannot include Whipped Cream.
Gluten Allergy: Cannot include Sprinkles.
[Order Calculation Rules]
Only one flavor can be selected.
Each add-on can only be added once.
The final price of the ice cream is the sum of the base price of the selected flavor and the costs of the selected add-ons.
Mutually exclusive components cannot exist in the ice cream at the same time. If an add-on conflicts with the flavor, it is removed from the build. If two add-ons conflict with each other, both are removed from the build.
It can be assumed that cyclic mutual exclusion is impossible.
If an add-on that a customer is allergic to is included in the order, it should be excluded from the final price calculation.
Allergy options take precedence over other constraints. For example, if the add-ons include Whipped Cream and Nuts, while the customer is allergic to Nuts, Whipped Cream can be preserved as Nuts are removed due to allergy concerns.
[Functional Requirements]
Your implementation should return the price of the ice cream. A valid ice cream contains 1 flavor plus any number of add-ons which do not break any mutual exclusion or allergy constraints.
If a valid ice cream cannot be built from the input, your implementation should return -1.
If the input itself is invalid, return -1.
You should write unit test cases to justify the correctness of your implementation.
Example
Input
Vanilla, Sprinkles, Nut Allergy