-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryManagementSystem.java
More file actions
209 lines (180 loc) · 6.48 KB
/
Copy pathInventoryManagementSystem.java
File metadata and controls
209 lines (180 loc) · 6.48 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
// Class representing an Inventory Item
class Item {
String itemName;
int itemID;
int quantity;
double price;
Item next; // Pointer to next item
// Constructor to initialize item details
public Item(String itemName, int itemID, int quantity, double price) {
this.itemName = itemName;
this.itemID = itemID;
this.quantity = quantity;
this.price = price;
this.next = null;
}
}
// Class to manage the Inventory as a Singly Linked List
class Inventory {
private Item head; // Head of the linked list
// Method to add an item at the beginning
public void addItemAtBeginning(String itemName, int itemID, int quantity, double price) {
Item newItem = new Item(itemName, itemID, quantity, price);
newItem.next = head;
head = newItem;
}
// Method to add an item at the end
public void addItemAtEnd(String itemName, int itemID, int quantity, double price) {
Item newItem = new Item(itemName, itemID, quantity, price);
if (head == null) {
head = newItem;
return;
}
Item temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newItem;
}
// Method to add an item at a specific position
public void addItemAtPosition(String itemName, int itemID, int quantity, double price, int position) {
if (position <= 0) {
System.out.println("Invalid position.");
return;
}
Item newItem = new Item(itemName, itemID, quantity, price);
if (position == 1) {
newItem.next = head;
head = newItem;
return;
}
Item temp = head;
for (int i = 1; i < position - 1 && temp != null; i++) {
temp = temp.next;
}
if (temp == null) {
System.out.println("Position out of range.");
return;
}
newItem.next = temp.next;
temp.next = newItem;
}
// Method to remove an item based on Item ID
public void removeItem(int itemID) {
if (head == null) {
System.out.println("Inventory is empty.");
return;
}
if (head.itemID == itemID) {
head = head.next;
return;
}
Item temp = head;
while (temp.next != null && temp.next.itemID != itemID) {
temp = temp.next;
}
if (temp.next == null) {
System.out.println("Item with ID " + itemID + " not found.");
return;
}
temp.next = temp.next.next;
}
// Method to update the quantity of an item by Item ID
public void updateItemQuantity(int itemID, int newQuantity) {
Item temp = head;
while (temp != null) {
if (temp.itemID == itemID) {
temp.quantity = newQuantity;
System.out.println("Updated quantity for Item ID " + itemID + " to " + newQuantity);
return;
}
temp = temp.next;
}
System.out.println("Item with ID " + itemID + " not found.");
}
// Method to search for an item by Item ID
public void searchItemByID(int itemID) {
Item temp = head;
while (temp != null) {
if (temp.itemID == itemID) {
System.out.println("Item Found: " + temp.itemName + " | ID: " + temp.itemID + " | Quantity: " + temp.quantity + " | Price: " + temp.price);
return;
}
temp = temp.next;
}
System.out.println("Item with ID " + itemID + " not found.");
}
// Method to search for an item by Item Name
public void searchItemByName(String itemName) {
Item temp = head;
while (temp != null) {
if (temp.itemName.equalsIgnoreCase(itemName)) {
System.out.println("Item Found: " + temp.itemName + " | ID: " + temp.itemID + " | Quantity: " + temp.quantity + " | Price: " + temp.price);
return;
}
temp = temp.next;
}
System.out.println("Item with Name '" + itemName + "' not found.");
}
// Method to calculate the total inventory value
public void calculateTotalValue() {
double totalValue = 0;
Item temp = head;
while (temp != null) {
totalValue += temp.price * temp.quantity;
temp = temp.next;
}
System.out.println("Total Inventory Value: " + totalValue);
}
// Method to display the inventory list
public void displayInventory() {
if (head == null) {
System.out.println("Inventory is empty.");
return;
}
System.out.println("Inventory List:");
Item temp = head;
while (temp != null) {
System.out.println(temp.itemName + " | ID: " + temp.itemID + " | Quantity: " + temp.quantity + " | Price: " + temp.price);
temp = temp.next;
}
}
}
// Main class to run the program
public class InventoryManagementSystem {
public static void main(String[] args) {
Inventory inventory = new Inventory();
// Adding items to the inventory
inventory.addItemAtEnd("Laptop", 1010, 5, 50000);
inventory.addItemAtEnd("Mouse", 1020, 20, 500);
inventory.addItemAtBeginning("Keyboard", 1030, 15, 900);
inventory.addItemAtPosition("Monitor", 1040, 10, 15000, 2);
// Displaying inventory
inventory.displayInventory();
// Searching for an item
inventory.searchItemByID(1020);
inventory.searchItemByName("Monitor");
// Updating quantity
inventory.updateItemQuantity(1010, 7);
// Calculating total inventory value
inventory.calculateTotalValue();
// Removing an item
inventory.removeItem(1030);
// Displaying inventory after removal
inventory.displayInventory();
}
}
//SampleOutput
//Inventory List:
//Keyboard | ID: 1030 | Quantity: 15 | Price: 900.0
//Monitor | ID: 1040 | Quantity: 10 | Price: 15000.0
//Laptop | ID: 1010 | Quantity: 5 | Price: 50000.0
//Mouse | ID: 1020 | Quantity: 20 | Price: 500.0
//Item Found: Mouse | ID: 1020 | Quantity: 20 | Price: 500.0
//Item Found: Monitor | ID: 1040 | Quantity: 10 | Price: 15000.0
//Updated quantity for Item ID 1010 to 7
//Total Inventory Value: 523500.0
//Inventory List:
//Monitor | ID: 1040 | Quantity: 10 | Price: 15000.0
//Laptop | ID: 1010 | Quantity: 7 | Price: 50000.0
//Mouse | ID: 1020 | Quantity: 20 | Price: 500.0