Question
One of the important uses of middleware such as RMI is in integrating legacy systems in new applications. In this assignment you will produce a
One of the important uses of middleware such as RMI is in integrating legacy systems in new applications. In this assignment you will produce a simple RMI stock quote service based on Alpha Vantage's web based finance API, and a simple RMI client to test your server.
The Server
Your server should have a single method in its remote interface:
public StockQuote getQuote (String symbol);
The getQuote method takes a stock symbol (e.g. FB, APPL, GOOG, MSFT, etc.) and returns a StockQuote object. StockQuote should be a simple class with public fields for currentPrice, priceChange, dailyHigh, and dailyLow.
The following program demonstrates how to get this data for a stock from Alpha Vantage using HTTP:
import java.io.*;
import java.net.*;
import java.text.DateFormat;
import java.text.DecimalFormat;
public class StockQuery {
public static void main(String[] args) {
String ticker = "MSFT"; // microsoft ticker symbol
String api_key = "demo";
DecimalFormat number = new DecimalFormat();
DecimalFormat dollars = new DecimalFormat("$#,##0.00;-$#,##0.00");
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
try {
URL url = new URL("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" + ticker + "&apikey=" + api_key + "&datatype=csv");
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
in.readLine(); // skip header row
String quoteString = in.readLine();
in.close();
if (quoteString == null) {
System.out.println("Requested data is not available.");
System.exit(0);
}
String[] data = quoteString.split(",");
if (data.length != 6) {
System.out.println("Bad output: " + quoteString);
System.exit(0);
}
java.sql.Date date = java.sql.Date.valueOf(data[0]);
double open = Double.parseDouble(data[1]);
double high = Double.parseDouble(data[2]);
double low = Double.parseDouble(data[3]);
double close = Double.parseDouble(data[4]);
double volume = Double.parseDouble(data[5]);
System.out.println(ticker + " as of " + df.format(date));
System.out.println("Current price: " + dollars.format(close));
System.out.println("Change: " + dollars.format(close - open));
System.out.println("Daily High: " + dollars.format(high));
System.out.println("Daily Low: " + dollars.format(low));
System.out.println("Volume: " + number.format(volume) + " shares");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Notes:
- The Alpha Vantage service returns a file in CSV (comma separated value) format. The first line is a header string, followed by one line of data for each of the most recent 100 days. The data is sorted from newest to oldest, so the first line of data is the most recent. Each line of data contains six values: date, opening price, daily high, daily low, closing price, and volume (number of shares traded).
- The URL contains four parameters: function, symbol, apikey, and datatype. The "demo" apikey will only work for MSFT (Microsoft). To get data for other stocks you need to register with Alpha Vantage and get your own API Key (this is free) here. You can also find full documentation for the Alpha Vantage api.
The Client
Your client program should allow the user to enter a stock symbol and view the current price, price change, daily high, and daily low for the stock. You may provide a graphical user interface, or just use console I/O.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started