-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetailStoreApp.java
More file actions
152 lines (127 loc) · 4.17 KB
/
RetailStoreApp.java
File metadata and controls
152 lines (127 loc) · 4.17 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.ArrayList;
import java.util.List;
// Base class for products
class Product {
private String name;
private double price;
private int quantity;
// Constructor
public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
// Getters and Setters (Encapsulation)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
// Method to display product details (Polymorphism)
public void displayProductDetails() {
System.out.println("Name: " + name);
System.out.println("Price: $" + price);
System.out.println("Quantity: " + quantity);
}
}
// Subclass for Fruits
class Fruit extends Product {
public Fruit(String name, double price, int quantity) {
super(name, price, quantity);
}
}
// Subclass for Vegetables
class Vegetable extends Product {
public Vegetable(String name, double price, int quantity) {
super(name, price, quantity);
}
}
// Subclass for Grocery
class Grocery extends Product {
public Grocery(String name, double price, int quantity) {
super(name, price, quantity);
}
}
// RetailStore class to manage products
class RetailStore {
private List<Product> products;
// Constructor to initialize the list
public RetailStore() {
products = new ArrayList<>();
}
// Method to add a product
public void addProduct(Product product) {
products.add(product);
}
// Method to edit a product
public void editProduct(String name, double newPrice, int newQuantity) {
for (Product product : products) {
if (product.getName().equals(name)) {
product.setPrice(newPrice);
product.setQuantity(newQuantity);
System.out.println(name + " has been updated.");
return;
}
}
System.out.println("Product not found.");
}
// Method to delete a product
public void deleteProduct(String name) {
for (Product product : products) {
if (product.getName().equals(name)) {
products.remove(product);
System.out.println(name + " has been deleted.");
return;
}
}
System.out.println("Product not found.");
}
// Method to display all available products
public void displayAllProducts() {
if (products.isEmpty()) {
System.out.println("No products available.");
} else {
System.out.println("Available Products:");
for (Product product : products) {
product.displayProductDetails();
System.out.println("------------------------");
}
}
}
}
// Main class to test the implementation
public class RetailStoreApp {
public static void main(String[] args) {
// Creating a retail store object
RetailStore store = new RetailStore();
// Adding products to the store
store.addProduct(new Fruit("Apple", 1.2, 50));
store.addProduct(new Vegetable("Carrot", 0.8, 100));
store.addProduct(new Grocery("Rice", 2.5, 200));
// Displaying available products
store.displayAllProducts();
// Editing a product
store.editProduct("Apple", 1.5, 60);
// Displaying products after editing
System.out.println("\nAfter Editing Apple Product:");
store.displayAllProducts();
// Deleting a product
store.deleteProduct("Carrot");
// Displaying products after deletion
System.out.println("\nAfter Deleting Carrot Product:");
store.displayAllProducts();
}
}