1

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
KenS2016
  • 21
  • 3

2 Answers2

1

Your logic is faulty.

The only time when you move to a different inventory item is when you don't have enough inventory for it.

But when you have finished all the sale items for a particular product, there could still be some inventory in it. If you sold less of a product than you have in stock, you'll never get to the point where you don't have enough inventory. And thus, you'll never get to the next product.

In your particular case, the first sale for item 10001 is more than you have. So it moves to item 10002. But there are 33 of item 10002, but you only sell 20 of them. You finish the loop without ever getting inv[k][1] < sale[i][1].

I believe your logic should be different. Assuming you cannot use sets and maps (which would be better suited for this), only arrays like that, then you should:

  • Go through the inventory array, one by one.
  • For each inventory item, search the sales array for all sales of that item and subtract as necessary from the inventory until you have exhausted the inventory.

So you'll have a loop inside a loop.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
0

First of all, you probably want to use a HashMap<Integer,Integer> rather than arrays for this problem.

Now, to your question: There's a problem in your loop's logic. @RealSkeptic points out the flaw in his answer. Your solution can be greatly simplified if you start by sorting the sale data by productId. You can sort a 2-D array using a Comparator (adapted from this answer):

    Arrays.sort(sale, new Comparator<Integer[]>() {
        @Override
        public int compare(final Integer[] entry1, final Integer[] entry2) {
            final Integer v1 = entry1[0];
            final Integer v2 = entry2[0];
            return v1.compareTo(v2);
        }
    });

The same approach should be used to ensure your inv array is also sorted. Once sorted, you can process sales using the following (simplified) loop:

    int i = 0; //inventory id
    for(int k=0; k < sale.length; k++){
        if(inv[k][0] == sale[i][0]){ //if product Ids match
            if(inv[k][1] >= sale[i][1]){ //if there's enough inventory
                inv[k][1] -= sale[i][1]; //subtract units from inventory
            }
        } else { //no more sales for this productId
            i++; //move to next item in the inventory
        }
    }

EDIT: Based on your comment below, you CAN do as @RealSkeptic suggests and use a nested loop. However, I strongly recommend you use a HashMap instead. For example:

final HashMap<Integer,Integer> inv = initInventory();
final int[][] sale = initSalesData();

//process the sales data
for(int i = 0; i < sale.length; i++){ //for each sale
    final int saleProductId = sale[i][0]; //get the sale productId 
    final int saleQty = sale[i][1]; // get the sale quantity
    final Integer qty = inv.get(saleProductId); //get the inventory quantity
    if(qty != null && qty >= saleQty){ //if productId is valid and inventory has sufficient quantity
        inv.put(saleProductId, qty-saleQty); //update inventory quantity
    }
}

You can test this method with the following code below. The inputs are based on the input you provided:

import java.util.HashMap;

public class InventoryExample {

    public static void main(String[] args) {

        final HashMap<Integer,Integer> inv = initInventory();
        final int[][] sale = initSalesData();

        //process the sales data
        for(int i = 0; i < sale.length; i++){ //for each sale
            final int saleProductId = sale[i][0]; //get the sale productId 
            final int saleQty = sale[i][1]; // get the sale quantity
            final Integer qty = inv.get(saleProductId); //get the inventory quantity
            if(qty != null && qty >= saleQty){ //if productId is valid and inventory has sufficient quantity
                inv.put(saleProductId, qty-saleQty); //update inventory quantity
            }
        }

        //print results
        for(int productId:inv.keySet()){
            System.out.println(productId+" | "+inv.get(productId));
        }

    }

    private static HashMap<Integer, Integer> initInventory() {
        final HashMap<Integer, Integer> inv = new HashMap<>();
        inv.put(10001, 3);
        inv.put(10002, 33);
        inv.put(10003, 51);
        inv.put(10004, 101);
        inv.put(10005, 4);
        inv.put(10006, 30);
        inv.put(10007, 36);
        inv.put(10008, 8);
        inv.put(10009, 22);
        inv.put(10010, 74);
        inv.put(20001, 24);
        inv.put(20002, 13);
        inv.put(20003, 0);
        inv.put(20004, 62);
        inv.put(20005, 21);
        inv.put(20006, 22);
        inv.put(30001, 59);
        inv.put(30002, 7);
        inv.put(30003, 18);
        inv.put(30004, 44);
        inv.put(30005, 28);
        inv.put(30006, 106);
        inv.put(30007, 23);
        inv.put(30008, 8);
        inv.put(30009, 29);
        inv.put(30010, 7);
        inv.put(30011, 20);
        inv.put(30012, 24);
        inv.put(30013, 55);
        inv.put(30014, 15);
        inv.put(30015, 64);
        inv.put(30016, 15);
        inv.put(30017, 13);
        inv.put(30018, 34);
        inv.put(30019, 44);
        inv.put(30020, 39);
        return inv;
    }

    private static int[][] initSalesData() {
        return new int[][] { 
            {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}
        };
    }

}

This produces the following output:

10001 | 3
10002 | 13
10003 | 23
10004 | 29
10005 | 0
10006 | 4
10007 | 8
10008 | 3
10009 | 3
10010 | 37
20001 | 5
20002 | 0
20003 | 0
20004 | 15
20005 | 0
20006 | 10
30001 | 21
30002 | 0
30003 | 3
30004 | 6
30005 | 7
30006 | 48
30007 | 8
30008 | 2
30009 | 7
30010 | 0
30011 | 4
30012 | 4
30013 | 17
30014 | 0
30015 | 4
30016 | 0
30017 | 3
30018 | 16
30019 | 1
30020 | 10
Community
  • 1
  • 1
Austin
  • 8,018
  • 2
  • 31
  • 37
  • Sorry for not clarifying, but I have to read the sales data sequentially without sorting, so if the inventory is less than the units sold, even if the inventory is not 0, then I have to move to next product id. So I have to move through the sales data array until the inventory of the same product id is less than unit sold, move to next inventory item, then start at the beginning of the sales data array again. – KenS2016 Mar 11 '16 at 19:46