diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index 859d9f7..ff18327 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -33,69 +33,59 @@ - Customer 1 purchased 1 banana; he used 0 rewards points """ +from collections import defaultdict class Item: - def __init__(self, itemId, item_price): - self.itemId = itemId + def __init__(self, _id, item_price): + self._id = _id self.item_price = item_price -from collections import defaultdict - class RewardsSystem: - REWARDS_RATIO_BELOW = 18 + REWARDS_RATIO_BELOW_CUTOFF = 18 + REWARDS_RATIO_ABOVE_CUTOFF = 17 REWARDS_CUTOFF = 250 def __init__(self): self.rewards_points = defaultdict(int) self.items_purchased = defaultdict(int) + def get_items_purchased(self, item_id): + return self.items_purchased[item_id] + def process_log(self, log): - amount_spent = defaultdict(int) + customer_to_amount_spent = defaultdict(int) + # First, process all logs for log_entry in log: customer_id = log_entry[0] reward_points_used = log_entry[1] items_purchased = log_entry[2] - if not customer_id: - total_spent = 0 - for item in items_purchased: - total_spent += item.itemId * item.item_price - - # Update items sold - for purchase in items_purchased: - self.items_purchased[purchase.itemId] = self.items_purchased.get(purchase.itemId, 0) + 1 - - items_purchased = len(items_purchased) == 0 - if items_purchased: - raise ValueError('Items purchased were not recorded.') - - else: + if not items_purchased: + # Empty list of purchases, error! + raise ValueError('Items purchased were not recorded.') + if customer_id: # Subtract rewards points used from customer self.rewards_points[customer_id] -= reward_points_used - total_spent = 0 for item in items_purchased: - total_spent += item.itemId * item.item_price - - amount_spent[customer_id] = amount_spent.get(customer_id, 0) + total_spent + customer_to_amount_spent[customer_id] += item.item_price - # Update items sold - for purchase in items_purchased: - self.items_purchased[purchase.itemId] = self.items_purchased.get(purchase.itemId, 0) + 1 - - print(self.reward_points) + # Update purchase counts by item + for purchase in items_purchased: + self.items_purchased[purchase._id] += 1 # At end of day, award reward points back to customers based on how much they spent - for customer_id in amount_spent: - # Calculate rewards points received - rewards_points = amount_spent[customer_id] // RewardsSystem.REWARDS_RATIO_BELOW - if amount_spent > RewardsSystem.REWARDS_CUTOFF: - rewards_points = amount_spent[customer_id] // 17 - + for customer_id, amount in customer_to_amount_spent.items(): + + # Determine rewards ratio to use + if amount > RewardsSystem.REWARDS_CUTOFF: + ratio = RewardsSystem.REWARDS_RATIO_ABOVE_CUTOFF + else: + ratio = RewardsSystem.REWARDS_RATIO_BELOW_CUTOFF + # Update customer rewards points - self.rewards_points[customer_id] += rewards_points + self.rewards_points[customer_id] += amount // ratio - def get_items_purchased(self, item_id): - return self.items_purchased[item_id] +