-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain1.java
More file actions
227 lines (190 loc) · 9.21 KB
/
Main1.java
File metadata and controls
227 lines (190 loc) · 9.21 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
package com.company;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.sun.xml.internal.ws.api.ha.HaInfo;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;
public class Main1 {
public static void main(String[] args) throws IOException, NullPointerException {
ArrayList<Trader> traders = new ArrayList<>();
ArrayList<Order> OrderBook = new ArrayList<>();
StockExchange stockExchange = new StockExchange();
// HashMap<UUID, Trader> trader1 = new HashMap<UUID, Trader>();
boolean order = false;
// for (Trader i : traders) {
// trader1.put(UUID.randomUUID(), i);
// }
// for (Trader i : traders) {
// System.out.println(i);
// }
File file = new File("Output.txt");
PrintWriter printWriter = new PrintWriter(file);
Scanner scanner = new Scanner(new File("C:\\Users\\Shraddha\\Desktop\\Sem 3\\CSD 203\\SampleInput.txt"));
while (scanner.hasNextLine()) {
String Line = scanner.nextLine();
String[] LineSplit = Line.split("[,:{} ]");
switch (LineSplit[0]) {
case "Add":
switch (LineSplit[1]) {
case "scrip":
StockExchange.addStocks(
LineSplit[3], LineSplit[7],
Double.parseDouble(LineSplit[10]), Double.parseDouble(LineSplit[13]),
Double.parseDouble(LineSplit[16]), Double.parseDouble(LineSplit[19])
);
printWriter.println("Added scrip: " + LineSplit[3] + " has been initiated with class Stock");
break;
case "user":
traders.add(new Trader(LineSplit[3], Double.parseDouble(LineSplit[6]), createHashMap(Line)));
printWriter.println("Added user: " + LineSplit[3] + " has been initiated with class Trader");
break;
}
break;
case "Place":
String name = LineSplit[5];
String type = LineSplit[9];
String scrip = LineSplit[13];
double qty = Double.parseDouble(LineSplit[16]);
double rate = Double.parseDouble(LineSplit[20]);
for (Trader i : traders) {
if (name.equals(i.name)) {
if (type.equals("sell")) {
if (stockExchange.checkTicker(scrip) && i.holdings.get(scrip) >= qty) {
OrderBook.add(new Order(name, type, scrip, qty, rate));
order = true;
}
// else
// System.out.println("Stock not found or Insufficient stocks.");
} else if (type.equals("buy")) {
if (i.funds > qty * rate) {
OrderBook.add(new Order(name, type, scrip, qty, rate));
order = true;
}
// else
// System.out.println("Funds not sufficient.");
}
}
// else
// System.out.println("User not found.");
}
printWriter.println("Order placed: " + Line);
break;
case "Show":
switch (LineSplit[1]) {
case "Orderbook":
printWriter.println(OrderBook.toString());
break;
case "sector":
String sector = LineSplit[3];
printWriter.println("Showing stocks by of " + sector + " sector");
for (int i = 0; i < stockExchange.NSE.size(); i++) {
if (stockExchange.NSE.get(i).type.equals(sector)) {
printWriter.println(stockExchange.NSE.get(i).toString());
}
}
break;
case "Scrips":
printWriter.println("Showing scrips: \n");
for (int i = 0; i < stockExchange.NSE.size(); i++)
printWriter.println(stockExchange.NSE.get(i).toString());
break;
case "Users":
printWriter.println("Showing Users: \n");
for (Trader i : traders) {
printWriter.println(i.toString());
}
}
case "Delete":
switch (LineSplit[1]) {
case "scrip":
printWriter.println("Scrip Deleted: " + LineSplit[3]);
stockExchange.NSE.remove(LineSplit[3]);
for (Trader i : traders)
i.holdings.remove(LineSplit[3]);
break;
case "User":
printWriter.println("User deleted: " + LineSplit[3]);
traders.remove(LineSplit[3]);
for (int i = 0; i < stockExchange.NSE.size(); i++) {
stockExchange.NSE.remove(LineSplit[3]);
}
}
case "Exit":
printWriter.println("Market Closed");
}
}
// System.out.println("Stock added." + StockExchange.NSE.toString() + "\n");
// System.out.println("User added." + traders.toString());
// System.out.println("Order placed.");
printWriter.close();
}
public void execute(ArrayList<Order> OrderBook,ArrayList<Trader> traders,StockExchange stockExchange) {
ArrayList<Order> BuyOrder = new ArrayList<>();
ArrayList<Order> SellOrder = new ArrayList<>();
double qtyNet = 0.0;
Stock stock;
for (Order i : OrderBook) {
if (i.type.equals("buy")) {
BuyOrder.add(i);
}
if (i.type.equals("sell")) {
SellOrder.add(i);
}
}
for (Order i : BuyOrder) {
for (Order j : SellOrder) {
if (i.scrip.equals(j.scrip)) {
String scrip = i.scrip;
double askPrice = j.getRate();
double bidPrice = i.getRate();
double qty1 = i.getQty(); //Quantity req by buyer
double qty2 = j.getQty(); //Quantity by seller
if (askPrice <= bidPrice) {
if (qty1 >= qty2) {
qtyNet = qty2;
} else if (qty1 < qty2) {
qtyNet = qty1;
}
Trader buyer;
Trader seller;
for( Trader k: traders){
if(i.name.equals(k.name))
buyer = k;
}
for( Trader k: traders){
if(j.name.equals(k.name))
seller = k;
}
for(int l=0;l<stockExchange.NSE.size();l++){
if(stockExchange.NSE.get(l).ticker.equals(scrip)){
stock = stockExchange.NSE.get(l);
}
}
// buyer.holdings.replace(scrip,buyer.holdings.get(scrip),buyer.holdings.get(scrip)+qtyNet);
// seller.holdings.replace(scrip,seller.holdings.get(scrip),seller.holdings.get(scrip)-qtyNet);
}
}
}
}
}
public static HashMap<String, Integer> createHashMap(String string) {
HashMap<String, Integer> hashMap = new HashMap<>();
if (string.endsWith("None")) {
} else {
String holdings = string.substring(string.indexOf("{") + 1, string.indexOf("}"));
String[] holdingsSplit = holdings.split("[,: ]");
for (int i = 0; i < holdingsSplit.length; i++) {
if (holdingsSplit[i].length() == 0)
continue;
else {
hashMap.put(holdingsSplit[i], Integer.parseInt(holdingsSplit[++i]));
}
}
}
return hashMap;
}
}