pparth602

Untitled

Oct 8th, 2022
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1.  public static List<Warehouse> WAREHOUSE_LIST = new ArrayList<Warehouse>();
  2.   private static List<Integer> WAREHOUSE_IDS = new ArrayList<Integer>();
  3.  
  4.   /** Load item records from the stock.json file */
  5.   static {
  6.     // System.out.println("Loading items");
  7.     BufferedReader reader = null;
  8.     try {
  9.       // ITEM_LIST.clear();
  10.       WAREHOUSE_LIST.clear();
  11.  
  12.       reader =
  13.           new BufferedReader(
  14.               new FileReader(
  15.                   "./data/stock.json"));
  16.       Object data = JSONValue.parse(reader);
  17.       if (data instanceof JSONArray) {
  18.         JSONArray dataArray = (JSONArray) data;
  19.         for (Object obj : dataArray) {
  20.           if (obj instanceof JSONObject) {
  21.             JSONObject jsonData = (JSONObject) obj;
  22.             Item item = new Item();
  23.             item.setState(jsonData.get("state").toString());
  24.             item.setCategory(jsonData.get("category").toString());
  25.             String date = jsonData.get("date_of_stock").toString();
  26.             // System.out.println("Item Date " + date);
  27.             item.setDateOfStock(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(date));
  28.             // System.out.println(item);
  29.  
  30.             int jsonWarehouseId = Integer.parseInt(jsonData.get("warehouse").toString());
  31.  
  32.             if (!WAREHOUSE_IDS.contains(jsonWarehouseId)) {
  33.               WAREHOUSE_IDS.add(jsonWarehouseId);
  34.  
  35.               Warehouse warehouse = new Warehouse(jsonWarehouseId);
  36.               warehouse.addItem(item);
  37.               WAREHOUSE_LIST.add(warehouse);
  38.             } else {
  39.               for (int i = 0; i < WAREHOUSE_LIST.size(); i++) {
  40.                 if (WAREHOUSE_LIST.get(i).getId() == jsonWarehouseId) {
  41.                   WAREHOUSE_LIST.get(i).addItem(item);
  42.                   // break;
  43.                 }
  44.               }
  45.             }
  46.           }
  47.         }
  48.       }
  49.     } catch (Exception e) {
  50.       e.printStackTrace();
  51.     } finally {
  52.       if (reader != null) {
  53.         try {
  54.           reader.close();
  55.         } catch (IOException e) {
  56.         }
  57.       }
  58.     }
  59.   }
  60.  
  61.   /**
  62.    * Get All items available in the repository
  63.    *
  64.    * @return
  65.    */
  66.   public static List<Item> getAllItems() {
  67.  
  68.     List<Item> allItems = new ArrayList<Item>();
  69.  
  70.     // for(Warehouse warehouse : WAREHOUSE_LIST) {
  71.     for (int i = 0; i < WAREHOUSE_LIST.size(); i++) {
  72.       List<Item> itemsWarehouse = new ArrayList<Item>();
  73.       for (Item item : WAREHOUSE_LIST.get(i).getStock()) {
  74.         item.setWarehouse(WAREHOUSE_LIST.get(i).getId());
  75.         itemsWarehouse.add(item);
  76.       }
  77.       allItems.addAll(itemsWarehouse);
  78.     }
  79.     return allItems;
  80.   }
  81.  
  82.   // By Warehouse
  83.  
  84.   /**
  85.    * Get the list of unique warehouse IDs
  86.    *
  87.    * @return
  88.    */
  89.   public static Set<Integer> getWarehouses() {
  90.     Set<Integer> warehouses = new HashSet<Integer>();
  91.     for (Warehouse warehouse : WAREHOUSE_LIST) {
  92.       warehouses.add(warehouse.getId());
  93.     }
  94.     return warehouses;
  95.   }
  96.  
  97.   /**
  98.    * Get the list of all items in a specific warehouse
  99.    *
  100.    * @param warehouse
  101.    * @return
  102.    */
  103.   public static List<Item> getItemsByWarehouse(int warehouse) {
  104.     return getItemsByWarehouse(warehouse, getAllItems());
  105.   }
  106.  
  107.   /**
  108.    * Get the list of items related to a specific warehouse in a given master-list
  109.    *
  110.    * @param warehouse
  111.    * @return
  112.    */
  113.   public static List<Item> getItemsByWarehouse(int warehouse, List<Item> masterList) {
  114.     List<Item> items = new ArrayList<Item>();
  115.     for (Item item : masterList) {
  116.       if (item.getWarehouse() == warehouse) {
  117.         items.add(item);
  118.       }
  119.     }
  120.     return items;
  121.   }
  122.  
  123.   // By Category
  124.  
  125.   /**
  126.    * Get the list of unique Categories
  127.    *
  128.    * @return
  129.    */
  130.   public static Set<String> getCategories() {
  131.     Set<String> categories = new HashSet<String>();
  132.     for (Item item : getAllItems()) {
  133.       categories.add(item.getCategory());
  134.     }
  135.     return categories;
  136.   }
  137.  
  138.   /**
  139.    * Get the list of all items of a specific category
  140.    *
  141.    * @param category
  142.    * @return
  143.    */
  144.   public static List<Item> getItemsByCategory(String category) {
  145.     return getItemsByCategory(category, getAllItems());
  146.   }
  147.  
  148.   /**
  149.    * Get the list of items of a specific category in a given master-list
  150.    *
  151.    * @param category
  152.    * @return
  153.    */
  154.   public static List<Item> getItemsByCategory(String category, List<Item> masterList) {
  155.     List<Item> items = new ArrayList<Item>();
  156.     for (Item item : masterList) {
  157.       if (item.getCategory().equalsIgnoreCase(category)) {
  158.         items.add(item);
  159.       }
  160.     }
  161.     return items;
  162.   }
  163.  
  164.   public static List<Item> getSearchItems(String searchItemName) {
  165.     List<Item> foundListItems = new ArrayList<Item>();
  166.     List<Item> allItems = WarehouseRepository.getAllItems();
  167.  
  168.     foundListItems =
  169.         allItems.stream()
  170.             .filter(item -> (item.toString().equalsIgnoreCase(searchItemName)))
  171.             .collect(Collectors.toList());
  172.     System.out.println(foundListItems);
  173.     return foundListItems;
  174.   }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment