Skip to content

Commit 654e99b

Browse files
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

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed
Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1-
public class Meal
2-
{
3-
//Instance attributes//
1+
public class Meal {
2+
// Instance attributes
43
private String name;
54
private double cost;
65

7-
//Class constructor//
8-
public Meal(String food, double price)
9-
{
10-
name = food;
11-
cost = price;
6+
// Class constructor
7+
public Meal(String food, double price) {
8+
this.name = food;
9+
this.cost = price;
1210
}
1311

14-
//Instance methods//
15-
/* Complete the method makeItACombo */
16-
}
12+
// Instance method: upgrade meal to combo (adds fixed extra cost)
13+
public void makeItACombo() {
14+
// Let's say making it a combo adds ₹50.0
15+
this.cost += 50.0;
16+
this.name += " Combo";
17+
}
1718

18-
/* Complete the method getBill */
19+
// Instance method: return bill amount
20+
public double getBill() {
21+
return this.cost;
22+
}
23+
24+
// toString method for display
25+
@Override
26+
public String toString() {
27+
return name + " costs ₹" + cost;
28+
}
29+
public static void main(String[] args) {
30+
Meal m1 = new Meal("Burger", 120);
31+
System.out.println(m1);
32+
33+
m1.makeItACombo();
34+
System.out.println("makeItACombo"+m1);
35+
36+
System.out.println("Bill: ₹" + m1.getBill());
37+
}
38+
}

0 commit comments

Comments
 (0)