|
1 | 1 | package hackerrank.api;
|
2 | 2 |
|
3 |
| -import com.fasterxml.jackson.core.JsonProcessingException; |
4 |
| -import com.fasterxml.jackson.databind.DeserializationFeature; |
5 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
6 |
| - |
7 | 3 | import java.io.IOException;
|
8 | 4 | import java.net.MalformedURLException;
|
9 | 5 | import java.util.logging.Level;
|
10 | 6 | import java.util.logging.Logger;
|
11 | 7 |
|
| 8 | +/** |
| 9 | + * Get inventories from barcode |
| 10 | + * |
| 11 | + */ |
12 | 12 | public class BarcodeReader {
|
| 13 | + /** |
| 14 | + * Class name |
| 15 | + */ |
| 16 | + private final static String TAG = "BarcodeReader"; |
13 | 17 |
|
14 |
| - private final static String TAG = BarcodeReader.class.getSimpleName(); |
15 |
| - private final static Logger logger = |
| 18 | + /** |
| 19 | + * Log messages |
| 20 | + */ |
| 21 | + private final static Logger LOGGER = |
16 | 22 | Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
|
17 |
| - private final static String inventoryURI = "https://jsonmock.hackerrank.com/api/inventory"; |
18 | 23 |
|
19 |
| - public Inventory read(int barcode) { |
20 |
| - Inventory inventory = null; |
21 |
| - String barcodeURI = inventoryURI + "?barcode="; |
| 24 | + /** |
| 25 | + * Inventory URI |
| 26 | + */ |
| 27 | + private final static String INVENTORY_URI = "https://jsonmock.hackerrank.com/api/inventory"; |
| 28 | + |
| 29 | + /** |
| 30 | + * Barcode |
| 31 | + */ |
| 32 | + private final int barcode; |
| 33 | + |
| 34 | + /** |
| 35 | + * |
| 36 | + * @return int barcode |
| 37 | + */ |
| 38 | + public int getBarcode() { |
| 39 | + return barcode; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * |
| 44 | + */ |
| 45 | + public BarcodeReader(final int barcode) { |
| 46 | + this.barcode = barcode; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Read inventory from barcode. |
| 51 | + * |
| 52 | + * @return Inventory |
| 53 | + */ |
| 54 | + public Inventory read() { |
| 55 | + Inventory inventory = new Inventory(); |
| 56 | + final String barcodeURI = INVENTORY_URI + "?barcode="; |
22 | 57 |
|
23 | 58 | try {
|
24 |
| - String inventoryJSONString = InputStreamConverter.toString( |
25 |
| - HttpRequests.get(barcodeURI+barcode)); |
| 59 | + final String inventoryJSON = InputStreamConverterUtil.toString( |
| 60 | + HttpRequestsUtil.get(barcodeURI + barcode)); |
26 | 61 |
|
27 |
| - inventory = getInventories(inventoryJSONString)[0]; |
| 62 | + inventory = inventory.getInventories(inventoryJSON)[0]; |
28 | 63 |
|
29 | 64 | } catch (InventoryNotFoundException e) {
|
30 |
| - if (logger.isLoggable(Level.INFO)) { |
31 |
| - logger.log(Level.INFO, TAG + " InventoryNotFoundException: " + e.getMessage()); |
| 65 | + if (LOGGER.isLoggable(Level.INFO)) { |
| 66 | + LOGGER.log(Level.INFO, TAG + " InventoryNotFoundException: " + e.getMessage()); |
32 | 67 | }
|
33 | 68 | } catch (MalformedURLException e) {
|
34 |
| - if (logger.isLoggable(Level.SEVERE)) { |
35 |
| - logger.log(Level.SEVERE, TAG + " MalformedURLException: " + e.getMessage()); |
| 69 | + if (LOGGER.isLoggable(Level.SEVERE)) { |
| 70 | + LOGGER.log(Level.SEVERE, TAG + " MalformedURLException: " + e.getMessage()); |
36 | 71 | }
|
37 | 72 | } catch (IOException e) {
|
38 |
| - if (logger.isLoggable(Level.SEVERE)) { |
39 |
| - logger.severe(TAG + " IOException: " + e.getMessage()); |
40 |
| - } |
41 |
| - } catch (Exception e) { |
42 |
| - if (logger.isLoggable(Level.INFO)) { |
43 |
| - logger.info(TAG + " Exception: " + e.getMessage()); |
| 73 | + if (LOGGER.isLoggable(Level.SEVERE)) { |
| 74 | + LOGGER.severe(TAG + " IOException: " + e.getMessage()); |
44 | 75 | }
|
45 | 76 | }
|
46 | 77 |
|
47 | 78 | return inventory;
|
48 | 79 | }
|
49 | 80 |
|
50 |
| - private Inventory[] getInventories(String jsonInventories) |
51 |
| - throws JsonProcessingException { |
52 | 81 |
|
53 |
| - ObjectMapper objectMapper = new ObjectMapper(); |
54 |
| - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
55 |
| - objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false); |
56 |
| - objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); |
57 |
| - |
58 |
| - return objectMapper |
59 |
| - .readValue(jsonInventories, |
60 |
| - Inventory[].class); |
61 |
| - } |
62 | 82 |
|
63 | 83 | }
|
64 | 84 |
|
0 commit comments