-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShoppingSystem.java
More file actions
65 lines (58 loc) · 1.82 KB
/
ShoppingSystem.java
File metadata and controls
65 lines (58 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Product {
// Static variable
private static double discount = 10.0; // Shared among all products
// Instance Variables
private final int productID; // Final Variable
private String productName;
private double price;
private int quantity;
// Constructor
Product(int productID, String productName, double price, int quantity) {
this.productID = productID;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
// Method to display product details
public void displayDetails() {
if (this instanceof Product) {
System.out.println("Product ID: " + productID);
System.out.println("Product Name: " + productName);
System.out.println("Price: " + price);
System.out.println("Quantity: " + quantity);
System.out.println("Discount: " + discount + "%");
System.out.println();
}
}
// Method to update discount
public static void updateDiscount(double discount) {
Product.discount = discount;
}
}
public class ShoppingSystem {
public static void main(String[] args) {
// Create objects of product class
Product p1 = new Product(101, "Laptop", 100000, 10);
Product p2 = new Product(102, "Mobile", 50000, 20);
// Display product details
p1.displayDetails();
p2.displayDetails();
// Method to update discount percentage
Product.updateDiscount(15);
System.out.println("Discount percentage updated successfully");
}
}
// Sample Output ->
//Product ID: 101
//Product Name: Laptop
//Price: 100000.0
//Quantity: 10
//Discount: 10.0%
//
//Product ID: 102
//Product Name: Mobile
//Price: 50000.0
//Quantity: 20
//Discount: 10.0%
//
//Discount percentage updated successfully