Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions BasicServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.origamisoftware.teach.advanced.services;

import com.origamisoftware.teach.advanced.model.StockQuote;
import com.origamisoftware.teach.advanced.util.Interval;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.*;

/**
* BasicServlet request data from the web service that is used by the WebStockService
*/
public class BasicServlet extends HttpServlet{
private String symbol;
private Calendar date;

public BasicServlet(String symbol, Calendar date){
this.symbol = symbol;
this.date = date;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("http://finance.yahoo.com/d/quotes.csv?s="+ symbol + "d2=" + date);
out.println("</body>");
out.println("</html>");
}
}
28 changes: 28 additions & 0 deletions ServiceFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.origamisoftware.teach.advanced.services;

/**
* A factory that returns a Services.
*/
public class ServiceFactory {

/**
* Prevent instantiations
*/
private ServiceFactory() {}

/**
*
* @return get a <CODE>StockService</CODE> instance
*/
public static WebStockService getStockService() {
return new WebStockService();
}

/**
*
* @return get a <CODE>UserService</CODE> instance
*/
public static UserService getUserService() {
return new DatabaseUserService();
}
}
14 changes: 14 additions & 0 deletions WebServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.origamisoftware.teach.advanced.services;


import org.junit.Test;
import static org.junit.Assert.assertNotNull;

public class WebServiceTest {

@Test
public void testGetStockServiceInstance() {
WebStockService webStockService = ServiceFactory.getStockService();
assertNotNull(webStockService);
}
}
46 changes: 46 additions & 0 deletions WebStockService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.origamisoftware.teach.advanced.services;

import com.origamisoftware.teach.advanced.model.StockQuote;
import com.origamisoftware.teach.advanced.util.Interval;
import yahoofinance.Stock;
import yahoofinance.YahooFinance;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* I assume this interacts with the YF API directly?
* not needing the servlet
* I made a simple servlet but couldn't understand how to
* integrate it into the WenStockService
*/
public class WebStockService implements StockService {
private Stock stock;

@Override
public StockQuote getQuote(String symbol) throws StockServiceException, IOException {
stock = YahooFinance.get(symbol);
String yfSymbol = stock.toString();

return new StockQuote(new BigDecimal(100), Calendar.getInstance().getTime(), yfSymbol);
}

@Override
public List<StockQuote> getQuote(String symbol, Calendar from, Calendar until, Interval interval) throws StockServiceException, IOException {
List<StockQuote> stockQuotes = new ArrayList<>();

stock = YahooFinance.get(symbol, from, until);
BigDecimal price = stock.getQuote().getPrice();
Date aDay = (Date) stock.getHistory();
String yfSymbol = stock.getSymbol();

stockQuotes.add(new StockQuote(price, aDay, yfSymbol));

return stockQuotes;
}
}