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
73 changes: 73 additions & 0 deletions stockquote-week5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.origamisoftware.teach.effective</groupId>
<artifactId>week-5-stock-ticker</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>starter-app</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerArgument>-Xlint</compilerArgument>
<encoding>${project.build.sourceEncoding}</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4</version>
</dependency>

<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<version>2.3.4.726</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.origamisoftware.teach.advanced.apps.stockquote;

import com.origamisoftware.teach.advanced.model.StockQuery;
import com.origamisoftware.teach.advanced.model.StockQuote;
import com.origamisoftware.teach.advanced.services.StockService;
import com.origamisoftware.teach.advanced.services.StockServiceException;
import com.origamisoftware.teach.advanced.services.StockServiceFactory;

import java.text.ParseException;
import java.util.List;

/**
* A simple application that shows the StockService in action.
*/
public class BasicStockQuoteApplication {

private StockService stockService;

// an example of how to use enum - not part of assignment 3 but useful for assignment 4

/**
* An enumeration that indicates how the program terminates (ends)
*/
private enum ProgramTerminationStatusEnum {

// for now, we just have normal or abnormal but could more specific ones as needed.
NORMAL(0),
ABNORMAL(-1);

// when the program exits, this value will be reported to underlying OS
private int statusCode;

/**
* Create a new ProgramTerminationStatusEnum
*
* @param statusCodeValue the value to return the OS. A value of 0
* indicates success or normal termination.
* non 0 numbers indicate abnormal termination.
*/
private ProgramTerminationStatusEnum(int statusCodeValue) {
this.statusCode = statusCodeValue;
}

/**
* @return The value sent to OS when the program ends.
*/
private int getStatusCode() {
return statusCode;
}
}

/**
* Create a new Application.
*
* @param stockService the StockService this application instance should use for
* stock queries.
* <p/>
* NOTE: this is a example of Dependency Injection in action.
*/
public BasicStockQuoteApplication(StockService stockService) {
this.stockService = stockService;
}

/**
* Given a <CODE>stockQuery</CODE> get back a the info about the stock to display to th user.
*
* @param stockQuery the stock to get data for.
* @return a String with the stock data in it.
* @throws StockServiceException If data about the stock can't be retrieved. This is a
* fatal error.
*/
public String displayStockQuotes(StockQuery stockQuery) throws StockServiceException {
StringBuilder stringBuilder = new StringBuilder();

List<StockQuote> stockQuotes =
stockService.getQuote(stockQuery.getSymbol(), stockQuery.getFrom(), stockQuery.getUntil());

stringBuilder.append("Stock quotes for: " + stockQuery.getSymbol() + "\n");
for (StockQuote stockQuote : stockQuotes) {
stringBuilder.append(stockQuote.toString());
}

return stringBuilder.toString();
}

/**
* Terminate the application.
*
* @param statusCode an enum value that indicates if the program terminated ok or not.
* @param diagnosticMessage A message to display to the user when the program ends.
* This should be an error message in the case of abnormal termination
* <p/>
* NOTE: This is an example of DRY in action.
* A program should only have one exit point. This makes it easy to do any clean up
* operations before a program quits from just one place in the code.
* It also makes for a consistent user experience.
*/
private static void exit(ProgramTerminationStatusEnum statusCode, String diagnosticMessage) {
if (statusCode == ProgramTerminationStatusEnum.NORMAL) {
System.out.println(diagnosticMessage);
} else if (statusCode == ProgramTerminationStatusEnum.ABNORMAL) {
System.err.println(diagnosticMessage);
} else {
throw new IllegalStateException("Unknown ProgramTerminationStatusEnum.");
}
System.exit(statusCode.getStatusCode());
}

/**
* Run the StockTicker application.
* <p/>
* When invoking the program supply one ore more stock symbols.
*
* @param args one or more stock symbols
*/
public static void main(String[] args) {

// be optimistic init to positive values
ProgramTerminationStatusEnum exitStatus = ProgramTerminationStatusEnum.NORMAL;
String programTerminationMessage = "Normal program termination.";
if (args.length != 3) {
exit(ProgramTerminationStatusEnum.ABNORMAL,
"Please supply 3 arguments a stock symbol, a start date (MM/DD/YYYY) and end date (MM/DD/YYYY)");
}
try {

StockQuery stockQuery = new StockQuery(args[0], args[1], args[2]);
StockService stockService = StockServiceFactory.getInstance();
BasicStockQuoteApplication basicStockQuoteApplication =
new BasicStockQuoteApplication(stockService);
basicStockQuoteApplication.displayStockQuotes(stockQuery);

} catch (ParseException e) {
exitStatus = ProgramTerminationStatusEnum.ABNORMAL;
programTerminationMessage = "Invalid date data: " + e.getMessage();
} catch (StockServiceException e) {
exitStatus = ProgramTerminationStatusEnum.ABNORMAL;
programTerminationMessage = "StockService failed: " + e.getMessage();
}

exit(exitStatus, programTerminationMessage);
System.out.println("Oops could not parse a date");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.origamisoftware.teach.advanced.model;

import java.text.SimpleDateFormat;

/**
* Abstract Base class for classes that hold Stock data.
* Provides common code for such classes including date formatting.
*/
public abstract class StockData {

/**
* Provide a single SimpleDateFormat for consistency
* and to avoid duplicated code.
*/
protected SimpleDateFormat simpleDateFormat;

/**
* Base constructor for StockData classes.
* Initialize member data that is shared with sub classes.
*/
public StockData() {
simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.origamisoftware.teach.advanced.model;


import org.apache.http.annotation.Immutable;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.Calendar;

/**
* This class is used to a single query to stock service.
*/
@Immutable
public class StockQuery extends StockData{

private String symbol;
private Calendar from;
private Calendar until;

/**
* Create a new instance from string data. This constructor will convert
* dates described as a String to Date objects.
*
* @param symbol the stock symbol
* @param from the start date as a string in the form of yyyy/MM/dd
* @param from the end date as a string in the form of yyyy/MM/dd
* @throws ParseException if the format of the date String is incorrect. If this happens
* the only recourse is to try again with a correctly formatted String.
*/
public StockQuery(@NotNull String symbol, @NotNull String from, @NotNull String until) throws ParseException {
super();
this.symbol = symbol;
this.from = Calendar.getInstance();
this.until = Calendar.getInstance();
System.out.println(simpleDateFormat);
this.from.setTime(simpleDateFormat.parse(from));
this.until.setTime(simpleDateFormat.parse(until));
}

/**
* @return get the stock symbol associated with this query
*/
public String getSymbol() {
return symbol;
}

/**
* @return get the start Calendar associated with this query
*/
public Calendar getFrom() {
return from;
}

/**
* @return get the end Calendar associated with this query
*/
public Calendar getUntil() {
return until;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.origamisoftware.teach.advanced.model;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
* A container class that contains stock data.
*/
public class StockQuote extends StockData {

private BigDecimal price;
private Date date;
private String symbol;
private Calendar from;
private Calendar until;

/**
* Create a new instance of a StockQuote.
*
* @param price the share price for the given date
* @param date the date of the share price
* @param symbol the stock symbol.
*/
public StockQuote(BigDecimal price, Date date, String symbol) {
super();
this.price = price;
this.date = date;
this.symbol = symbol;
}
/**
* @return Get the share price for the given date.
*/
public BigDecimal getPrice() {
return price;
}

/**
* @return The date of the share price
*/
public Date getDate() {
return date;
}

/**
* @return The stock symbol.
*/
public String getSymbol() {
return symbol;
}

@Override
public String toString() {
String dateString = simpleDateFormat.format(date);
return "StockQuote{" +
"price=" + price +
", date=" + dateString +
", symbol='" + symbol + '\'' +
'}';
}
}
Loading