Problem Statement
When fines are calculated without a clear, consistent method, errors occur leading to unfair penalties or revenue loss. Manual or ad-hoc fine calculations cause confusion and disputes among users and administrators.
┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │ User Input │─────▶│ Fine Calculation│─────▶│ Fine Output │ │ (violation, │ │ Module │ │ (amount, │ │ delay, etc) │ │ (rules engine) │ │ details) │ └───────────────┘ └───────────────┘ └───────────────┘
This diagram shows the flow from user input through the fine calculation module applying rules, producing the fine output.
### Before: naive fine calculation without clear structure def calculate_fine(days_late): if days_late <= 0: return 0 elif days_late <= 5: return days_late * 10 else: return 100 ### After: structured fine calculation using a class and rules class FineCalculator: def __init__(self): self.rules = [ (lambda days: days <= 0, lambda days: 0), (lambda days: days <= 5, lambda days: days * 10), (lambda days: True, lambda days: 100) ] def calculate(self, days_late): for condition, action in self.rules: if condition(days_late): return action(days_late) # Usage calculator = FineCalculator() fine = calculator.calculate(3) # returns 30
What is the primary purpose of a fine calculation system in low-level design?
Which of the following is the correct way to represent a fine rate for a violation type in a configuration file?
violation_fine_rates = {'speeding': 100,'parking': 50,'signal_jump': 150}
Given the following code snippet, what will be the total fine calculated?
violation_fine_rates = {'speeding': 100, 'parking': 50}violations = ['speeding', 'parking', 'speeding']total_fine = sum(violation_fine_rates[v] for v in violations)print(total_fine)
Identify the error in the following fine calculation code snippet:
violation_fine_rates = {'speeding': 100, 'parking': 50}violations = ['speeding', 'parking', 'signal_jump']total_fine = sum(violation_fine_rates[v] for v in violations)print(total_fine)
You are designing a fine calculation system that must support multiple violation types, each with different fine rates and possible discounts for repeat offenses. Which design approach is best?
Pass the test and this pattern moves unknown