-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicStockQuoteApplication.java
More file actions
177 lines (153 loc) · 7.36 KB
/
BasicStockQuoteApplication.java
File metadata and controls
177 lines (153 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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.ServiceFactory;
import com.origamisoftware.teach.advanced.util.Interval;
import com.origamisoftware.teach.advanced.xml.Stock;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
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(),
Interval.DAY); // get one quote for each day in the from until date range.
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/>
*
* @param args one or more stock symbols
*/
private static String xmlInstance =
"<stocks>\n" +
" <stock symbol=\"VNET\" price=\"110.10\" time=\"2015-02-10 00:00:01\"/>\n" +
" <stock symbol=\"AGTK\" price=\"120.10\" time=\"2015-02-10 00:00:01\"/>\n" +
" <stock symbol=\"AKAM\" price=\"3.10\" time=\"2015-02-10 00:00:01\"/>\n" +
" <stock symbol=\"AOL\" price=\"30.10\" time=\"2015-02-10 00:00:01\"/>\n" +
"</stocks>";
public static void main(String[] args) throws JAXBException {
// 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 = ServiceFactory.getStockService();
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();
} catch (Throwable t) {
exitStatus = ProgramTerminationStatusEnum.ABNORMAL;
programTerminationMessage = "General application error: " + t.getMessage();
}
// here is how to go from XML to Java
JAXBContext jaxbContext = JAXBContext.newInstance(Stock.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Stock stock = (Stock) unmarshaller.unmarshal(new StringReader(xmlInstance));
System.out.println(stock.toString());
// here is how to go from Java to XML
JAXBContext context = JAXBContext.newInstance(Stock.class);
Marshaller marshaller = context.createMarshaller();
//for pretty-print XML in JAXB
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(stock, System.out);
exit(exitStatus, programTerminationMessage);
System.out.println("Oops could not parse a date");
}
}