-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPurchaseOrderCSVReader.java
More file actions
85 lines (75 loc) · 3.13 KB
/
Copy pathPurchaseOrderCSVReader.java
File metadata and controls
85 lines (75 loc) · 3.13 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
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
public class PurchaseOrderCSVReader {
String[] header;
HashMap<String, PurchaseOrderItem> ordersBySku;
public PurchaseOrderCSVReader(String filenameString, String pathString) {
this.ordersBySku = new HashMap<String, PurchaseOrderItem>();
readPurchaseOrderFile(filenameString, pathString);
}
public void readPurchaseOrderFile(String filename, String pathString) {
try (
Reader reader = Files.newBufferedReader(Paths.get(pathString, filename));
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
) {
Iterator<CSVRecord> it = csvParser.iterator();
CSVRecord csvRecord;
// Grab the header
if (it.hasNext()) {
csvRecord = it.next();
Iterable<String> source = csvRecord;
ArrayList<String> target = new ArrayList<String>();
source.forEach(target::add);
header = new String[csvRecord.size()];
header = target.toArray(header);
System.out.println("Found PO Header\n"+ Arrays.toString(header));
}
while (it.hasNext()) {
csvRecord = it.next();
// Accessing SKU and Actual by Column Index
PurchaseOrderItem item = new PurchaseOrderItem();
item.setItemName(csvRecord.get(0));
item.setVariationName(csvRecord.get(1));
item.setSkuString(csvRecord.get(2));
item.setGtin(csvRecord.get(3));
item.setVendorCode(csvRecord.get(4));
item.setNotes(csvRecord.get(5));
item.setQty(csvRecord.get(6));
item.setUnitCost(csvRecord.get(7));
ordersBySku.put(item.getSkuString(), item);
}
} catch(Exception e) {
System.out.println("An exception was thrown while reading Inventory Item Counts. File: "+ filename);
System.out.println(e.getMessage());
}
}
public HashMap<String, PurchaseOrderItem> getOrdersBySku() {
return ordersBySku;
}
public String[] getHeader() {
return header;
}
// Unit Test
public static void main(String[] args) throws IOException {
final String pathString = ".";
final String filename = "test-purchaseorder.csv";
PurchaseOrderCSVReader reader = new PurchaseOrderCSVReader(filename, pathString);
HashMap<String, PurchaseOrderItem> items = reader.getOrdersBySku();
int count = items.size();
if (count == 5) {
System.out.println("Succes: "+filename+" loaded successfully");
System.exit(0);
}
System.out.println("Error reading "+filename+": Expected 5 items and saw "+count);
System.exit(1);
}
}