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:

Java Help. I have an array like is: And the arrays have

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):

a constructor that all do the same thing, get a file and

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:

puts them into a hashmap (All the constructors have a different file

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 tickerPrices = new 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 tickerPrices = new 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 tickerPrices = new 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

}

}

static public class StockMachine private StockPriceService[] stockPriceServices; private String[] tickerSymbols; public StockMachine (String fileName) throws IOException{ stockPriceservices - new StockPriceService[3]: stockPriceServices []-new UHStockService() stockPriceServices [1] = new NLPService(); stockPriceServices [2] ce(); new Externalservi

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!