Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a29c0cb

Browse files
committed
Refactoring - clean code
1 parent 4a4fa48 commit a29c0cb

File tree

5 files changed

+35
-36
lines changed

5 files changed

+35
-36
lines changed

‎src/main/java/hackerrank/api/BarcodeNotFoundException.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎src/main/java/hackerrank/api/BarcodeReader.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.DeserializationFeature;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import com.sun.net.httpserver.HttpHandler;
76

87
import java.io.IOException;
98
import java.net.MalformedURLException;
@@ -12,22 +11,25 @@
1211

1312
public class BarcodeReader {
1413

15-
private final static String TAG = HttpHandler.class.getSimpleName();
14+
private final static String TAG = BarcodeReader.class.getSimpleName();
1615
private final static Logger logger =
1716
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
18-
private final static String barcodeURI = "https://jsonmock.hackerrank.com/api/inventory?barcode=";
17+
private final static String inventoryURI = "https://jsonmock.hackerrank.com/api/inventory";
1918

20-
public Inventory read(int barcode)
21-
throwsBarcodeNotFoundException {
22-
Inventory[] inventories = null;
19+
public Inventory read(int barcode) {
20+
Inventoryinventory = null;
21+
StringbarcodeURI = inventoryURI + "?barcode=";
2322

2423
try {
2524
String inventoryJSONString = InputStreamConverter.toString(
26-
HttpRequests.get(barcodeURI+barcode))
27-
.toString();
25+
HttpRequests.get(barcodeURI+barcode));
2826

29-
inventories = getInventories(inventoryJSONString);
27+
inventory = getInventories(inventoryJSONString)[0];
3028

29+
} catch (InventoryNotFoundException e) {
30+
if (logger.isLoggable(Level.INFO)) {
31+
logger.log(Level.INFO, TAG + " InventoryNotFoundException: " + e.getMessage());
32+
}
3133
} catch (MalformedURLException e) {
3234
if (logger.isLoggable(Level.SEVERE)) {
3335
logger.log(Level.SEVERE, TAG + " MalformedURLException: " + e.getMessage());
@@ -37,19 +39,15 @@ public Inventory read(int barcode)
3739
logger.severe(TAG + " IOException: " + e.getMessage());
3840
}
3941
} catch (Exception e) {
40-
if (logger.isLoggable(Level.SEVERE)) {
41-
logger.severe(TAG + " Exception: " + e.getMessage());
42+
if (logger.isLoggable(Level.INFO)) {
43+
logger.info(TAG + " Exception: " + e.getMessage());
4244
}
4345
}
4446

45-
if ( inventories.length == 0 ) {
46-
throw new BarcodeNotFoundException("Barcode Not Found!");
47-
}
48-
49-
return inventories[0];
47+
return inventory;
5048
}
5149

52-
private Inventory[] getInventories(String inventories)
50+
private Inventory[] getInventories(String jsonInventories)
5351
throws JsonProcessingException {
5452

5553
ObjectMapper objectMapper = new ObjectMapper();
@@ -58,7 +56,7 @@ private Inventory[] getInventories(String inventories)
5856
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
5957

6058
return objectMapper
61-
.readValue(inventories,
59+
.readValue(jsonInventories,
6260
Inventory[].class);
6361
}
6462

‎src/main/java/hackerrank/api/InputStreamConverter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
public abstract class InputStreamConverter {
1212

13-
public static String toString(InputStream is) {
13+
public static String toString(InputStream is)
14+
throws InventoryNotFoundException {
1415

1516
StringBuilder sb = new StringBuilder();
1617
String jsonString = null;
@@ -25,6 +26,11 @@ public static String toString(InputStream is) {
2526

2627
JSONObject json = new JSONObject(sb.toString());
2728
JSONArray dataArray = (JSONArray) json.get("data");
29+
30+
if ( dataArray.isEmpty() ) {
31+
throw new InventoryNotFoundException("Inventory Not Found!");
32+
}
33+
2834
jsonString = dataArray.toString();
2935

3036
} catch (IOException e) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package hackerrank.api;
2+
3+
public class InventoryNotFoundException extends NullPointerException {
4+
public InventoryNotFoundException(String errorMessage) {
5+
super(errorMessage);
6+
}
7+
}

‎src/test/java/hackerranktest/api/BarcodeReaderTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package hackerranktest.api;
22

3-
import hackerrank.api.BarcodeNotFoundException;
43
import hackerrank.api.BarcodeReader;
54
import hackerrank.api.Inventory;
65
import org.junit.jupiter.api.Assertions;
@@ -19,16 +18,12 @@ void givenBarCodeListData() {
1918
}
2019

2120
@Test
22-
void barcodeEmptyException() {
23-
Exception exception = Assertions.assertThrows(
24-
BarcodeNotFoundException.class, () -> {
25-
BarcodeReader br = new BarcodeReader();
26-
br.read(0);
27-
});
21+
void givenBarcodeNotListed() {
2822

29-
String expectedMessage = "Barcode Not Found!";
30-
String actualMessage = exception.getMessage();
23+
int barcode = 0;
24+
BarcodeReader obj = new BarcodeReader();
25+
Inventory actualArr = obj.read(barcode);
3126

32-
Assertions.assertTrue(actualMessage.contains(expectedMessage));
27+
Assertions.assertNull(actualArr);
3328
}
3429
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /