-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStore Management System.java
More file actions
245 lines (210 loc) · 6.68 KB
/
Store Management System.java
File metadata and controls
245 lines (210 loc) · 6.68 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//importing java libraries
import java.util.ArrayList;
import java.util.Scanner;
/**
* Item class represents a product in the store with all required attributes
*/
class Item {
// Private fields for encapsulation
private String name;
private String vendor;
private double price;
private double cost;
private double weight;
private boolean taxable;
/**
* Constructor that accepts name, cost, and price
*/
public Item(String name, double cost, double price) {
this.name = name;
this.cost = cost;
this.price = price;
// Default values for other attributes
this.vendor = "Unknown";
this.weight = 0.0;
this.taxable = false;
}
// Getter and setter methods for all attributes
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public boolean isTaxable() {
return taxable;
}
public void setTaxable(boolean taxable) {
this.taxable = taxable;
}
/**
* Calculates the profit margin (price - cost)
* @return Profit margin amount
*/
public double calculateProfitMargin() {
return price - cost;
}
}
/**
* ShoppingCart class manages a collection of items using aggregation
*/
class ShoppingCart {
// Using ArrayList to store items (aggregation)
private ArrayList<Item> items;
public ShoppingCart() {
items = new ArrayList<>();
}
/**
* Adds an item to the shopping cart
* @param item Item to be added
*/
public void addItem(Item item) {
items.add(item);
System.out.println(item.getName() + " added to cart.");
}
/**
* Removes an item from the shopping cart by name
* @param itemName Name of item to remove
*/
public void removeItem(String itemName) {
for (Item item : items) {
if (item.getName().equalsIgnoreCase(itemName)) {
items.remove(item);
System.out.println(itemName + " removed from cart.");
return;
}
}
System.out.println("Item not found in cart.");
}
/**
* Calculates total number of items in cart
* @return Total item count
*/
public int getTotalItems() {
return items.size();
}
/**
* Calculates total cost including tax where applicable
* @return Total cost with tax
*/
public double getTotalCost() {
double total = 0;
for (Item item : items) {
if (item.isTaxable()) {
total += item.getPrice() * 1.10; // Add 10% tax
} else {
total += item.getPrice();
}
}
return total;
}
/**
* Calculates total weight of all items
* @return Total weight
*/
public double getTotalWeight() {
double total = 0;
for (Item item : items) {
total += item.getWeight();
}
return total;
}
/**
* Prints a receipt showing all items with details
*/
public void printReceipt() {
System.out.println("\n=== STORE RECEIPT ===");
System.out.println("ITEM\t\tPRICE\tTAX");
System.out.println("----------------------------");
for (Item item : items) {
System.out.printf("%-10s\t$%.2f\t%s\n",
item.getName(),
item.getPrice(),
item.isTaxable() ? "Yes (10%)" : "No");
}
System.out.println("----------------------------");
System.out.printf("TOTAL ITEMS: %d\n", getTotalItems());
System.out.printf("TOTAL WEIGHT: %.2f kg\n", getTotalWeight());
System.out.printf("TOTAL COST: $%.2f\n", getTotalCost());
System.out.println("============================");
}
}
/**
* Main class to demonstrate the Store Management System
*/
// ... (previous Item and ShoppingCart classes remain the same)
public class StoreManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ShoppingCart cart = new ShoppingCart();
System.out.println("=== STORE MANAGEMENT SYSTEM ===");
// Create some sample items
Item item1 = new Item("Laptop", 800, 1000);
item1.setWeight(2.5);
item1.setTaxable(true);
Item item2 = new Item("Mouse", 10, 15);
item2.setWeight(0.2);
Item item3 = new Item("Notebook", 2, 5);
item3.setWeight(0.3);
item3.setTaxable(true);
// Simple menu system with error handling
while (true) {
System.out.println("\n1. Add sample items to cart");
System.out.println("2. View receipt");
System.out.println("3. Exit");
System.out.print("Enter your choice (1-3): ");
//Exception handing with Try catch blocks
try {
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
cart.addItem(item1);
cart.addItem(item2);
cart.addItem(item3);
break;
case 2:
cart.printReceipt();
break;
case 3:
System.out.println("Thank you for using the system!");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please enter 1, 2, or 3.");
}
} catch (java.util.InputMismatchException e) { //Input mismatch exception checks datatype erorr on user input
System.out.println("Invalid input! Please enter a number (1, 2, or 3).");
scanner.nextLine(); // Clear the invalid input
}
//runs the print statement whether the condition is true or false
finally {
System.out.println("Thank you for purshasing these items....");
}
}
}
}