Jump to content

ItemResource Class - Retrieves live item price from GE


jca

Recommended Posts

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 by jca
  • Like 1
Link to comment
Share on other sites

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 by jca
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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. :boge: I'm pretty sure most of the code here is someone's else's anyway, but nice release anywho.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...