2016-03-11 34 views
1

我試圖使用2維數組作爲銷售數據銷售銷售項目後計算庫存,但只計算我的第二個產品ID的庫存?我使用一個二維數組作爲庫存,第二個二維數組作爲按順序讀取的銷售數據。第一個數組在第一列中包含產品ID,在第二列中包含起始庫存。在java中只有一種產品的庫存在減少?

庫存陣列印刷:

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 

銷售數據陣列印刷:

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 

我試圖讀取銷售數據陣列和庫存陣列順序地並且只要庫存是從庫存中減去單位足夠了,但是我的輸出顯示只有第二個產品ID的庫存正在計算中?我不知道爲什麼它只爲第二個產品ID工作,所以任何幫助將不勝感激。

  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 
      } 
     } 

我的輸出:

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 

回答

1

你的邏輯是錯誤的。

只有當您沒有足夠的庫存時,纔會轉移到其他庫存料品。

但是,當您完成特定產品的所有銷售項目時,仍可能存在一些庫存。如果您銷售的產品數量少於庫存數量,則永遠無法達到庫存不足的程度。因此,你永遠不會去下一個產品。

在您的具體情況下,第10001項的第一筆銷售量比您的要多。所以它移動到項目10002.但是項目10002有33個,但是你只能出售其中的20個。你完成循環,沒有得到inv[k][1] < sale[i][1]

我相信你的邏輯應該是不同的。假設你不能使用集合和地圖(這將更適合於此),只有這樣的數組,那麼你應該:

  • 逐個瀏覽庫存數組。
  • 對於每個庫存商品,搜索該商品的所有銷售的銷售數組,並根據需要從庫存中減去,直到用完庫存。

所以你會有一個循環內循環。

0

首先,您可能想要使用HashMap<Integer,Integer>而不是陣列來解決此問題。

現在,你的問題:你的循環的邏輯有一個問題。 @RealSkeptic在他的回答中指出了缺陷。如果您按照productId對銷售數據進行排序,則可以大大簡化您的解決方案。您可以按使用Comparator(改編自this answer)2-d數組:

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); 
     } 
    }); 

同樣的方法應採用,以確保您inv陣列也來分類的。一旦排序,您可以使用以下(簡化)循環處理銷售:

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 
     } 
    } 

編輯:根據您在下面的評論,你可以做@RealSkeptic建議和使用嵌套循環。不過,我強烈建議您改用HashMap。例如:

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 
    } 
} 

您可以使用下面的代碼測試此方法。這些輸入根據您提供的輸入:

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} 
     }; 
    } 

} 

這將產生以下的輸出:

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 
+0

對不起,不澄清,但我不排序讀取銷售數據順序,因此,如果庫存小於銷售的單位,即使庫存不是0,那麼我必須轉移到下一個產品ID。因此,我必須遍歷銷售數據陣列,直到相同產品ID的庫存小於銷售量,轉到下一個庫存項目,然後再次從銷售數據數組的開始處開始。 – KenS2016

+0

我已根據您的評論更新了我的答案。 –

相關問題