Commit 29b8119
committed
feat: Add Calculate class for product price computation with user input
WHAT the code does:
- Defines an `Item` class with `main()` entry point.
- Creates a `Calculate` class that:
- Reads product price (`p`) and quantity (`q`) via `Scanner`.
- Multiplies them to get total (`p * q`).
- Prints the computed total cost.
WHY this matters:
- Demonstrates **basic class separation** in Java:
- `Item` handles execution entry.
- `Calculate` encapsulates business logic (data input, calculation, output).
- Shows beginner-friendly use of **Scanner**, encapsulation, and simple arithmetic.
- Useful as a stepping stone into structured, object-oriented Java programs.
HOW it works:
1. Program starts in `Item.main()`.
2. Instantiates `Calculate` object.
3. Calls methods in sequence:
- `getData()` → reads price and quantity.
- `Result()` → multiplies values to compute total.
- `Show()` → prints the total.
Tips & gotchas:
- It’s good practice to close the `Scanner` after input (`sc.close()`).
- Currently no input validation (negative values allowed).
- Logic is tied to console I/O; separating calculation from input/output would improve testability.
Use-cases:
- Small utility programs (shopping cart total, billing).
- Teaching tool for methods, objects, and **class design** in Java.
- Basis for extending into discount logic, multiple items, or tax computation.
Short key: class-calculate product-price-scanner.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 27b46fa commit 29b8119
1 file changed
+9
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 3 | + | |
| 4 | + | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
14 | | - | |
15 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
16 | 15 | | |
17 | | - | |
18 | | - | |
| 16 | + | |
19 | 17 | | |
20 | 18 | | |
21 | 19 | | |
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
25 | 23 | | |
26 | | - | |
27 | | - | |
| 24 | + | |
28 | 25 | | |
29 | 26 | | |
30 | 27 | | |
31 | | - | |
32 | | - | |
| 28 | + | |
33 | 29 | | |
34 | 30 | | |
35 | | - | |
| 31 | + | |
0 commit comments