Skip to content

Commit 29b8119

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

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

Section10Methods/src/Item.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
import java.util.Scanner;
22

3-
class Item
4-
{
5-
public static void main(String[] args)
6-
{
3+
class Item {
4+
public static void main(String[] args) {
75
Calculate c = new Calculate();
86
c.getData();
97
c.Result();
108
c.Show();
119
}
1210
}
1311

14-
class Calculate
15-
{
12+
13+
14+
class Calculate {
1615
int p, q, total;
17-
void getData()
18-
{
16+
void getData() {
1917
Scanner sc = new Scanner(System.in);
2018
System.out.println("Enter the price of product:");
2119
p = sc.nextInt();
2220
System.out.println("Enter the quantity of the product:");
2321
q = sc.nextInt();
2422
}
2523

26-
void Result()
27-
{
24+
void Result() {
2825
total = p*q;
2926
}
3027

31-
void Show()
32-
{
28+
void Show() {
3329
System.out.println("Total price is: "+total);
3430
}
35-
}
31+
}

0 commit comments

Comments
 (0)