Skip to content

Commit f5a6ca4

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

1 file changed

+33
-38
lines changed

Section11ObjectOrientedProgramming/src/ProductAndCostomer.java

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,81 @@
11
/*
22
Product: itemno, name, price, qty 4 properties here.
3-
you should write properties method get and set method you have write down methods.
3+
you should write properties method get and set method you have writedown methods.
44
and now ypu have to write constructors.
55
Customer: custID, name, address, phno.
6-
What is data type of itemNo, this is alpha numeric and this sholud be string or int. Ex: A25 , this should be string.
6+
What is a data type of itemNo, this is an alphanumeric, and this should be string or int. Ex: A25, this should be string.
77
you also takr long data type.
8-
price double and name is string and Quantity in shop may be short or long byte what ever you required decide data type proper.
8+
9+
price double and name is string and Quantity in shop may be short or long byte whatever you required decide data type proper.
910
Properties methods: you have to write get method. String getItemNo, String getName, like this...
10-
setMethod: set method means it change the propertie of item can i have setItemNo. setItemNO(String itemNo) we can not change itemNo everytime.
11-
setItemNo should not be allowd. it should be in constructer.
12-
setName also not allowd.
13-
setPrice allowd beacuse it changes frequently you write down those
14-
setQuanttity allowd. it changes frequently.
1511
16-
Now Constructer, if you have construction product class it must take itemnumber. there is no non parameterized constucter.
17-
Product(String itemNo , String name)
12+
setMethod: set method means it change the propertie of item can i have setItemNo.
13+
setItemNO(String itemNo) we can not change itemNo everytime.
14+
setItemNo should not be allowd. it should be in constructer.
15+
setName also aren't allowed.
16+
setPrice allowed because it changes frequently you write down those
17+
setQuantity allowed. it changes frequently.
1818
19+
Now Constructor, if you have construction product class it must take itemnumber. there is no non-parameterized constructor.
20+
Product (String itemNo , String name)
1921
2022
Class Customer: CustomerID, name, address, PhoneNumber, now decide data type, properties method and constructers.
2123
like what we do in product class.
22-
CustomerId: String is ok, name string , phoneNo: String because +91 we added , all thing are Strings.
23-
Propertie methods are: all of them are get method and set method can not modified we gave at the time of registration and we cant change after that.
24+
CustomerId: String is ok, name string , phoneNo: String because +91 we added, all things are Strings.
25+
Propertie methods are: all of them are get method and set method cannot modify we gave it at the time of registration, and we can't change after that.
2426
we change only addresss, so setAddress. setPhoneNumber.
25-
Constructers; Customer(c.id, name) we cannot create customer id without mentioning his customerid and name. so there is no parameterized constructer here.
27+
Constructers; Customer(c.id, name) we cannot create a customer id without mentioning his customerid and name.
28+
so there is no parameterized constructor here.
2629
2730
This is a Java program that defines two classes,
2831
Product and Customer, and then takes input from the user to create objects of these classes and display their details.*/
2932

3033
import java.util.Scanner;
3134

32-
class product
33-
{
35+
class product {
3436
private String itemNo;
3537
private String name;
3638
private double price;
3739
private short qty;
3840

39-
public product(String itemNo)
40-
{
41+
public product(String itemNo) {
4142
this.itemNo = itemNo;
4243
}
4344

44-
public product(String itemNo, String name)
45-
{
45+
public product(String itemNo, String name) {
4646
this.itemNo = itemNo;
4747
this.name = name;
4848
}
49-
public product(String itemNo, String name, double price, short qty)
50-
{
49+
50+
public product(String itemNo, String name, double price, short qty) {
5151
this.itemNo=itemNo;
5252
this.name=name;
5353
setPrice(price);
5454
setQuantity(qty);
5555
}
56+
5657
public String getItemNo(){return itemNo;}
5758
public String getName(){return name;}
5859
public double getPrice(){return price;}
5960
public short getQuantity(){return qty;}
6061

61-
public void setPrice(double price)
62-
{
62+
public void setPrice(double price) {
6363
this.price=price;
6464
}
65-
public void setQuantity(short qty)
66-
{
65+
66+
public void setQuantity(short qty) {
6767
this.qty=qty;
6868
}
6969
}
7070

7171
//Now for Customer
72-
73-
class Customer
74-
{
72+
class Customer {
7573
private String custID;
7674
private String name;
7775
private String address;
7876
private String phno;
7977

80-
public Customer(String custId, String name)
81-
{
78+
public Customer(String custId, String name) {
8279
this.custID = custId; // ✅ Corrected assignment
8380
this.name = name; // ✅ Corrected assignment
8481
this.address = ""; // Default value (can be changed later)
@@ -93,16 +90,14 @@ public Customer(String custId, String name)
9390
public void setAddress(String address){
9491
this.address=address;
9592
}
96-
public void setPhno(String phno)
97-
{
93+
94+
public void setPhno(String phno) {
9895
this.phno=phno;
9996
}
10097
}
10198

102-
public class ProductAndCostomer
103-
{
104-
public static void main(String[] args)
105-
{
99+
public class ProductAndCostomer {
100+
public static void main(String[] args) {
106101
Scanner sc = new Scanner(System.in);
107102

108103
// Taking product details
@@ -176,7 +171,7 @@ Only price and qty can be modified after creation (via setter methods).
176171
Only address and phno can be changed after registration.
177172
178173
Constructors:
179-
Customer(String custID, String name) → Requires a customer ID and name.
174+
Customer (String custID, String name) → Requires a customer ID and name.
180175
ProductAndCustomer (Main Class)
181176
182177
Uses Scanner to take user input.
@@ -190,4 +185,4 @@ Prompts the user for customer details (custID, name, address, phno).
190185
✅ Shows constructors for object initialization.
191186
✅ Implements user input handling with Scanner.
192187
✅ Practices object-oriented programming (OOP) in Java.
193-
*/
188+
*/

0 commit comments

Comments
 (0)