Question: Java Help. I have an array like is: And the arrays have a constructor that all do the same thing, get a file and puts
Java Help.
I have an array like is:

And the arrays have a constructor that all do the same thing, get a file and puts them into a hashmap (All the constructors have a different file to open but and are similar to this):

I'm supposed to calculate which service (hashmap) has the lowest average and highest average, but can't see how to put the priceForTicker (in the interface )function to work in an array:

I'll appreciate the help, here my code to better understand:
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;
public class Main {
////////////////////////////////////////////////////////////////////////////////////////////
public interface StockPriceService{
double priceForTicker(String ticker);
String nameOfService();
}
////////////////////////////////////////////////////////////////////////////////////////////
static public class NLPService implements StockPriceService{
private HashMap
public NLPService() throws IOException {
String ticker;
Double price;
Scanner scanner = new Scanner(new FileReader("NLPService.txt"));
while(scanner.hasNext())
{
ticker = scanner.next();
price = scanner.nextDouble();
tickerPrices.put(ticker, price);
}
scanner.close();
}
@Override
public double priceForTicker(String ticker) {
// TODO Auto-generated method stub
if(tickerPrices.get(ticker) == null)
return 0;
else
return tickerPrices.get(ticker);
}
@Override
public String nameOfService() {
// TODO Auto-generated method stub
return "NLP";
}
}
////////////////////////////////////////////////////////////////////////////////////////////
static public class UHStockService implements StockPriceService{
private HashMap
public UHStockService() throws IOException {
String ticker;
Double price;
Scanner scanner = new Scanner(new FileReader("UHStockService.txt"));
while(scanner.hasNext())
{
ticker = scanner.next();
price = scanner.nextDouble();
tickerPrices.put(ticker, price);
}
scanner.close();
}
@Override
public double priceForTicker(String ticker) {
// TODO Auto-generated method stub
if(tickerPrices.get(ticker) == null)
return 0;
else
return tickerPrices.get(ticker);
}
@Override
public String nameOfService() {
// TODO Auto-generated method stub
return "UH";
}
}
////////////////////////////////////////////////////////////////////////////////////////////
static public class ExternalService implements StockPriceService{
private HashMap
public ExternalService () throws IOException {
String ticker;
Double price;
Scanner scanner = new Scanner(new FileReader("ExternalService.txt"));
while(scanner.hasNext())
{
ticker = scanner.next();
price = scanner.nextDouble();
tickerPrices.put(ticker, price);
}
scanner.close();
}
@Override
public double priceForTicker(String ticker) {
// TODO Auto-generated method stub
if(tickerPrices.get(ticker) == null)
return 0;
else
return tickerPrices.get(ticker);
}
@Override
public String nameOfService() {
// TODO Auto-generated method stub
return "External";
}
}
////////////////////////////////////////////////////////////////////////////////////////////
static public class StockMachine {
private StockPriceService[] stockPriceServices;
private String[] tickerSymbols;
public StockMachine(String fileName) throws IOException {
stockPriceServices = new StockPriceService[3];
stockPriceServices[0] = new UHStockService();
stockPriceServices[1] = new NLPService();
stockPriceServices[2] = new ExternalService();
}
public String getServiceWithLowestAvg() {
double sum;
for(int i = 0; i priceForTicke(stockPriceServices[i].get()); } } //////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) throws IOException { // TODO Auto-generated method stub } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
