Projects · 6 / 19 Exercise Hard

Expense tracker

Objective

Create a Python program that manages a list of expenses using an enumeration for the categories and a dataclass for each expense.

Instructions

  • Read an integer n and then n lines with the format concept amount CATEGORY. The concept has no spaces, the amount has at most two decimals and the category must be FOOD, TRANSPORT, LEISURE or OTHER.
  • If the category is not valid, show Line k: invalid category (X) (with k the line number, starting at 1) and discard the expense.
  • Store amounts in cents (integers) to avoid rounding errors. Show four CATEGORY: x.xx lines (FOOD, TRANSPORT, LEISURE, OTHER) with the total of each category with two decimals, and then Total: x.xx.
  • If there are valid expenses, show Biggest expense: concept (x.xx) (the first one on a tie) and Most expensive category: CATEGORY (the first in the order above on a tie). If there are none, show No expenses.
Input
4
pan 2.50 FOOD
bus 1.20 TRANSPORT
cine 9.00 LEISURE
regalo 5.00 GIFT
Output
Line 4: invalid category (GIFT)
FOOD: 2.50
TRANSPORT: 1.20
LEISURE: 9.00
OTHER: 0.00
Total: 12.70
Biggest expense: cine (9.00)
Most expensive category: LEISURE

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise