How should I get the lowest price?

Discussion in 'Plugin Development' started by MayoDwarf, Dec 23, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    MayoDwarf

    So I am making an mcpvp gold plugin. I have everything done. For the items, I used a kind of constructor using files! Neat, I know I know! :p The thing I am trying to accomplish now is getting the lowest price and giving the player that. Also then if it is not the amount they want how I would loop through it again and give them the lowest price and more of it and so on until they have how much they bought of it. Thanks - CoderJared :)
     
  2. Offline

    ZeusAllMighty11 Retired Staff

    I don't understand.
     
  3. Offline

    MayoDwarf

    One second. I'll edit this in a second.

    So I made 4 files. They each have something. When someone sells something they do /sell <item> <price> <amount> So the player name goes in one file as 1. Item goes in a items.yml as 1. Price goes in a prices.yml. Amount goes in an amount.yml. How to get these is by getting their number index. They are all 1, it acts as a storage mechanism. I was wondering how I could get the lowest priced item and then give them the amount of that item, and if the amount wasn't what they asked for, to keep going until they got the amount asked for the lowest price every time. Thanks - Jared ZeusAllMighty11

    ZeusAllMighty11 Any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    ZeusAllMighty11 Retired Staff

    Sorry, I still have no idea what you're doing. Good luck, though.
     
  5. Offline

    EcMiner

    You should store the prices + description whatever in a HashMap. If you loop through all values and keys in a map it will by default show the lowest value first, like this:
    Code:java
    1. HashMap<Integer, String> map = new HashMap<Integer, String>();
    2. map.put(5, "five");
    3. map.put(3, "three");
    4. map.put(4, "four");
    5. map.put(2, "two");
    6. map.put(1, "one");
    7.  
    8. // Loop through all keys and values:
    9. for(Map.Entry<Integer, String> e : map.entrySet()) {
    10. System.out.println(e.getKey() + ":" + e.getValue());
    11. }
    12.  


    the output will be:
    1: one
    2:two
    3:three
    4:four
    5:five
     
  6. Offline

    MayoDwarf

    You obviously do not understand the post. This is storage for permanent use not temporary. I have file lists. I want to know how to get the lowest price of it and add that to their inventory until they get the amount they paid for at the lowest price each time. There is 5 diamonds on market for 1 cent. Bob buys 10. He gets the 5 for 1 cent. Then it would keep going until he got the amount he asked for at the lowest price everytime. How would I do this? EcMiner ZeusAllMighty11
     
  7. Offline

    ZeusAllMighty11 Retired Staff

    MayoDwarf

    Well I really don't know what you're doing, but @EcMiner's way is a lot easier. And yes, you can store a hashmap to a file and retrieve it back, easily.
     
    EcMiner likes this.
  8. Offline

    EcMiner


    You just lost me...
     
  9. Offline

    MayoDwarf

    Alright so you know how you can make lists in files? So when someone sells something is set the different args in different files in the same index. So it would set f d w y in different files but all the same index, 1. When you would get them with filename.get(1) filename2.get(1) and so on, it would get that data, acting as a list that holds multiple items. Understand? EcMiner
     
  10. Offline

    Heracles421

    MayoDwarf
    Just save the stuff in the files with a different index using an integer variable.
    When you retrieve the data use the same variable to get that index.
     
  11. Offline

    NathanWolf

    I think you are unnecessarily complicating this!

    For one, you are forcing your implementation to revolve around your file format. You shouldn't be dealing with the files except to save and load data. You're not loading and scanning files constantly every time a sale or purchase is made...... right??

    I'm not sure how many items you plan on storing, but for a smallish amount you could probably start very unoptimized... like, why not just have a list of objects (with price, amount, item type, etc)- and loop through it to find the lowest priced item that they want.

    If you need to optimize from there, then I agree about using hashmaps, but I would still keep the one list for reading+writing from a file easily. I think what I would do to optimize this would be to make a hashmap by item type to a list- the list being a list of all items of that type, sorted by price.

    Anyway, make it work first *then* worry about saving to files- it's not hard to persist lists of objects, so don't base your entire implementation on how you think the files should look. My 2 cents anyway!
     
  12. Offline

    MayoDwarf

    Heracles421
    What?

    NathanWolf It's not a smallish amount however.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  13. Offline

    NathanWolf

    Iterating over a list is still going to perform better than loading and searching through several different config files each time.

    But if you wanted to optimize up front, you could load and save directly to a hashmap. I think map of item types to a list if items (which you could keep sorted by price) would work out well.
     
  14. Offline

    Heracles421

    MayoDwarf
    Sorry, I think that I still don't get what you want to do...
     
  15. Offline

    HyrulesLegend

    I know what he's trying to do, but I'm not sure how to go about it. Gl with this mayo. (Anyone who plays MCPVP's hardcore/raid servers will understand what he's trying to accomplish.)
     
    MayoDwarf likes this.
  16. Offline

    MayoDwarf

    Yah I guess. I could do it onEnable. The only thing I have a problem with is how I would get the lowest price as I said and give them the amount of that item and then if they don't get the amount they bought, to keep going until they have gotten the items at the lowest price each time.

    Thank you :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  17. Offline

    NathanWolf

    The idea with the hashmap is you can get a list of item objects from it. Each list would be mapped by the type of item it contains, and sorted by price.

    So at that point you just get the first item in the list, if it does not have enough, go to the next one and so on. Remove the items as they get used up, or reduce the amounts for partial sales.
     
Thread Status:
Not open for further replies.

Share This Page