-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
299 lines (256 loc) · 11.1 KB
/
Copy pathMain.java
File metadata and controls
299 lines (256 loc) · 11.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import java.io.File;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Collections;
import java.util.InputMismatchException;
import java.nio.file.Files;
//this program reads a list of trades from a CSV file
//and provides basic analytics on the trades
public class Main {
public static void main(String[] args) {
//scanner to read user input from command line
Scanner sc = new Scanner(System.in);
//array of trade objects
ArrayList<Trade> trades = new ArrayList<Trade>();
int option=-1;
while(true){
System.out.println("Select the command:\n" +
"1-Add trade\n2-Delete trade\n3-Totals by instrument\n" +
"4-Totals by User\n5-Print\n0-Exit");
try{
option=sc.nextInt(); //read input from scanner
}catch (InputMismatchException ex){
System.out.println("Error: Please enter a valid option");
sc.nextLine();
continue;
}
switch (option){
case 0:
System.exit(0); //exit the system
break;
case 1:
trades = addTrade(sc); //specify a file and load the trades into memory
System.out.println("All trades were successfully loaded into memory");
break;
case 2:
deleteTrade(sc,trades); //delete a trade from the in-memory list
break;
case 3:
totalsByInstrument(trades); //output the total quantity that has been traded for each instrument
break;
case 4:
totalsByUser(trades); //output the total consideration (quantity x by price) for each user
break;
case 5:
printTradesInCSV(sc, trades); //output the trades in CSV format
break;
default:
System.out.println("Error: Please enter a valid option");
break;
}
}
}
//print the trades in CSV format
//input: scanner to read input from command line, list of trades
private static void printTradesInCSV(Scanner sc, ArrayList<Trade> trades){
if(trades.size() !=0) {
System.out.println("Please choose sorting order:" +
"\n0-default\n1-order by trade id\n2-order by user id" +
"\n3-order by quantity\n4-order by price");
sc.nextLine();
int sortingType = -1;
try {
sortingType = sc.nextInt();
switch (sortingType) {
case 0: //default order
System.out.println("Trades in CSV format\nDefault order");
for (Trade tr : trades) {
System.out.println(tr);
}
break;
case 1: //order by trade id
System.out.println("Trades in CSV format\nOrdered by trade id");
Collections.sort(trades, Trade.tradeIdComparator);
for (Trade tr : trades) {
System.out.println(tr);
}
break;
case 2:
System.out.println("Trades in CSV format\nOrdered by user id");
Collections.sort(trades, Trade.userIdComparator);
for (Trade tr : trades) {
System.out.println(tr);
}
break;
case 3:
System.out.println("Trades in CSV format\nOrdered by quantity");
Collections.sort(trades, Trade.qtyComparator);
for (Trade tr : trades) {
System.out.println(tr);
}
break;
case 4:
System.out.println("Trades in CSV format\nOrdered by price");
Collections.sort(trades, Trade.priceComparator);
for (Trade tr : trades) {
System.out.println(tr);
}
break;
default:
System.out.println("Error: Please enter a valid option for sorting");
break;
}
} catch (InputMismatchException ex) {
System.out.println("Error: Sorting option can only be numeric");
sc.nextLine();
}
}else{
System.out.println("Error: Nothing to print. Memory contains to trades");
}
}
//output the total quantity that has been traded for each instrument
//input: list of trades
private static void totalsByInstrument(ArrayList<Trade> trades){
if(trades.size() !=0) {
Map<String, Integer> ttlByInst = new HashMap<>();
for (Trade tr : trades) {
if (ttlByInst.containsKey(tr.getInstrument())) {
Integer qty = ttlByInst.get(tr.getInstrument());
qty += tr.getQuantity();
ttlByInst.put(tr.getInstrument(), qty);
} else {
Integer qty = tr.getQuantity();
ttlByInst.put(tr.getInstrument(), qty);
}
}
//output
System.out.println("Totals by instrument");
for (Map.Entry<String, Integer> entry : ttlByInst.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}else{
System.out.println("Error: Memory contains no trades");
}
}
//output the total consideration (quantity x by price) for each user
//input: list of trades
private static void totalsByUser(ArrayList<Trade> trades){
DecimalFormat df = new DecimalFormat(".##");
if(trades.size() != 0) {
Map<Integer, Double> ttlByUser = new HashMap<>();
for (Trade tr : trades) {
if (ttlByUser.containsKey(tr.getUserID())) {
Double ttlConsideration = ttlByUser.get(tr.getUserID());
ttlConsideration += tr.getQuantity() * tr.getPrice();
ttlByUser.put(tr.getUserID(), ttlConsideration);
} else {
Double ttlConsideration = tr.getQuantity() * tr.getPrice();
ttlByUser.put(tr.getUserID(), ttlConsideration);
}
}
//output
System.out.println("Totals by user");
for (Map.Entry<Integer, Double> entry : ttlByUser.entrySet()) {
System.out.println(entry.getKey() + " " + df.format(entry.getValue()));
}
}else{
System.out.println("Error: Memory contains no trades");
}
}
//delete a trade from the in-memory list
//input: scanner to read input from command line, list of trades
private static void deleteTrade(Scanner sc, ArrayList<Trade> trades){
if(trades.size()!=0) {
System.out.println("Please enter the trade id to delete:");
sc.nextLine();
int tradeId = 0;
try {
tradeId = sc.nextInt();
Trade trade = null;
//iterate through trades
for (Trade tr : trades) {
if (tr.getTradeID() == tradeId) {
trade = tr;
break;
}
}
if (trade != null) {
trades.remove(trade);
System.out.println("Trade with id " + tradeId + " was removed from memory");
} else {
System.out.println("Error: Trade with id " + tradeId + " cannot be found in memory");
}
} catch (InputMismatchException ex) {
System.out.println("Error: Trade id can only be numeric");
sc.nextLine();
}
}else{
System.out.println("Error: Nothing to delete. Memory contains no trades");
}
}
//specify a file and load the trades into memory
//input: scanner to read input from command line
//output: list of trades loaded into memory
private static ArrayList<Trade> addTrade(Scanner sc){
System.out.println("Please enter full path to CSV file.");
sc.nextLine();
String csvFileName=sc.nextLine();
File csvFile=null;
BufferedReader br=null;
String line=null;
String fileType = "undetermined";
ArrayList<Trade> trades=new ArrayList<Trade>();
try{
while(br==null){
try{
csvFile=new File(csvFileName);
fileType = Files.probeContentType(csvFile.toPath());
//check if file is CSV files
if(fileType !=null &&
!fileType.equals("text/csv") &&
!fileType.equals("application/vnd.ms-excel") &&
!fileType.equals("text/comma-separated-values") &&
!fileType.equals("application/csv") &&
!fileType.equals("application/excel") &&
!fileType.equals("application/vnd.msexcel") &&
!fileType.equals("text/anytext")){
System.out.println("Error: Not a CSV file. Please enter full path to CSV file");
csvFileName=sc.nextLine();
continue;
}
br=new BufferedReader(new FileReader(csvFile));
}catch (FileNotFoundException ex){
System.out.println("Error: File is not found. Please enter full path to CSV file");
csvFileName=sc.nextLine();
continue;
}catch (IOException ioException){
System.out.println("Error: Unable to determine file type");
csvFileName=sc.nextLine();
continue;
}
}
while ((line = br.readLine()) != null) {
// use comma as separator
String[] rawTrades = line.split(",");
Trade trade=new Trade(
Integer.parseInt(rawTrades[0]),
Integer.parseInt(rawTrades[1]),
rawTrades[2],
Integer.parseInt(rawTrades[3]),
rawTrades[4],
Double.parseDouble(rawTrades[5]));
trades.add(trade);
}
}catch (IOException e){
e.printStackTrace();
}
return trades;
}
}