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 22a2e48

Browse files
committed
Hackerrank api problem solved
Run PMD: ./run.sh pmd -d /home/anit/work/codechallenge-java-pom/src/main/java/hackerrank/api -f text -R /home/anit/work/codechallenge-java-pom/.pmd --cache /tmp/codechallenge-pmd-analysis | more Run unit test: mvn test -Dtest=hackerranktest.api.BarcodeReaderTest
1 parent 0f87492 commit 22a2e48

File tree

8 files changed

+238
-113
lines changed

8 files changed

+238
-113
lines changed
Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,84 @@
11
package hackerrank.api;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.DeserializationFeature;
5-
import com.fasterxml.jackson.databind.ObjectMapper;
6-
73
import java.io.IOException;
84
import java.net.MalformedURLException;
95
import java.util.logging.Level;
106
import java.util.logging.Logger;
117

8+
/**
9+
* Get inventories from barcode
10+
*
11+
*/
1212
public class BarcodeReader {
13+
/**
14+
* Class name
15+
*/
16+
private final static String TAG = "BarcodeReader";
1317

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 =
1622
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
17-
private final static String inventoryURI = "https://jsonmock.hackerrank.com/api/inventory";
1823

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=";
2257

2358
try {
24-
String inventoryJSONString = InputStreamConverter.toString(
25-
HttpRequests.get(barcodeURI+barcode));
59+
finalString inventoryJSON = InputStreamConverterUtil.toString(
60+
HttpRequestsUtil.get(barcodeURI + barcode));
2661

27-
inventory = getInventories(inventoryJSONString)[0];
62+
inventory = inventory.getInventories(inventoryJSON)[0];
2863

2964
} 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());
3267
}
3368
} 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());
3671
}
3772
} 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());
4475
}
4576
}
4677

4778
return inventory;
4879
}
4980

50-
private Inventory[] getInventories(String jsonInventories)
51-
throws JsonProcessingException {
5281

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

6383
}
6484

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package hackerrank.api;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
8+
/**
9+
* Process HTTP Requests
10+
*
11+
*/
12+
@SuppressWarnings("PMD.LawOfDemeter")
13+
public final class HttpRequestsUtil {
14+
15+
/**
16+
* To prevent initialization
17+
*/
18+
private HttpRequestsUtil() {
19+
throw new UnsupportedOperationException();
20+
}
21+
22+
/**
23+
* Process HTTP GET request
24+
*
25+
* @param url String
26+
* @return InputStream
27+
* @throws IOException``
28+
*/
29+
public static InputStream get(final String url)
30+
throws IOException {
31+
final URL urlObject = new URL(url);
32+
final HttpURLConnection conn = (HttpURLConnection) urlObject.openConnection();
33+
conn.setRequestMethod("GET");
34+
35+
return conn.getInputStream();
36+
}
37+
}

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

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package hackerrank.api;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.InputStreamReader;
10+
11+
/**
12+
* Convert Input Stream to String
13+
*/
14+
@SuppressWarnings("PMD.LawOfDemeter")
15+
public final class InputStreamConverterUtil {
16+
17+
/**
18+
* To prevent initialization
19+
*/
20+
private InputStreamConverterUtil() {
21+
throw new UnsupportedOperationException();
22+
}
23+
24+
/**
25+
*
26+
* @param inputStream InputStream
27+
* @return String JSON inventory string
28+
* @throws InventoryNotFoundException Inventory Not Found
29+
*/
30+
public static String toString(final InputStream inputStream)
31+
throws InventoryNotFoundException, IOException {
32+
33+
final StringBuilder stringBuilder = new StringBuilder();
34+
String line;
35+
36+
final BufferedReader reader = new BufferedReader(
37+
new InputStreamReader(inputStream));
38+
do {
39+
line = reader.readLine();
40+
stringBuilder.append(line).append('\n');
41+
} while (line != null);
42+
43+
final JSONObject json = new JSONObject(
44+
stringBuilder.toString());
45+
final JSONArray dataArray = (JSONArray) json.get("data");
46+
47+
if ( dataArray.isEmpty() ) {
48+
throw new InventoryNotFoundException(
49+
"Inventory Not Found!");
50+
}
51+
52+
return dataArray.toString();
53+
}
54+
}

0 commit comments

Comments
(0)

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