I am trying to calculate inventory after selling the sales items using a 2 dimensional array as a sales data, but only my second product id's inventory is being calculated? I am using one 2 dimensional array as the inventory and a second 2 dimensional array as the sales data which is being read in sequential order. The first array has the the product ids in the first column and starting inventory in the second column.
Inventory array printed:
10001 | 3
10002 | 33
10003 | 51
10004 | 101
10005 | 4
10006 | 30
10007 | 36
10008 | 8
10009 | 22
10010 | 74
20001 | 24
20002 | 13
20003 | 0
20004 | 62
20005 | 21
20006 | 22
30001 | 59
30002 | 7
30003 | 18
30004 | 44
30005 | 28
30006 | 106
30007 | 23
30008 | 8
30009 | 29
30010 | 7
30011 | 20
30012 | 24
30013 | 55
30014 | 15
30015 | 64
30016 | 15
30017 | 13
30018 | 34
30019 | 44
30020 | 39
Sales Data array printed:
10002 || 4
10004 || 6
10008 || 2
10010 || 3
10010 || 3
10007 || 10
20003 || 4
20003 || 7
30019 || 1
30020 || 9
10004 || 9
10006 || 7
20005 || 5
30004 || 10
20004 || 2
30002 || 7
30013 || 6
20005 || 5
30006 || 1
30006 || 7
30007 || 2
30012 || 10
30015 || 7
30015 || 3
10001 || 5
20001 || 8
30007 || 3
30014 || 3
30015 || 6
30016 || 2
10001 || 8
10003 || 8
10007 || 5
10007 || 8
20005 || 6
20006 || 10
30003 || 4
30011 || 7
30014 || 7
30016 || 5
20003 || 1
30003 || 4
30010 || 5
30011 || 8
30012 || 1
30014 || 9
30016 || 4
30018 || 5
10006 || 1
10010 || 9
20005 || 5
30001 || 1
30004 || 9
30007 || 5
30009 || 1
30014 || 6
30016 || 5
30016 || 2
30020 || 8
10004 || 3
10004 || 7
10008 || 2
20001 || 4
20003 || 7
20004 || 6
20006 || 2
30001 || 1
30003 || 7
30004 || 1
30004 || 10
30005 || 2
30006 || 7
30017 || 10
30018 || 2
30004 || 6
10009 || 9
10009 || 7
10010 || 10
20005 || 3
30002 || 1
30004 || 2
30004 || 8
30013 || 3
30019 || 3
10004 || 10
10007 || 5
30001 || 7
30003 || 6
30006 || 2
30006 || 3
30007 || 2
30013 || 8
30016 || 8
10003 || 6
10004 || 9
10004 || 2
10006 || 8
10010 || 1
20003 || 9
20003 || 10
30001 || 8
30002 || 1
30013 || 5
30015 || 7
30018 || 1
30019 || 10
30020 || 9
10008 || 1
10009 || 3
20004 || 10
30010 || 7
30012 || 9
30014 || 7
30017 || 4
20004 || 5
30005 || 8
30010 || 7
30011 || 1
10004 || 9
10004 || 4
10006 || 5
10010 || 7
20001 || 3
20003 || 2
20004 || 7
30004 || 8
30010 || 2
30012 || 5
30019 || 8
30020 || 1
10008 || 7
30001 || 9
30003 || 10
30003 || 8
30006 || 5
30008 || 10
30009 || 7
30009 || 8
30015 || 9
30017 || 9
30018 || 7
30020 || 1
10001 || 7
10002 || 10
10003 || 3
10005 || 4
10010 || 4
20001 || 4
20002 || 1
20005 || 5
30006 || 9
30006 || 6
10003 || 7
10003 || 4
10004 || 6
10009 || 6
20002 || 6
20004 || 3
20005 || 3
30001 || 9
30005 || 2
30006 || 10
30008 || 6
30009 || 6
30011 || 10
30013 || 8
30013 || 3
30013 || 5
30015 || 1
30019 || 10
10002 || 6
10004 || 7
10006 || 3
10006 || 2
20002 || 6
20004 || 7
20004 || 7
30005 || 4
30006 || 8
30007 || 3
30008 || 6
30009 || 8
30014 || 5
30015 || 10
30015 || 9
30018 || 3
30019 || 2
30019 || 9
30020 || 1
10006 || 10
20003 || 2
30001 || 3
30005 || 5
30012 || 5
30015 || 5
30015 || 3
30016 || 2
30019 || 8
30019 || 5
I tried to read the sales data array and inventory arrays sequentially and subtract the units from inventory as long as inventory is sufficient, but my output is showing the only the second product id's inventory is being calculated? I have no idea why it is only working for the second product id, so any help will be greatly appreciated.
int i = 0;//counter for rows in sales data
int k = 0;//counter for rows in inventory
while(i < sale.length ){//while not end of sale data array
if(inv [k][0] == sale[i][0]{//if the product ids are the same
if(inv[k][1] < sale[i][1]){//if the inventory is less than the units to be sold
i = 0;//go back to beginning of sale data
if(k < inv.length){ //while not end of inventory array
k++;//move to next inventory item
}
}
if((inv[k][1] > sale[i][1]) && (inv[k][1] > 0)){//if there is enough inventory
inv[k][1] = inv[k][1] - sale[i][1];//subtract units from inventory
i++;//move to next sales item
}
}
else{
i++;//move to next sales item
}
}
My output:
10001 | 3
10002 | 13
10003 | 51
10004 | 101
10005 | 4
10006 | 30
10007 | 36
10008 | 8
10009 | 22
10010 | 74
20001 | 24
20002 | 13
20003 | 0
20004 | 62
20005 | 21
20006 | 22
30001 | 59
30002 | 7
30003 | 18
30004 | 44
30005 | 28
30006 | 106
30007 | 23
30008 | 8
30009 | 29
30010 | 7
30011 | 20
30012 | 24
30013 | 55
30014 | 15
30015 | 64
30016 | 15
30017 | 13
30018 | 34
30019 | 44
30020 | 39