Advertisement
Tyluur

Untitled

Jul 4th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. package org.zamorak.bot.methods;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.text.NumberFormat;
  9.  
  10. /**
  11.  * Gets prices for given items.
  12.  *
  13.  * @author Tyluur
  14.  * @since Jul 4, 2013
  15.  */
  16. public class PriceChecker {
  17.  
  18.     /**
  19.      * Example usage
  20.      * @param args
  21.      */
  22.     public static void main(String... args) {
  23.         String item = "Amulet of fury";
  24.         System.out.println("The price of " + item + " is: " + NumberFormat.getInstance().format(new ItemPrice(item).getPrice()));
  25.     }
  26.  
  27.     /**
  28.      * Parses the formatting of the web page and finds the price of an item
  29.      * @param name The name of the item
  30.      * @return The price of the item.
  31.      * @throws IOException
  32.      */
  33.     public long getPrice(String name) throws IOException {
  34.         String link = "http://forums.zybez.net/runescape-2007-prices/api/"
  35.                 + (name.replaceAll(" ", "+"));
  36.         URL url = new URL(link);
  37.         URLConnection con = url.openConnection();
  38.         BufferedReader in = new BufferedReader(new InputStreamReader(
  39.                 con.getInputStream()));
  40.         String line;
  41.         while ((line = in.readLine()) != null) {
  42.             if (line.contains("\"average\"")) {
  43.                 line = line.split("average")[1];
  44.                 line = line.split(",")[0];
  45.                 line = line.replaceAll("\"", "");
  46.                 line = line.replaceAll(":", "");
  47.                 return Math.round(Double.parseDouble(line));
  48.             }
  49.         }
  50.         return 0;
  51.     }
  52.  
  53.     /**
  54.      * The getter
  55.      * @return The instance of the class
  56.      */
  57.     public static PriceChecker get() {
  58.         return INSTANCE;
  59.     }
  60.  
  61.     /** The instance of this class. */
  62.     private static final PriceChecker INSTANCE = new PriceChecker();
  63.  
  64.     /**
  65.      * The wrapper for an item price based on the name.
  66.      *
  67.      * @author Tyluur
  68.      * @since July 4th, 2013
  69.      */
  70.     public static class ItemPrice {
  71.  
  72.         /**
  73.          * The new constructor for the item price
  74.          * @param name The name of the item
  75.          */
  76.         public ItemPrice(String name) {
  77.             this.name = name;
  78.         }
  79.  
  80.         /** The instance of the name of the item. */
  81.         private String name;
  82.  
  83.         /**
  84.          * Gets the name of the item
  85.          * @return The name of the item.
  86.          */
  87.         public String getName() {
  88.             return name;
  89.         }
  90.  
  91.         /**
  92.          * Gets the price of the item
  93.          * @return The price of the item
  94.          */
  95.         public long getPrice() {
  96.             try {
  97.                 return PriceChecker.get().getPrice(getName());
  98.             } catch (IOException e) {
  99.                 e.printStackTrace();
  100.                 return -1;
  101.             }
  102.         }
  103.  
  104.         /**
  105.          * Transforms the ItemPrice into a string when instantiated
  106.          */
  107.         @Override
  108.         public String toString() {
  109.             return "" + getPrice();
  110.         }
  111.  
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement