DEV Community

Putra Prima A
Putra Prima A

Posted on

Dart Object Oriented For Beginner : Expense Manager Case Study Part 1

Lesson 1: What is OOP? (Why We Need It)

Duration: 15 minutes

App Feature: 🎯 Planning our Expense Manager

What You'll Learn: Understand how OOP helps organize our expense tracking app


Introduction

Welcome to your journey of learning Dart and Object-Oriented Programming! Instead of learning abstract concepts, we're going to build a real expense manager app together. By the end of this tutorial, you'll understand not just what OOP is, but why it's essential for building apps.


The Problem: Life Without OOP

Imagine you want to track your expenses. Let's say you want to record:

  • A coffee you bought for $4.50
  • Groceries for $85.50
  • Netflix subscription for $15.99

Approach 1: Separate Variables (The Bad Way)

void main() {
  // First expense - Coffee
  String expense1Description = 'Coffee';
  double expense1Amount = 4.50;
  String expense1Category = 'Food';
  DateTime expense1Date = DateTime.now();

  // Second expense - Groceries
  String expense2Description = 'Groceries';
  double expense2Amount = 85.50;
  String expense2Category = 'Food';
  DateTime expense2Date = DateTime.now();

  // Third expense - Netflix
  String expense3Description = 'Netflix';
  double expense3Amount = 15.99;
  String expense3Category = 'Entertainment';
  DateTime expense3Date = DateTime.now();

  print('Expense 1: $expense1Description - \$$expense1Amount');
  print('Expense 2: $expense2Description - \$$expense2Amount');
  print('Expense 3: $expense3Description - \$$expense3Amount');
}
Enter fullscreen mode Exit fullscreen mode

Problems with This Approach:

  1. Messy and repetitive - We need 4 variables for each expense!
  2. Hard to manage - What if you have 50 expenses? That's 200 variables!
  3. No relationship - Nothing connects expense1Description with expense1Amount
  4. Error-prone - Easy to mix up expense1Amount with expense2Amount
  5. Can't reuse code - Want to calculate total? You need to add each variable manually

Try to imagine tracking 10 expenses this way... That's 40 separate variables! 😱


The Solution: Object-Oriented Programming

OOP lets us bundle related data together. Instead of 4 separate variables per expense, we create a blueprint (called a class) and make objects from it.

Think of it Like This:

Real World Analogy:

🏠 House Blueprint (Class)

  • A blueprint shows what every house should have: rooms, doors, windows
  • It's not an actual house, just a template

🏑 Actual Houses (Objects)

  • You can build many houses from one blueprint
  • Each house is unique (different colors, sizes) but follows the blueprint

In Programming:

πŸ“‹ Expense Class (Blueprint)

  • Defines what every expense should have: description, amount, category, date
  • Just a template, not actual data

πŸ’° Expense Objects (Actual Expenses)

  • Coffee expense: $4.50, Food, Today
  • Netflix expense: $15.99, Entertainment, Today
  • Each is a real expense created from the template

Understanding Classes and Objects

What is a Class?

A class is a blueprint or template that defines:

  • Properties (data): what information it stores
  • Methods (actions): what it can do

Think of it as a cookie cutter - it shapes what the cookies will look like, but it's not the cookie itself.

What is an Object?

An object is an actual instance created from a class.

Think of it as the cookies - real things you can eat, made from the cookie cutter template.


Our First Look at OOP Code

Don't worry if you don't understand everything yet - we'll learn step by step. This is just to show you the difference:

// The BLUEPRINT - This is a class
class Expense {
  String description;
  double amount;
  String category;
  DateTime date;
}

void main() {
  // Creating OBJECTS from the blueprint
  var coffee = Expense();
  coffee.description = 'Coffee';
  coffee.amount = 4.50;
  coffee.category = 'Food';
  coffee.date = DateTime.now();

  var netflix = Expense();
  netflix.description = 'Netflix';
  netflix.amount = 15.99;
  netflix.category = 'Entertainment';
  netflix.date = DateTime.now();

  print('${coffee.description}: \$${coffee.amount}');
  print('${netflix.description}: \$${netflix.amount}');
}
Enter fullscreen mode Exit fullscreen mode

See the difference?

  • We defined the Expense structure once
  • We created multiple expenses easily
  • Each expense is a separate object with its own data
  • The code is cleaner and more organized

Why OOP Matters for Your Expense Manager

As we build our expense manager app, you'll see how OOP helps us:

1. Organization

Without OOP: 100 expenses = 400+ variables floating around
With OOP: 100 expense objects, each neatly packaged
Enter fullscreen mode Exit fullscreen mode

2. Reusability

// Want to add a new expense? Just create an object!
var lunch = Expense();
var dinner = Expense();
var gas = Expense();
// Same blueprint, different data
Enter fullscreen mode Exit fullscreen mode

3. Maintainability

// Need to add a "notes" field to all expenses?
// Change the class ONCE, all objects get the new feature
class Expense {
  String description;
  double amount;
  String category;
  DateTime date;
  String notes; // Added here - affects all expenses!
}
Enter fullscreen mode Exit fullscreen mode

4. Real-World Modeling

Your app will have:
- Expense objects (each purchase)
- Category objects (Food, Transport, etc.)
- Budget objects (monthly limits)
- User object (your profile)

Each is a separate class, working together!
Enter fullscreen mode Exit fullscreen mode

The Four Pillars of OOP

As we build our expense manager, we'll learn these four core concepts:

1. Encapsulation πŸ”’

Bundle data and methods together, hide internal details.

In our app: Keep expense data safe, validate amounts (can't be negative!)

2. Inheritance 🌳

Create new classes based on existing ones.

In our app: RecurringExpense (like Netflix) inherits from Expense but adds frequency

3. Polymorphism 🎭

One action, multiple forms.

In our app: All expenses can printDetails(), but recurring ones show frequency too

4. Abstraction 🎯

Hide complex details, show only what's necessary.

In our app: You don't need to know HOW expenses are stored, just add/view/delete them

Don't worry if these seem confusing now! We'll learn each one by actually using it in our app.


What We're Building Together

Over the next lessons, we'll build a complete expense tracking system:

πŸ“± Expense Manager App
β”œβ”€β”€ πŸ’° Expense Class
β”‚   β”œβ”€β”€ Track amount, date, category
β”‚   β”œβ”€β”€ Validate data (no negative amounts!)
β”‚   └── Calculate totals
β”‚
β”œβ”€β”€ πŸ”„ RecurringExpense Class
β”‚   β”œβ”€β”€ Monthly bills (rent, subscriptions)
β”‚   └── Calculate yearly costs
β”‚
β”œβ”€β”€ 🎯 OneTimeExpense Class
β”‚   β”œβ”€β”€ Special purchases (gifts, emergencies)
β”‚   └── Tag occasions
β”‚
β”œβ”€β”€ πŸ’³ PaymentMethod Classes
β”‚   β”œβ”€β”€ Credit card, cash, digital wallet
β”‚   └── Process payments differently
β”‚
└── πŸ“Š ExpenseManager Class
    β”œβ”€β”€ Store all expenses
    β”œβ”€β”€ Filter by category
    β”œβ”€β”€ Calculate totals
    └── Generate reports
Enter fullscreen mode Exit fullscreen mode

Quick Check: Do You Understand?

Before moving to the next lesson, make sure you can answer these:

βœ… Self-Check Questions:

  1. What's the difference between a class and an object? Click to see answer
  • Class = Blueprint/template (defines structure)
  • Object = Actual instance (real data)
  • Example: Expense is the class, your coffee purchase is an object
  1. Why is OOP better than using separate variables? Click to see answer
  • Organizes related data together
  • Easier to manage many items
  • Less error-prone
  • Code is reusable
  1. Name one real-world example of a class and its objects Click to see answer

Examples:

  • Class: Car β†’ Objects: Your Toyota, Your friend's Honda
  • Class: Student β†’ Objects: John, Sarah, Mike
  • Class: Song β†’ Objects: "Bohemian Rhapsody", "Hotel California"

Try It Yourself! 🎯

Before moving to Lesson 2, think about what properties an Expense should have:

Your Task:

Write down (on paper or in notes) what information you'd want to track for each expense:

Example answers:

  • βœ“ Description (what you bought)
  • βœ“ Amount (how much it cost)
  • βœ“ Category (Food, Transport, etc.)
  • βœ“ Date (when you bought it)
  • ? What else would be useful?

Bonus questions to think about:

  • Should expenses have a unique ID number?
  • Should we track the payment method (cash, card)?
  • Should we allow notes or tags?

We'll use your ideas in the next lesson when we actually create the Expense class!


Key Takeaways πŸŽ“

Before moving forward, remember:

βœ… OOP organizes code into objects - like real-world things

βœ… Classes are blueprints - they define structure

βœ… Objects are instances - actual data created from classes

βœ… OOP makes code cleaner - easier to read and maintain

βœ… Perfect for apps - Flutter itself is built on OOP principles


What's Next?

In Lesson 2, we'll:

  • Write our first Expense class
  • Create actual expense objects
  • Learn about constructors
  • Add methods to print expense details
  • See our code actually work!

Time to get hands-on and write real code! πŸš€


Need Help?

Common questions beginners ask:

Q: Do I need to memorize all the OOP terms?

A: No! Focus on understanding the concepts. The terms will become natural as you use them.

Q: Is OOP hard?

A: It might feel strange at first, but by building a real app, you'll see why it's actually easier than the alternative!

Q: Can I skip OOP and just learn Flutter?

A: Flutter IS built with OOP! Every widget is a class. Understanding OOP will make Flutter much easier.

Q: What if I get stuck?

A: That's normal! Re-read sections, try the examples, and remember - we're building this step by step together.


Ready for Lesson 2? Let's write some code! β†’

Top comments (0)