I am not a programmer. I work at warehouse and I would like to make my work easier. One of my responsibilities is to divide excel file with over 2000 rows into smaller ones and send them out. I do it manually and I would like to create a program that does it for me.
I have managed to read the data from excel. Each row is put into an Item data structure. And all of them are put into ArrayList.
public class Item{
String ean;
int amount;
}
Here is my problem.
I have one ArrayList with over 2000 elements. Each element has String Name and int amount.
I must divide it into smaller ArrayLists but the condition is that the total amount of item.getAmount() can´t be more than 800;
int totalAmount = 0;
int counter = 1;
List<Item> temp = new ArrayList<>();
for (Itema item: items) {
totalAmount += item.getAmount();
if (totalAmount <= 800) {
temp.add(item);
}
if (counter == items.size()) {
createFile(temp);
temp.clear();
}
if (totalAmount > 800) {
createFile(temp);
temp.clear();
temp.add(item);
totalAmount = 0;
}
counter++;
}
When I run this with ArrayList that contains thirty items each with amount 100. It creates 4 excel files. First three only have 1 row each and fourth one has 4 rows.
I can't figure it out. Any suggestions??