Advertisement
Guest User

Grand Exchange Price Class

a guest
Mar 30th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package com.reiddacosta.ge;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7.  
  8. /**
  9.  * GrandExchange Price Class
  10.  *
  11.  * @author Reid
  12.  *
  13.  */
  14. public class GrandExchange {
  15.  
  16.     private static final String BASE = "https://api.rsbuddy.com/grandExchange?a=guidePrice&i=";
  17.     private String choice;
  18.  
  19.     /**
  20.      * Default Constructor
  21.      *
  22.      */
  23.     public GrandExchange() {
  24.  
  25.     }
  26.    
  27.     /**
  28.      * Gets the overall price of an item.
  29.      *
  30.      * @param itemID
  31.      * @return itemPrice
  32.      * @throws IOException
  33.      */
  34.     public int getOverallPrice(final int itemID) throws IOException {
  35.         choice = "overall";
  36.         return parse(itemID);
  37.     }
  38.  
  39.    
  40.     /**
  41.      * Gets the buying price of an item.
  42.      *
  43.      * @param itemID
  44.      * @return itemPrice
  45.      * @throws IOException
  46.      */
  47.     public int getBuyingPrice(final int itemID) throws IOException {
  48.         choice = "buying";
  49.         return parse(itemID);
  50.     }
  51.    
  52.     /**
  53.      * Gets the selling price of an item.
  54.      *
  55.      * @param itemID
  56.      * @return itemPrice
  57.      * @throws IOException
  58.      */
  59.     public int getSellingPrice(final int itemID) throws IOException {
  60.         choice = "selling";
  61.         return parse(itemID);
  62.     }
  63.  
  64.  
  65.     /**
  66.      * Retrieves the price of an item.
  67.      *
  68.      * @param itemID
  69.      * @return itemPrice
  70.      * @throws IOException
  71.      */
  72.     private int parse(final int itemID) throws IOException {
  73.         final URL url = new URL(BASE + itemID);
  74.         BufferedReader file = new BufferedReader(new InputStreamReader(url.openStream()));
  75.         String line;
  76.         String price = null;
  77.         while ((line = file.readLine()) != null) {
  78.             if (line.contains("{")) {
  79.                 price = (line).trim();
  80.             }
  81.         }
  82.         if (choice.equals("buying")){
  83.             price = price.substring(price.indexOf(",") + 11, price.lastIndexOf(",") - 1).trim();
  84.         } else if(choice.equals("selling")) {
  85.             price = price.substring(price.lastIndexOf(",") + 12, price.indexOf("}") - 1).trim();
  86.         } else {
  87.             price = price.substring(price.indexOf("{") + 12, price.indexOf(",") - 1).trim();
  88.         }
  89.         file.close();
  90.         return Integer.parseInt(price);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement