Commit f5a6ca4
committed
feat: Add Product and Customer classes with encapsulation and interactive input
WHAT the code does:
Defines product class with private fields:
- itemNo, name, price, and quantity.
- Provides overloaded constructors for partial and full initialization.
- Getters and setters provide controlled access.
Defines Customer class with private fields:
- custID, name, address, and phone number.
- Constructor initializes ID and name, with defaults for address and phone.
- Getters and setters allow controlled updates.
Defines ProductAndCostomer with main():
- Uses Scanner to take user input for product and customer details.
- Creates product and customer objects with the given values.
- Displays stored details for both objects.
WHY this matters:
Demonstrates encapsulation by making fields private and exposing only controlled access.
Shows constructor overloading to support multiple initialization scenarios.
Illustrates interactive object creation using Scanner for input.
Models a simple real-world relationship: customer purchasing a product.
HOW it works:
User is prompted for product details (item number, name, price, quantity).
A product object is created using the full constructor.
User is prompted for customer details (ID, name, address, phone).
A customer object is created and populated via constructor and setters.
Program prints product and customer details via getters.
Tips and gotchas:
Class names should follow Java conventions: Product (instead of product) and ProductAndCustomer (instead of ProductAndCostomer).
Validation logic is missing: setters currently allow negative price/quantity; adding checks would improve robustness.
Default values in Customer (empty string for address and phone) are fine for placeholders but could be null-checked in practice.
toString() overrides for Product and Customer would simplify printing object state.
Use-cases:
Educational example of constructors, encapsulation, and object composition.
Simple model of product–customer interaction for retail or billing systems.
Foundation for extending into order management or inventory tracking applications.
Short key: class-product customer encapsulation constructor-overloading scanner-input.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9e15c93 commit f5a6ca4
File tree
1 file changed
+33
-38
lines changed- Section11ObjectOrientedProgramming/src
1 file changed
+33
-38
lines changedLines changed: 33 additions & 38 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
9 | 10 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | 11 | | |
16 | | - | |
17 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
22 | | - | |
23 | | - | |
| 24 | + | |
| 25 | + | |
24 | 26 | | |
25 | | - | |
| 27 | + | |
| 28 | + | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
32 | | - | |
33 | | - | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
39 | | - | |
40 | | - | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | | - | |
| 49 | + | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
61 | | - | |
62 | | - | |
| 62 | + | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
66 | | - | |
| 65 | + | |
| 66 | + | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
73 | | - | |
74 | | - | |
| 72 | + | |
75 | 73 | | |
76 | 74 | | |
77 | 75 | | |
78 | 76 | | |
79 | 77 | | |
80 | | - | |
81 | | - | |
| 78 | + | |
82 | 79 | | |
83 | 80 | | |
84 | 81 | | |
| |||
93 | 90 | | |
94 | 91 | | |
95 | 92 | | |
96 | | - | |
97 | | - | |
| 93 | + | |
| 94 | + | |
98 | 95 | | |
99 | 96 | | |
100 | 97 | | |
101 | 98 | | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
| 99 | + | |
| 100 | + | |
106 | 101 | | |
107 | 102 | | |
108 | 103 | | |
| |||
176 | 171 | | |
177 | 172 | | |
178 | 173 | | |
179 | | - | |
| 174 | + | |
180 | 175 | | |
181 | 176 | | |
182 | 177 | | |
| |||
190 | 185 | | |
191 | 186 | | |
192 | 187 | | |
193 | | - | |
| 188 | + | |
0 commit comments