Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.zamorak.bot.methods;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.text.NumberFormat;
- /**
- * Gets prices for given items.
- *
- * @author Tyluur
- * @since Jul 4, 2013
- */
- public class PriceChecker {
- /**
- * Example usage
- * @param args
- */
- public static void main(String... args) {
- String item = "Amulet of fury";
- System.out.println("The price of " + item + " is: " + NumberFormat.getInstance().format(new ItemPrice(item).getPrice()));
- }
- /**
- * Parses the formatting of the web page and finds the price of an item
- * @param name The name of the item
- * @return The price of the item.
- * @throws IOException
- */
- public long getPrice(String name) throws IOException {
- String link = "http://forums.zybez.net/runescape-2007-prices/api/"
- + (name.replaceAll(" ", "+"));
- URL url = new URL(link);
- URLConnection con = url.openConnection();
- BufferedReader in = new BufferedReader(new InputStreamReader(
- con.getInputStream()));
- String line;
- while ((line = in.readLine()) != null) {
- if (line.contains("\"average\"")) {
- line = line.split("average")[1];
- line = line.split(",")[0];
- line = line.replaceAll("\"", "");
- line = line.replaceAll(":", "");
- return Math.round(Double.parseDouble(line));
- }
- }
- return 0;
- }
- /**
- * The getter
- * @return The instance of the class
- */
- public static PriceChecker get() {
- return INSTANCE;
- }
- /** The instance of this class. */
- private static final PriceChecker INSTANCE = new PriceChecker();
- /**
- * The wrapper for an item price based on the name.
- *
- * @author Tyluur
- * @since July 4th, 2013
- */
- public static class ItemPrice {
- /**
- * The new constructor for the item price
- * @param name The name of the item
- */
- public ItemPrice(String name) {
- this.name = name;
- }
- /** The instance of the name of the item. */
- private String name;
- /**
- * Gets the name of the item
- * @return The name of the item.
- */
- public String getName() {
- return name;
- }
- /**
- * Gets the price of the item
- * @return The price of the item
- */
- public long getPrice() {
- try {
- return PriceChecker.get().getPrice(getName());
- } catch (IOException e) {
- e.printStackTrace();
- return -1;
- }
- }
- /**
- * Transforms the ItemPrice into a string when instantiated
- */
- @Override
- public String toString() {
- return "" + getPrice();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement