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