Skip to content

Instantly share code, notes, and snippets.

@raunaqsingh2020
Created February 16, 2020 02:05
Show Gist options
  • Save raunaqsingh2020/91ec6e95dc25fbcf7f6954a44b9f57dd to your computer and use it in GitHub Desktop.
Save raunaqsingh2020/91ec6e95dc25fbcf7f6954a44b9f57dd to your computer and use it in GitHub Desktop.
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.util.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class StockJson
{
public StockJson(){
String[] companies = {"AAPL","ALL","AMZN","GOOGL","NFLX","FB","SEB","NVR","BKNG","MKL","TSLA","SHOP","PRTS","WMT","T","VZ","F","IRBT","W"};
float[] prices = new float[companies.length];
for(int i=0; i < companies.length;i++){
prices[i] = findPrice(companies[i]);
companies[i] = findName(companies[i]) + " (" + companies[i] + ")";
}
/*
HashTable table = new HashTable((int)(companies.length*1.7));
for(int i = 0; i < companies.length; i++){
HashNode node = new HashNode(companies[i], findPrice(companies[i]));
table.add(node);
}
Scanner sc = new Scanner(System.in);
System.out.println("Press 'Q' to quit.");
boolean quit = false;
while(!quit){
System.out.println("Enter a ticker symbol (AAPL,ALL,AMZN,GOOGL,NFLX,FB,SEB,NVR,BKNG,MKL,TSLA,SHOP,PRTS,WMT,T,VZ,F,IRBT,W)");
String symbol = sc.next();
if(symbol.equals("Q")){
quit = true;
break;
}
table.find(symbol);
}
*/
//System.out.println(Arrays.toString(prices));
//Selection Sort
for(int i = 0; i < prices.length; i++){
float min = 1000000;
String min2 = "";
int spot = 0;
for(int j = i; j < prices.length; j++){
if(prices[j]<min){
min = prices[j];
min2 = companies[j];
spot = j;
}
}
float temp = prices[i];
prices[i] = min;
prices[spot] = temp;
String temp2 = companies[i];
companies[i] = min2;
companies[spot] = temp2;
}
for(int i=0; i < companies.length;i++){
System.out.println(companies[i] + " - $" + prices[i]);
}
}
private static String streamToString(InputStream inputStream) {
String text = new Scanner(inputStream, "UTF-8").useDelimiter("\\Z").next();
return text;
}
public static String jsonGetRequest(String urlQueryString) {
String json = null;
try {
URL url = new URL(urlQueryString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "utf-8");
connection.connect();
InputStream inStream = connection.getInputStream();
json = streamToString(inStream); // input stream to string
} catch (IOException ex) {
ex.printStackTrace();
}
return json;
}
public float findPrice(String company){
String csvString = "";
URL url = null;
URLConnection urlConn = null;
InputStreamReader inStream = null;
BufferedReader buff = null;
try{
url = new URL("https://financialmodelingprep.com/api/company/profile/" + company);
urlConn = url.openConnection();
inStream = new InputStreamReader(urlConn.getInputStream());
buff= new BufferedReader(inStream);
// get the quote as a csv string
String s;
while ((s=buff.readLine())!=null){
// System.out.println(buff.readLine());
csvString += buff.readLine();
csvString += buff.readLine();
}
//System.out.println("test: " + csvString);
//String[] fd = loadStrings("https://financialmodelingprep.com/api/company/profile/" + sym);
} catch(Exception e){
System.out.println("ERROR, no PE");
}
//System.out.println(csvString.indexOf("\"Price\": "));
String temp = csvString.substring(csvString.indexOf("\"Price\": ")+9,csvString.indexOf(","));
//System.out.println(company + temp);
return Float.parseFloat(temp);
}
public String findName(String company){
String csvString = "";
URL url = null;
URLConnection urlConn = null;
InputStreamReader inStream = null;
BufferedReader buff = null;
try{
url = new URL("https://financialmodelingprep.com/api/company/profile/" + company);
urlConn = url.openConnection();
inStream = new InputStreamReader(urlConn.getInputStream());
buff= new BufferedReader(inStream);
// get the quote as a csv string
String s;
while ((s=buff.readLine())!=null){
// System.out.println(buff.readLine());
csvString += buff.readLine();
csvString += buff.readLine();
}
//System.out.println("test: " + csvString);
//String[] fd = loadStrings("https://financialmodelingprep.com/api/company/profile/" + sym);
} catch(Exception e){
System.out.println("ERROR, no PE");
}
//System.out.println(csvString.indexOf("\"Price\": "));
String temp = csvString.substring(csvString.indexOf("\"companyName\": \"")+16);
temp = temp.substring(0,temp.indexOf("\","));
//csvString.indexOf("\",")
//System.out.println(temp);
return temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment