Jump to content

PriceWrapper [Collect and Store item values]


NotoriousPP

Recommended Posts

Well I thought today I would release some of my private collection of different snippets to hopefully help new writers learn a little bit, and to maybe see a rise in quality of scripts throughout this forum.

 

My third release is a PriceWrapper, which can keep track of items price using Zybez and then storing the collected data. This is very easy to use, and most of all very optimal in terms of grabbing prices via URL. Instead of directly looking up a items value each time (You can still do this, but not recommended), it will store the items value in HashMap and then return the value, making much more efficient, and if the price is not in our pricemap, it will add it first, then return the value.

 

What does this class actually do?

Collect and store items value using Hashmaps and Zybez.

 

How does is work?
In the example below, I will show how to correctly call this.

private PriceWrapper prices = new PriceWrapper();

// Will add items to map without returning a value
prices.addItemsToMap("Bones", "Big bones", "Dragon bones");
// Will get items from map returning the items value
prices.getItemPrice("Bones");
// Will add item that is not already in map, and return it's value
prices.getItemPrice("Iron ore");
// Will get items price from Zyebez, not the map
prices.lookUpItemPrice("Onion");
// Will update the items price in the map
prices.updatePrice("Bones");

 

Finally, where the magic happens:
The source code to the PriceWrapper class!

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

public class PriceWrapper {

    private HashMap<String, Integer> priceMap = new HashMap<>();

    /**
     * Get the price of a item in our price map, if our map
     * doesn't contain price, then look it up and add it to map.
     *
     * @param name The name of the item you wish to get the value of.
     * @return Item value that was stored in our pricemap.
     */
    public int getItemPrice(String name){
        if(!priceMap.containsKey(name))
            addItemsToMap(name);
        return priceMap.get(name);
    }

    /**
     * Adds items to the price map, to be retrieved for later use.
     * @param names Names of the items you want to store values of.
     */
    public void addItemsToMap(String... names){
        for(String s : names){
            priceMap.put(s, lookUpItemPrice(s));
        }
    }

    /**
     *
     * @param name The name of the item you wish to update the price
     */
    public void updatePrice(String name){
        priceMap.remove(name);
        priceMap.put(name, lookUpItemPrice(name));
    }

    /**
     *  Directly looks up item prices using Zybez.
     * @param name Name of the item you wish to check the price for.
     * @return int of the amount of the items value.
     */
    public int lookUpItemPrice(String name) {
        String info = getInfo(name);
        if(info != null && info.contains(name)){
            info = info.substring(info.indexOf("average") + 10);
            info = info.substring(0, info.indexOf(",") - 1);
            if(info.contains(".")){
                info = info.substring(0, info.indexOf('.'));
            }
            System.out.println(Integer.parseInt(info));
            return Integer.parseInt(info);
        }
        return 0;
    }

    /**
     *
     * @param name Name of the item you wish to check the price for.
     * @return String of data we retrieve from Zybez.
     */
    private String getInfo(String name) {
        name = name.replaceAll(" ", "%20");
        try {
            String text = "http://forums.zybez.net/runescape-2007-prices/api/" + name;
            System.out.println(text);
            URL url = new URL(text);
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0");
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String f = in.readLine();
            in.close();
            return f;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

Questions/Comments?:
If you see anything I messed up on, or should be improved, please let me know, but be respectful about it, we have too many keyboard warriors thinking their hot shit, yet do nothing but bash others and never give any useful resources.
Even if you have a question, free to ask me, just please refrain from asking me blatant obvious questions, or ones you did little to no research on before asking, I'm not here to spoon feed you, though I am willing to help someone is trying.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Very usefull.

Thx man !

 

But...

 

return me wrong values for dragon bones.

This is because Babydragon bones are in front of Dragon bones in the list.

U have to search for name in info and make it case sensitive and problem solved.

 

 

change:

public int lookUpItemPrice(String name) {
String info = getInfo(name);
if(info != null && info.contains(name)){
info = info.substring(info.indexOf("average") + 10);
info = info.substring(0, info.indexOf(",") - 1);
if(info.contains(".")){
info = info.substring(0, info.indexOf('.'));
}
System.out.println(Integer.parseInt(info));
return Integer.parseInt(info);
}
return 0;
}

to

public int lookUpItemPrice(String name) {
String info = getInfo(name);
if(info != null && info.contains(name)){
info = info.substring(info.indexOf(name));// <-------- added
info = info.substring(info.indexOf("average") + 10);
info = info.substring(0, info.indexOf(",") - 1);
if(info.contains(".")){
info = info.substring(0, info.indexOf('.'));
}
System.out.println(Integer.parseInt(info));
return Integer.parseInt(info);
}
return 0;
}

is this right?

EDIT: this is right !

Edited by fre024
  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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