-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryManagementSystem.java
More file actions
234 lines (170 loc) · 6 KB
/
InventoryManagementSystem.java
File metadata and controls
234 lines (170 loc) · 6 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
import java.io.*;
import java.util.*;
//Creating a Product Class
class Product {
private int id;
private String name;
private int quantity;
private double price;
// Constructor to initialize values
public Product(int id, String name, int quantity, double price) {
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
}
//gets ID of the product
public int getId() {
return id;
}
//Calculates value of the stock using quantity and price
public double getStockValue() {
return quantity * price;
}
public String toFileString() {
return id + "," + name + "," + quantity + "," + price;
}
public void display() {
System.out.println("--------------------------------");
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Quantity: " + quantity);
System.out.println("Price: " + price);
System.out.println("Stock Value: " + getStockValue());
}
}
// Main Class
public class InventoryManagementSystem {
static ArrayList<Product> products = new ArrayList<>();
static final String FILE_NAME = "products.txt";
// Add Products to the array
public static void addProduct(Scanner sc) {
try {
System.out.print("Enter Product ID: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Enter Product Name: ");
String name = sc.nextLine();
System.out.print("Enter Quantity: ");
int quantity = sc.nextInt();
System.out.print("Enter Price: ");
double price = sc.nextDouble();
Product p = new Product(id, name, quantity, price);
products.add(p);
System.out.println("Product added successfully.");
} catch (Exception e) {
System.out.println("Invalid input. Try again.");
sc.nextLine();
}
}
// Display Products present in the array
public static void displayProducts() {
if (products.isEmpty()) {
System.out.println("No products available.");
return;
}
for (Product p : products) {
p.display();
}
}
// Search for Product using ProductID
public static void searchProduct(Scanner sc) {
System.out.print("Enter Product ID to Search: ");
int id = sc.nextInt();
for (Product p : products) {
if (p.getId() == id) {
System.out.println("Product found:");
p.display();
return;
}
}
System.out.println("Product not found in the list.");
}
// Calculate Total Stock Value of all the entered items
public static void calculateTotalValue() {
double total = 0;
for (Product p : products) {
total += p.getStockValue();
}
System.out.println("Total Stock Value: " + total);
}
// Save to File
public static void saveToFile() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME));
for (Product p : products) {
writer.write(p.toFileString());
writer.newLine();
}
writer.close();
System.out.println("Products saved to file successfully.");
} catch (IOException e) {
System.out.println("Error saving file.Try again.");
}
}
// Load from File
// To get values of previously entered Products from the file products.txt
public static void loadFromFile() {
try {
File file = new File(FILE_NAME);
if (!file.exists()) {
return;
}
BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME));
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
int id = Integer.parseInt(data[0]);
String name = data[1];
int quantity = Integer.parseInt(data[2]);
double price = Double.parseDouble(data[3]);
Product p = new Product(id, name, quantity, price);
products.add(p);
}
reader.close();
System.out.println("Products loaded from file successfully.");
} catch (IOException e) {
System.out.println("Error loading file.Try again.");
}
}
// Main Method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Load existing products when program starts, only if the file exists
loadFromFile();
while (true) {
System.out.println("\n===== Inventory Management System =====");
System.out.println("1. Add Product");
System.out.println("2. Display Products");
System.out.println("3. Search Product");
System.out.println("4. Calculate Total Stock Value");
System.out.println("5. Save Products to File");
System.out.println("6. Exit");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
addProduct(sc);
break;
case 2:
displayProducts();
break;
case 3:
searchProduct(sc);
break;
case 4:
calculateTotalValue();
break;
case 5:
saveToFile();
break;
case 6:
System.out.println("Exiting program...");
sc.close();
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
}