views:

72

answers:

1

I have two List of array string. I want to be able to create a New List (newList) by combining the 2 lists. But it must meet these 3 conditions:

1) Copy the contents of store_inventory into newList.

2) Then if the item names in store_inventory & new_acquisitions match, just add the two quantities together and change it in newList.

3) If new_acquisitions has a new item that does not exist in store_inventory, then add it to the newList.

The titles for the CSV list are: Item Name, Quantity, Cost, Price.

The List contains an string[] of item name, quantity, cost and price for each row.

    CSVReader from = new CSVReader(new FileReader("/test/new_acquisitions.csv"));
    List <String[]> acquisitions = from.readAll();

    CSVReader to = new CSVReader(new FileReader("/test/store_inventory.csv"));
    List <String[]> inventory = to.readAll();

    List <String[]> newList;

Any code to get me started would be great! =]

this is what i have so far...

        for (int i = 0; i < acquisitions.size(); i++) {
        temp1 = acquisitions.get(i);
        for (int j = 1; j < inventory.size(); j++) {
            temp2 = inventory.get(j);
            if (temp1[0].equals(temp2[0])) {
                //if match found... do something?



                //break out of loop
            }
        }
        //if new item found... do something?
    }
+4  A: 

I would start by building the newList as a HashMap or TreeMap instead of a List. This makes it easy to search for the matching record. Furthermore, I would convert the String[] to a custom object (e.g. Record) that contains the name, quantity, cost and price field. This would take care of copying the information. The you could try something like this:

Map<String, Record> newMap = new TreeMap<String, Record>();
for(String[] ss : acquisitions) {
    Record rec = Record.parse(ss); // For requirement (1)
    newMap.put(rec.getName(), rec);
}

for(String[] ss : inventory) {
    Record rec = Record.parse(ss); // For requirement (1)
    if(newMap.containsKey(rec.getName())) {
        // For requirement (2)
        // The mergeWith method can then add quantities together
        newMap.get(rec.getName()).mergeWith(rec);
    } else {
        // For requirement (3)
        newMap.put(rec.getName(), rec);
    }
}

edit An extra advantage of having a Record object, is that it can be printed to screen much easier by implementing the toString function.

public class Record implements Comparable<Record> {
    public static Record parse(String[] ss) {
        // TODO: implement some basic parsing
    }

    private String name;
    private int quantity;
    private BigDecimal cost, price;

    private Record() {}

    public String getName() { return name; }
    public int getQuantity() { return quantity; }
    public BigDecimal getCost() { return cost; }
    public BigDecimal getPrice() { return price; }

    public int compareTo(Record other) {
        return this.name.compareTo(other.name);
    }

    public String toString() {
        return name;
    }
}
Marc
+1 for suggesting a TreeMap
Jan Kuboschek
im new to programming, you are mentioning all these concepts i havent learned about yet. mind just writing the code i need, you seem to have a lot of it done already haha. do you have a paypal? +D
nubme
This is very simple Java code when you start to understand the basics. Writing code using String arrays is just very complicated, while when using custom objects the code becomes very readable. I would advise trying to understand the basics of Maps (do you know why I advised the TreeMap?) and objects (do you know why I implemented the Comparable interface?).
Marc
ya i see now that its getting pretty complicated, i stored it in a List <String[]> because the csv library i used uses that. TreeMap for efficency and sorting? Im not too sure about comparable interface.
nubme
Nm, i think i sorta got it Marc, thanks =]
nubme