-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.java
More file actions
342 lines (276 loc) · 13.2 KB
/
Copy pathinventory.java
File metadata and controls
342 lines (276 loc) · 13.2 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package com.example.fxpartcw;
import javafx.scene.control.Alert;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
public class inventory {
static final String finalFilePathway = System.getProperty("user.dir") +
"/src/main/resources/com/example/fxpartcw/itemDetails.txt";
private int itemCode;
private String itemName;
private String itemBrand;
private double itemPrice;
private int itemQuantity;
private String itemCategory;
private String itemPurchasedDate;
public int getItemCode() {
return itemCode;
}
public String getItemName() {
return itemName;
}
public String getItemBrand() {
return itemBrand;
}
public double getItemPrice() {
return itemPrice;
}
public int getItemQuantity() {
return itemQuantity;
}
public String getItemCategory() {
return itemCategory;
}
public String getItemPurchasedDate() {
return itemPurchasedDate;
}
public void setItemCode(int itemCode) {this.itemCode = itemCode;}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public void setItemBrand(String itemBrand) {
this.itemBrand = itemBrand;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public void setItemQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
public void setItemCategory(String itemCategory){this.itemCategory = itemCategory;}
public void setItemPurchasedDate(String itemPurchasedDate) {
this.itemPurchasedDate = itemPurchasedDate;
}
// making a hashmap and adding item details to a hashmap
static HashMap<String,String> addingItemDetailsMap = new HashMap<>();
public static ArrayList<HashMap<String, String>> readData(){
String line;
ArrayList<HashMap<String,String>> arrayListOfTheItemDetailsMap = new ArrayList<>();
try(BufferedReader reader = new BufferedReader(new FileReader(filePathway))) {
while ((line = reader.readLine()) != null){
HashMap<String,String> hashMap = parseLineToHashMap(line);
if (hashMap != null){
arrayListOfTheItemDetailsMap.add(hashMap);
}
}
} catch (IOException e){
e.printStackTrace();
}
return arrayListOfTheItemDetailsMap;
}
public void addItemsToHashMap(){
addingItemDetailsMap.put("itemCode" , String.valueOf(itemCode));
addingItemDetailsMap.put("itemName" , itemName);
addingItemDetailsMap.put("itemBrand" , itemBrand);
addingItemDetailsMap.put("itemPrice" , String.valueOf(itemPrice));
addingItemDetailsMap.put("itemQuantity" , String.valueOf(itemQuantity));
addingItemDetailsMap.put("itemCategory" , itemCategory);
addingItemDetailsMap.put("itemPurchasedDate" , String.valueOf(itemPurchasedDate));
System.out.println(addingItemDetailsMap);
}
static String filePathway = finalFilePathway;
public void saveHashMapToTextFile(){
//writing the details to file
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePathway,true))) {
writer.write(String.valueOf(addingItemDetailsMap));
writer.newLine();
writer.close();
System.out.println("Details saved successfully");
System.out.println(addingItemDetailsMap.entrySet());
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean deleteItemDetails(String itemCode){
String line;
ArrayList<HashMap<String,String>> arrayListOfTheItemDetailsMap = new ArrayList<>();
try(BufferedReader reader = new BufferedReader(new FileReader(filePathway))) {
while ((line = reader.readLine()) != null){
HashMap<String,String> hashMap = parseLineToHashMap(line);
if (hashMap != null){
arrayListOfTheItemDetailsMap.add(hashMap);
}
}
} catch (IOException e){
e.printStackTrace();
}
System.out.println(arrayListOfTheItemDetailsMap);
arrayListOfTheItemDetailsMap.removeIf(itemData -> itemData.get("itemCode").equals(itemCode));
System.out.println(arrayListOfTheItemDetailsMap);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePathway))) {
for (Map<String, String> mapAfterDeleteItemDetails : arrayListOfTheItemDetailsMap){
writer.write(String.valueOf(mapAfterDeleteItemDetails));
writer.newLine();
System.out.println("Data has been written to itemDetails.txt");
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
ArrayList<HashMap<String, String>> arrayListOfDeleteAndSaved = new ArrayList<>();
public void searchForUpdateDetails(String getItemCode, TextField itemCode, TextField itemName, TextField
itemCategory, TextField itemBrand, TextField itemQuantity, TextField itemPrice, DatePicker itemPurchasedDate) {
String line;
try (BufferedReader reader = new BufferedReader(new FileReader(filePathway))) {
while ((line = reader.readLine()) != null) {
HashMap<String, String> hashMap = parseLineToHashMap(line);
if (hashMap != null) {
arrayListOfDeleteAndSaved.add(hashMap);
}
}
boolean itemFound = false;
for (HashMap<String, String> hashMap : arrayListOfDeleteAndSaved) {
if (hashMap.get("itemCode").equals(getItemCode)) {
itemFound = true;
itemCode.setPromptText(hashMap.get("itemCode"));
itemName.setPromptText(hashMap.get("itemName"));
itemBrand.setPromptText(hashMap.get("itemBrand"));
itemPrice.setPromptText(hashMap.get("itemPrice"));
itemQuantity.setPromptText(hashMap.get("itemQuantity"));
itemCategory.setPromptText(hashMap.get("itemCategory"));
itemPurchasedDate.setPromptText(hashMap.get("itemPurchasedDate"));
break; // No need to search further once the item is found
}
}
if (!getItemCode.matches("\\d+")) { // Check if the input is not a number
showAlert("Item code should be an integer.");
} else if (!itemFound) { // Check if the item code is not found in the HashMap
showAlert("Item code not found.");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void showAlert(String message) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Input Error");
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
public void updateData(String getItemCode, TextField itemCode, TextField itemName, TextField
itemCategory, TextField itemBrand, TextField itemQuantity, TextField itemPrice, DatePicker itemPurchasedDate) {
String line;
arrayListOfDeleteAndSaved.clear();
try (BufferedReader reader = new BufferedReader(new FileReader(filePathway))) {
while ((line = reader.readLine()) != null) {
HashMap<String, String> hashMap = parseLineToHashMap(line);
if (hashMap != null) {
arrayListOfDeleteAndSaved.add(hashMap);
}
}
for (HashMap<String, String> hashMap : arrayListOfDeleteAndSaved) {
if (hashMap.get("itemCode").equals(getItemCode)) {
// Validate itemCode
try {
int code = Integer.parseInt(itemCode.getText());
hashMap.put("itemCode", String.valueOf(code));
itemCode.setStyle("");
} catch (NumberFormatException e) {
// Display an alert for invalid itemCode
showAlert("Invalid itemCode! Please enter a valid integer value.");
itemCode.setStyle("-fx-border-color: red;");
return;
}
// Validate itemName, itemCategory, and itemBrand
if (!itemName.getText().matches("[A-Za-z]+")) {
// Display an alert for invalid itemName
showAlert("Invalid itemName! Please enter letters in English alphabet only.");
itemName.setStyle("-fx-border-color: red;");
return;
}
hashMap.put("itemName", itemName.getText());
if (!itemCategory.getText().matches("[A-Za-z]+")) {
// Display an alert for invalid itemCategory
showAlert("Invalid itemCategory! Please enter letters in English alphabet only.");
itemCategory.setStyle("-fx-border-color: red;");
return;
}
hashMap.put("itemCategory", itemCategory.getText());
if (!itemBrand.getText().matches("[A-Za-z]+")) {
// Display an alert for invalid itemBrand
showAlert("Invalid itemBrand! Please enter letters in English alphabet only.");
itemBrand.setStyle("-fx-border-color: red;");
return;
}
hashMap.put("itemBrand", itemBrand.getText());
// Validate itemPrice
try {
double price = Double.parseDouble(itemPrice.getText());
hashMap.put("itemPrice", String.valueOf(price));
itemCode.setStyle("");
} catch (NumberFormatException e) {
// Display an alert for invalid itemPrice
showAlert("Invalid itemPrice! Please enter a valid number.");
itemPrice.setStyle("-fx-border-color: red;");
return;
}
// Validate itemQuantity
try {
int quantity = Integer.parseInt(itemQuantity.getText());
hashMap.put("itemQuantity", String.valueOf(quantity));
itemCode.setStyle("");
} catch (NumberFormatException e) {
// Display an alert for invalid itemQuantity
showAlert("Invalid itemQuantity! Please enter a valid integer value.");
itemQuantity.setStyle("-fx-border-color: red;");
return;
}
// Validate itemPurchasedDate (assuming itemPurchasedDate is of type LocalDate)
if (itemPurchasedDate.getValue() == null) {
// Display an alert for missing itemPurchasedDate
showAlert("Missing itemPurchasedDate! Please select a date.");
return;
}
hashMap.put("itemPurchasedDate", String.valueOf(itemPurchasedDate.getValue()));
System.out.println(hashMap);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePathway))) {
for (Map<String, String> mapAfterDeleteItemDetails : arrayListOfDeleteAndSaved) {
writer.write(String.valueOf(mapAfterDeleteItemDetails));
writer.newLine();
System.out.println("Data has been written to itemDetails.txt");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static HashMap<String, String> parseLineToHashMap(String line) {
HashMap<String,String> hashMap = new HashMap<>();
line = line.replaceAll("[{}]", "");
String[] keyValuePairs = line.split(", ");
for (String keyValuePair : keyValuePairs){
String[] keyValue = keyValuePair.split("=");
if (keyValue.length == 2) {
String key = keyValue[0].trim();
String value = keyValue[1].trim();
hashMap.put(key,value);
} else {
System.out.println("Invalid line format: " + line);
return null;
}
}
return hashMap;
}
}