Commit 654e99b
committed
feat: Add demo output for makeItACombo in Meal class
WHAT the code does:
- Defines a `Meal` class with attributes `name` and `cost`.
- Constructor initializes the meal with a food name and price.
- `makeItACombo()` upgrades a meal to a combo by:
- Adding ₹50.0 to cost.
- Appending `" Combo"` to the name.
- `getBill()` returns the final bill amount.
- Overrides `toString()` for formatted string output.
- `main()` tests functionality by:
1. Printing initial meal.
2. Applying `makeItACombo()` and printing updated meal with a label.
3. Printing final bill.
WHY this matters:
- Demonstrates how to model **real-world objects** with Java classes.
- Highlights method-driven state change (combo upgrade).
- Shows usage of `toString()` for human-readable output.
- Reinforces the principle of encapsulation with private fields.
HOW it works:
1. `Meal m1 = new Meal("Burger", 120)` → creates meal.
2. `System.out.println(m1)` → invokes `toString()`.
3. `m1.makeItACombo()` → cost increases to ₹170, name becomes `"Burger Combo"`.
4. Prints `"makeItAComboBurger Combo costs ₹170.0"`.
5. `getBill()` → returns final cost `170.0`.
Tips & gotchas:
- Each call to `makeItACombo()` keeps stacking ₹50 → may not be desired.
- Hardcoded ₹50 could be replaced with a configurable combo charge.
- For money, prefer `BigDecimal` to avoid floating-point inaccuracies.
- `System.out.println("makeItACombo"+m1);` concatenates label directly with meal info — spacing could be improved for clarity.
Use-cases:
- Educational Java OOP practice.
- Simulating POS (Point of Sale) billing systems.
- Starter example for **mutable object state**.
- Base for extending features like discounts, taxes, or menus.
Short key: class-meal-combo-upgrade-demo
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 324b5a7 commit 654e99b
1 file changed
+32
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
14 | | - | |
15 | | - | |
16 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
17 | 18 | | |
18 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
0 commit comments