jca Posted December 15, 2017 Share Posted December 15, 2017 (edited) Needed a class that would hold active GE price, have the amount of item collected, name of item etc. Please feedback if this is not the most efficient way of doing this. Use by calling Spoiler itemResource = new ItemResource("Item name", item_id); Code: Spoiler import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; class ItemResource { private String name; private String url; private int id; private int price; private int amount; public ItemResource(String name, int id) { this.url = "http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=" + id; this.name = name; this.id = id; this.price = fetchPrice(); } // Getters public int getPrice() { return price; } public int getAmount() { return amount; } public String getName() { return name; } // Setters public void setAmount(int i) { amount = amount + i; } // Reset Amount public void resetAmount() { amount = 0; } private int fetchPrice() { try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()))) { final String raw = reader.readLine().replace(",", "").replace("\"", "").split("price:")[1].split("}")[0]; reader.close(); return raw.endsWith("m") || raw.endsWith("k") ? (int) (Double.parseDouble(raw.substring(0, raw.length() - 1)) * (raw.endsWith("m") ? 1_000_000 : 1_000)) : Integer.parseInt(raw); } catch (final Exception e) { e.printStackTrace(); } return -1; } } Credit to whoever wrote the fetchPrice() snippet, found on external site. Edited December 20, 2017 by jca 1 Quote Link to comment Share on other sites More sharing options...
liverare Posted December 15, 2017 Share Posted December 15, 2017 Naming anything "resource" is bad, because it has a very ambiguous meaning in programming. Anything could be a resource. Perhaps GEItem, or something? 1 Quote Link to comment Share on other sites More sharing options...
Explv Posted December 15, 2017 Share Posted December 15, 2017 (edited) Not sure why you have the constants THOUSAND and MILLION (which should be static btw). Everyone knows what 1000 is, and if you have a hard time reading a lot of 0s you can separate using underscores: 1_000_000. Edited December 15, 2017 by Explv Quote Link to comment Share on other sites More sharing options...
jca Posted December 15, 2017 Author Share Posted December 15, 2017 (edited) 18 minutes ago, liverare said: Naming anything "resource" is bad, because it has a very ambiguous meaning in programming. Anything could be a resource. Perhaps GEItem, or something? It made sense in the context, a Resource would be Oak logs, Lobsters etc. That wouldn't fit the GEItem as it's not specific to GE and couldn't think of another name. Would ItemResource work better? Thanks for the feedback. 15 minutes ago, Explv said: Not sure why you have the constants THOUSAND and MILLION (which should be static btw). Everyone knows what 1000 is, and if you have a hard time reading a lot of 0s you can separate using underscores: 1_000_000. Thanks! Edited the snippet with underscores. Edited December 15, 2017 by jca Quote Link to comment Share on other sites More sharing options...
TheWind Posted December 16, 2017 Share Posted December 16, 2017 7 hours ago, jca said: ItemResource no that's just as generic as resource Quote Link to comment Share on other sites More sharing options...
jca Posted December 16, 2017 Author Share Posted December 16, 2017 6 hours ago, TheWind said: no that's just as generic as resource It's a very generic class Quote Link to comment Share on other sites More sharing options...
HeyImJamie Posted December 16, 2017 Share Posted December 16, 2017 Just call it priceGrabber Quote Link to comment Share on other sites More sharing options...
jca Posted December 16, 2017 Author Share Posted December 16, 2017 8 minutes ago, HeyImJamie said: Just call it priceGrabber Again, you can use this for more than storing the price. For example the amount collected / gained / fished / cut, the name, ID and really anything to do with the item that you need to cache if you build on top of this. Anything to do with price / GE would not describe the class accurately as it's not solely used for that. I'll leave it at ItemResource for now. Change it if you want to use it for something else. Quote Link to comment Share on other sites More sharing options...
HeyImJamie Posted December 16, 2017 Share Posted December 16, 2017 41 minutes ago, jca said: Again, you can use this for more than storing the price. For example the amount collected / gained / fished / cut, the name, ID and really anything to do with the item that you need to cache if you build on top of this. Anything to do with price / GE would not describe the class accurately as it's not solely used for that. I'll leave it at ItemResource for now. Change it if you want to use it for something else. I personally see no use for this class other than price grabbing and possibly storing, but then you might as well just store prices in a map. I'm pretty sure most of the code here is someone's else's anyway, but nice release anywho. Quote Link to comment Share on other sites More sharing options...