Matthias_18

Untitled

Feb 17th, 2021 (edited)
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. package com.knapp.codingcontest.cc20160408.solution;
  2.  
  3. import com.knapp.codingcontest.cc20160408.Input;
  4. import com.knapp.codingcontest.cc20160408.data.LocationCollection;
  5. import com.knapp.codingcontest.cc20160408.data.PickOrderCollection;
  6. import com.knapp.codingcontest.cc20160408.data.ProductCollection;
  7. import com.knapp.codingcontest.cc20160408.entities.*;
  8. import com.knapp.codingcontest.cc20160408.util.Contract;
  9.  
  10. import java.util.*;
  11.  
  12. public class Solution {
  13.  
  14. /**
  15. * Your name
  16. */
  17. public final String participantName = "Matthias Utrata";
  18. /**
  19. * The Id of your institute - please refer to the handout
  20. */
  21. public final String instituteId = "htl3r";
  22. /**
  23. * local reference to the global location collection
  24. */
  25. private final LocationCollection locationCollection;
  26. /**
  27. * local reference to the global product collection
  28. */
  29. private final ProductCollection productCollection;
  30. /**
  31. * local reference to the global collection with unfulfilled pick-orders
  32. * <p>
  33. * Note: the pickOrderCollection is always up to date when GetNextReplenishmentOrder is called
  34. */
  35. private final PickOrderCollection pickOrderCollection;
  36.  
  37. // ----------------------------------------------------------------------------
  38.  
  39. public static final boolean DEBUG = false;
  40.  
  41. private Map<String, Integer> quantityOnHand;
  42. private List<PickOrder> pickOrders;
  43.  
  44. /**
  45. * Create the solution instance74
  46. * <p>
  47. * Do all your preparations here
  48. *
  49. * @param input
  50. */
  51. public Solution(final Input input) {
  52.  
  53. Contract.requires(input != null, "illegal argument");
  54.  
  55. Contract.requires(input.getLocationCollection() != null, "illegal argument");
  56. Contract.requires(input.getLocationCollection().count() > 0, "illegal argument");
  57.  
  58. Contract.requires(input.getProductCollection() != null, "illegal argument");
  59. Contract.requires(input.getProductCollection().count() > 0, "illegal argument");
  60.  
  61. Contract.requires(input.getPickOrderCollection() != null, "illegal argument");
  62. Contract.requires(input.getPickOrderCollection().count() > 0, "illegal argument");
  63.  
  64. Contract.requires(!Contract.isNullOrWhiteSpace(instituteId), "Please set InstituteId in Solution.java");
  65. Contract.requires(!Contract.isNullOrWhiteSpace(participantName), "Please set ParticipantName in Solution.java");
  66.  
  67. //
  68. locationCollection = input.getLocationCollection();
  69. productCollection = input.getProductCollection();
  70. pickOrderCollection = input.getPickOrderCollection();
  71. // Your code goes here
  72.  
  73. // Prepare custom Collections
  74. quantityOnHand = new HashMap<>();
  75.  
  76. productCollection.getProducts().forEach(product -> quantityOnHand.put(product.getCode(), 0));
  77.  
  78. pickOrders = new LinkedList<>(pickOrderCollection.getPickOrders());
  79.  
  80. pickOrderCollection.getPickOrders().forEach(pickOrder -> pickOrder.calcReplenishmentsNeeded(quantityOnHand));
  81. }
  82.  
  83. // ----------------------------------------------------------------------------
  84.  
  85.  
  86. /**
  87. * return the next replen move for the caller to execute
  88. *
  89. * @return the next replen move for the caller to execute
  90. */
  91. public ReplenishmentOrder getNextReplenishmentOrder() {
  92. // The caller (KNAPP code) executes the replen and performs the next possible pick
  93. // If no replenishment order should be executed in this timeframe, return null
  94.  
  95. // add your code here to select the next (best) replen move
  96. // and return it to the caller
  97.  
  98. // return your created ReplenishmentOrder, or null if u do not want to do anything in this frame
  99.  
  100. Product product = null;
  101. Location location = null;
  102. int quantity = 0;
  103.  
  104. Collections.sort(pickOrders);
  105. product = getNextProduct();
  106.  
  107. if (DEBUG) {
  108. System.out.println("Quantity on Hand:");
  109.  
  110. quantityOnHand.entrySet().stream().filter(stringIntegerEntry -> stringIntegerEntry.getValue() != 0).forEach(stringIntegerEntry -> System.out.println("Product: " + stringIntegerEntry.getKey() + " Quantity: " + stringIntegerEntry.getValue()));
  111.  
  112. System.out.println("Queue:");
  113. if (pickOrders.size() < 3) {
  114. pickOrders.subList(0, pickOrders.size()).stream().map(pickOrder -> pickOrder.toString()).forEach(System.out::println);
  115. } else {
  116. pickOrders.subList(0, 3).stream().map(pickOrder -> pickOrder.toString()).forEach(System.out::println);
  117. }
  118. }
  119.  
  120. if (product != null) {
  121.  
  122. location = getNextFreeLocation(product);
  123.  
  124. quantity = calcQuantity(product);
  125.  
  126. // Update Quantity on Hand
  127. quantityOnHand.replace(product.getCode(), quantityOnHand.get(product.getCode()) + quantity);
  128.  
  129. if (DEBUG) {
  130. System.out.println("Product: " + product);
  131. System.out.println("Location: " + location);
  132. System.out.println("Quantity: " + quantity);
  133. }
  134.  
  135. return new ReplenishmentOrder(product, location, quantity);
  136. }
  137.  
  138. return null;
  139. }
  140.  
  141.  
  142. private Product getNextProduct() {
  143.  
  144. for (PickOrder pickOrder : pickOrders) {
  145. for (PickOrderLine pickOrderLine : pickOrder.getPickOrderLines()) {
  146. if (pickOrderLine.getQuantity() > quantityOnHand.get(pickOrderLine.getProductCode())) {
  147. return productCollection.findByCode(pickOrderLine.getProductCode());
  148. }
  149. }
  150. }
  151. return null;
  152. }
  153.  
  154. // ............................................................................
  155.  
  156.  
  157. private Location getNextFreeLocation(Product product) {
  158. return locationCollection.getLocations().stream().filter(n -> n.getAssignedProduct() == null).findFirst().get();
  159. }
  160.  
  161.  
  162. private int calcQuantity(Product product) {
  163.  
  164. int productQuantityOnHand = quantityOnHand.get(product.getCode());
  165. int productQuantityNeeded = pickOrderCollection.getCurrentNeededQuantity(product.getCode()) -
  166. productQuantityOnHand;
  167.  
  168. return productQuantityNeeded > product.getMaxLocationQuantity() ?
  169. product.getMaxLocationQuantity() :
  170. productQuantityNeeded;
  171. }
  172.  
  173. // ----------------------------------------------------------------------------
  174.  
  175.  
  176. /**
  177. * This function is called after all picks have been performed by the framework.
  178. * <p>
  179. * If necessary, you can handle it.
  180. * Note: the pickOrderCollection is also updated and always reflects the current state,
  181. * which meansm the id's in pickedOrderIds can no longer be found in the pickOrderCollection
  182. *
  183. * @param pickedOrders read-only collection with the ids of the order that have been picked
  184. */
  185. public void handlePickedOrders(final List<PickOrder> pickedOrders) {
  186.  
  187. Contract.requires(pickedOrders != null, "illegal argument");
  188.  
  189. // Your code goes here - if needed
  190.  
  191. if (DEBUG) {
  192. new Scanner(System.in).nextLine();
  193. }
  194.  
  195. if (!pickedOrders.isEmpty()) {
  196.  
  197. // Get Products from the picked Orders
  198. Map<String, Integer> removedProducts = new LinkedHashMap<>();
  199. for (PickOrder pickedOrder : pickedOrders) {
  200. for (PickOrderLine pickedOrderLines : pickedOrder.getPickOrderLines()) {
  201. if (!removedProducts.containsKey(pickedOrderLines.getProductCode())) {
  202. removedProducts.put(pickedOrderLines.getProductCode(), pickedOrderLines.getQuantity());
  203. } else {
  204. removedProducts.replace(pickedOrderLines.getProductCode(),
  205. removedProducts.get(pickedOrderLines.getProductCode()) +
  206. pickedOrderLines.getQuantity());
  207. }
  208. }
  209. }
  210.  
  211. // Update Quantity on Hand
  212. for (Map.Entry<String, Integer> entry : removedProducts.entrySet()) {
  213. if (DEBUG) {
  214. System.out.println("Remove " + entry.getValue() + " pieces of " + entry.getKey());
  215. }
  216.  
  217. quantityOnHand.replace(entry.getKey(), quantityOnHand.get(entry.getKey()) - entry.getValue());
  218. }
  219.  
  220. // Performance optimization
  221. if (DEBUG) {
  222. System.out.println("Remove Pick Orders " + pickedOrders + " from Queue");
  223. }
  224.  
  225. pickOrders.removeAll(pickedOrders);
  226. for (PickOrder pickOrder : pickOrders) {
  227. for (String productCode : removedProducts.keySet()) {
  228. if (pickOrder.containsProduct(productCode)) {
  229. pickOrder.calcReplenishmentsNeeded(quantityOnHand);
  230. if (DEBUG) {
  231. System.out.println("Recalculating: " + pickOrder);
  232. }
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. }
  239.  
  240. // ----------------------------------------------------------------------------
  241. }
  242.  
Add Comment
Please, Sign In to add comment