package cryptowatch;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonValue;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.io.IOUtils;

public class Market {
    static String rawBittrex;
    
    public static void startTimer(){
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                String url = "https://bittrex.com/api/v1.1/public/getmarketsummaries";
                try {
                    URL url2 = new URL(url);
                    URLConnection con = url2.openConnection();
                    InputStream in = con.getInputStream();
                    String encoding = "UTF-8";
                    rawBittrex = IOUtils.toString(in, encoding);
                }
                catch(MalformedURLException e) {}
                catch(IOException e) {}
            }
        }, 0,5000);
    }
    
    public static float getPrice(String exchange, String market) {
        if(exchange.equals("bittrex")) {
            JsonArray arrayBittrex = Json.parse(rawBittrex).asObject().get("result").asArray();
            int numArrays = Json.parse(rawBittrex).asObject().get("result").asArray().size();

            for(int i=0; i<numArrays; i++){
                if(market.equalsIgnoreCase(arrayBittrex.get(i).asObject().getString("MarketName", ""))) {
                    float last = arrayBittrex.get(i).asObject().getFloat("Last", 0);
                    return last;
                }
            }
        }
        return 0;
    } 
}

@@ -434,18 +434,23 @@
 tic 
-String raw
+JsonArray array
 Bitt
@@ -980,16 +980,23 @@
         
+String 
 rawBittr
@@ -1032,16 +1032,110 @@
 oding);%0A
+                    arrayBittrex = Json.parse(rawBittrex).asObject().get(%22result%22).asArray();%0A
         
@@ -1391,104 +1391,8 @@
 ) %7B%0A
-            JsonArray arrayBittrex = Json.parse(rawBittrex).asObject().get(%22result%22).asArray();%0A
     
@@ -1419,65 +1419,20 @@
 s = 
-Json.parse(raw
+array
 Bittrex
-).asObject().get(%22result%22).asArray()
 .siz
@@ -1436,17 +1436,16 @@
 size();%0A
-%0A
         

Reorganize strings and arrays