Day 28 120 min beginner

Project: Personal Finance Tracker

Build a professional-grade CLI tool to manage expenses, using JSON for storage and math for analysis.

Learning Objectives

  • Model expense data using dictionaries
  • Implement JSON-based data persistence
  • Perform statistical analysis on collections
  • Build a multi-feature CLI interface

Week 4 Milestone

You’ve learned the “Power Features” of Python: Standard library tools, JSON data, and project isolation. Today, you’ll build a Personal Finance Tracker to put it all together.

Project Requirements

Your application should allow a user to:

  1. Add an expense (Amount, Category, Date).
  2. View all expenses in a clean table.
  3. Analyze spending (Total spent, Average expense).
  4. Export/Import data automatically using a finance.json file.

Step 1: Modeling an Expense

finance.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import json
from datetime import datetime

class FinanceTracker:
    def __init__(self):
        self.filename = "finance.json"
        self.expenses = self.load_data()

    def load_data(self):
        try:
            with open(self.filename, "r") as f:
                return json.load(f)
        except (FileNotFoundError, json.JSONDecodeError):
            return []

    def save_data(self):
        with open(self.filename, "w") as f:
            json.dump(self.expenses, f, indent=4)

    def add_expense(self, amount, category):
        expense = {
            "amount": float(amount),
            "category": category,
            "date": datetime.now().strftime("%Y-%m-%d %H:%M")
        }
        self.expenses.append(expense)
        self.save_data()

Step 2: Adding Analysis

analysis.py
1
2
3
4
5
6
7
8
    def get_total(self):
        return sum(e["amount"] for e in self.expenses)

    def get_summary(self):
        total = self.get_total()
        count = len(self.expenses)
        avg = total / count if count > 0 else 0
        return f"Total: ${total:.2f} | Count: {count} | Avg: ${avg:.2f}"

Your Challenge

Implement the CLI loop in main():

  • Use Type Hints for your helper functions.
  • Add a feature to filter expenses by Category (e.g., “Show only Food”).
  • Bonus: Use the statistics module to calculate the median expense.

Interactive Practice

Visualize the data flow: User Input -> Python Dict -> JSON File -> Storage.

flowchart LR User[User Input] --> App[Python Script] App -- json.dump --> File[finance.json] File -- json.load --> App App --> Report[CLI Report]

Quiz

Complete this quiz with a minimum score of 80% to mark Day 28 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!