Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.reiddacosta.ge;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- /**
- * GrandExchange Price Class
- *
- * @author Reid
- *
- */
- public class GrandExchange {
- private static final String BASE = "https://api.rsbuddy.com/grandExchange?a=guidePrice&i=";
- private String choice;
- /**
- * Default Constructor
- *
- */
- public GrandExchange() {
- }
- /**
- * Gets the overall price of an item.
- *
- * @param itemID
- * @return itemPrice
- * @throws IOException
- */
- public int getOverallPrice(final int itemID) throws IOException {
- choice = "overall";
- return parse(itemID);
- }
- /**
- * Gets the buying price of an item.
- *
- * @param itemID
- * @return itemPrice
- * @throws IOException
- */
- public int getBuyingPrice(final int itemID) throws IOException {
- choice = "buying";
- return parse(itemID);
- }
- /**
- * Gets the selling price of an item.
- *
- * @param itemID
- * @return itemPrice
- * @throws IOException
- */
- public int getSellingPrice(final int itemID) throws IOException {
- choice = "selling";
- return parse(itemID);
- }
- /**
- * Retrieves the price of an item.
- *
- * @param itemID
- * @return itemPrice
- * @throws IOException
- */
- private int parse(final int itemID) throws IOException {
- final URL url = new URL(BASE + itemID);
- BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream()));
- String line;
- String price = null;
- while ((line = file.readLine()) != null) {
- if (line.contains("{")) {
- price = (line).trim();
- }
- }
- if (choice.equals("buying")){
- price = price.substring(price.indexOf(",") + 11, price.lastIndexOf(",") - 1).trim();
- } else if(choice.equals("selling")) {
- price = price.substring(price.lastIndexOf(",") + 12, price.indexOf("}") - 1).trim();
- } else {
- price = price.substring(price.indexOf("{") + 12, price.indexOf(",") - 1).trim();
- }
- file.close();
- return Integer.parseInt(price);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement