Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.64 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.stream.*;
  4. import static java.util.stream.Collectors.joining;
  5. import static java.util.stream.Collectors.toList;
  6.  
  7.  
  8. class ETF {
  9.  
  10. private List<EtfComponent> components;
  11. private Map<String, Float> componentWeight;
  12. private String name;
  13. private double bestBid;
  14. private double bestAsk;
  15. private double ourAsk;
  16. private double ourBid;
  17. private String currency;
  18.  
  19. public String getName() {
  20. return name;
  21. }
  22.  
  23. public String getCurrency() {
  24. return currency;
  25. }
  26.  
  27. public Map<String, Float> getComponentWeight() {
  28. return componentWeight;
  29. }
  30.  
  31. public void setComponentWeight(Map<String, Float> componentWeight) {
  32. this.componentWeight = componentWeight;
  33. }
  34.  
  35. public void setBestBid(double bestBid) {
  36. this.bestBid = bestBid;
  37. }
  38.  
  39. public void setBestAsk(double bestAsk) {
  40. this.bestAsk = bestAsk;
  41. }
  42.  
  43. public void setOurAsk(double ourAsk) {
  44. this.ourAsk = ourAsk;
  45. }
  46.  
  47. public void setOurBid(double ourBid) {
  48. this.ourBid = ourBid;
  49. }
  50.  
  51. public double getOurAsk() {
  52. return ourAsk;
  53. }
  54.  
  55. public double getOurBid() {
  56. return ourBid;
  57. }
  58.  
  59.  
  60. public double getBestBid() {
  61. return bestBid;
  62. }
  63.  
  64. public double getBestAsk() {
  65. return bestAsk;
  66. }
  67.  
  68. public List<EtfComponent> getComponents() {
  69. return components;
  70. }
  71.  
  72. public ETF(List<EtfComponent> components, String name, String currency){
  73. this.components = components;
  74. this.name = name;
  75. this.currency = currency;
  76. }
  77.  
  78. }
  79.  
  80. class EtfComponent {
  81.  
  82. double bid;
  83. double ask;
  84. String name;
  85. String currency;
  86.  
  87.  
  88. public String getName() {
  89. return name;
  90. }
  91.  
  92. public double getBid() {
  93. return bid;
  94. }
  95.  
  96. public double getAsk() {
  97. return ask;
  98. }
  99.  
  100. public String getCurrency() {
  101. return currency;
  102. }
  103.  
  104. public void setBid(double bid) {
  105. this.bid = bid;
  106. }
  107.  
  108. public void setAsk(double ask) {
  109. this.ask = ask;
  110. }
  111.  
  112.  
  113. public EtfComponent(String name, String currency) {
  114. this.name = name;
  115. this.currency = currency;
  116. }
  117.  
  118. }
  119.  
  120. class Market{
  121.  
  122. private static Market market_instance = null;
  123. private List<ETF> etfs;
  124. private List<EtfComponent> components;
  125. private Map<String, Double> exchangeRatesCurrencies;
  126. private Map<String, ETF> nameToETF;
  127. private Map<String, EtfComponent> nameToEtfComponent;
  128. private Map<String, Double> marketETFAsks;
  129. private Map<String, Double> marketETFBids;
  130. private Map<String, Double> ETFsAO;
  131. private Map<String, Double> ETFsBO;
  132.  
  133. private Market(){
  134. etfs = new ArrayList<>();
  135. components = new ArrayList<>();
  136. exchangeRatesCurrencies = new HashMap<>();
  137. nameToETF = new HashMap<>();
  138. nameToEtfComponent = new HashMap<>();
  139. marketETFAsks = new HashMap<>();
  140. marketETFBids = new HashMap<>();
  141. ETFsAO = new HashMap<>();
  142. ETFsBO = new HashMap<>();
  143.  
  144. }
  145.  
  146. public Map<String, EtfComponent> getNameToEtfComponent() {
  147. return nameToEtfComponent;
  148. }
  149.  
  150. public static Market getMarket(){
  151. if (market_instance == null){
  152. market_instance = new Market();
  153. }
  154. market_instance.exchangeRatesCurrencies.put("EUREUR", 1.0);
  155. return market_instance;
  156. }
  157.  
  158. public List<ETF> getEtfs() {
  159. return etfs;
  160. }
  161.  
  162. public static void addEtf(ETF etf){
  163. market_instance.nameToETF.put(etf.getName(), etf);
  164. market_instance.etfs.add(etf);
  165. }
  166.  
  167. public static void addEtfComponent(EtfComponent component){
  168. market_instance.nameToEtfComponent.put(component.getName(), component);
  169. market_instance.components.add(component);
  170. }
  171.  
  172. public void updateComponent(String name, double price, String side){
  173.  
  174. if (side.equals("A")){
  175. market_instance.nameToEtfComponent.get(name).setAsk(price);
  176. }else if(side.equals("B")){
  177. market_instance.nameToEtfComponent.get(name).setBid(price);
  178. }
  179. // TODO: else throw an exception
  180.  
  181. }
  182.  
  183. public double calculateTheo(ETF etf, String side){
  184.  
  185. double theo = 0;
  186. try{
  187. for (EtfComponent component: etf.getComponents()
  188. ) {
  189. double bidAskprice = (side.equals("A")) ? component.getAsk() : component.getBid();
  190. System.out.println(component.getCurrency() + etf.getCurrency());
  191. theo += bidAskprice *
  192. etf.getComponentWeight().get(component.getName()) *
  193. exchangeRatesCurrencies.get(component.getCurrency() + etf.getCurrency());
  194. }
  195. theo /= etf.getComponents().size();
  196.  
  197. theo = (double) Math.round(theo*100) / 100; //round to 2 decimals
  198.  
  199. if (side.equals("A"))
  200. etf.setOurAsk(theo);
  201. else if (side.equals("B"))
  202. etf.setOurBid(theo);
  203.  
  204. return theo;
  205. } catch (Exception e){
  206. System.out.println("Exception when calculating theo for: " + etf.getName() + " on side " + side);
  207. etf.setOurBid(-1);
  208. etf.setOurAsk(-1);
  209. return -1;
  210. }
  211.  
  212. }
  213.  
  214. public void updateCurrencyExchange(String name, double price){
  215.  
  216. exchangeRatesCurrencies.put(name.substring(0,3)+name.substring(0,3),1.0); // optimize
  217. exchangeRatesCurrencies.put(name.substring(3,6)+name.substring(6,6),1.0); // optimize
  218. exchangeRatesCurrencies.put(name, price);
  219. exchangeRatesCurrencies.put(flipCurrency(name), 1/price);
  220.  
  221. }
  222.  
  223. public void updateEtf(String name, double price, String side){
  224.  
  225. if (side.equals("A"))
  226. market_instance.nameToETF.get(name).setBestAsk(price);
  227. else if (side.equals("B"))
  228. market_instance.nameToETF.get(name).setBestBid(price);
  229. // TODO: else throw an exception
  230. }
  231.  
  232. public List<List<String>> updateOffers() {
  233. List<List<String>> output = new ArrayList<>();
  234. for(ETF etf: etfs) {
  235. String currentETF = etf.getName();
  236. double currentAO = etf.getOurAsk();
  237. double currentBO = etf.getOurBid();
  238. double marketAsk = etf.getBestAsk();
  239. double marketBid = etf.getBestBid();
  240.  
  241. System.out.println("ETF: " + currentETF);
  242. System.out.println("Our Ask: " + currentAO);
  243. System.out.println("Our Bid: " + currentBO);
  244. System.out.println("Market Ask: " + marketAsk);
  245. System.out.println("Market Bid: " + marketBid);
  246.  
  247.  
  248. if (marketAsk != 0 || marketBid != 0) {
  249. if (currentAO > marketAsk) {
  250. currentAO = -1;
  251. } else {
  252. currentAO = marketAsk - 0.01;
  253. }
  254.  
  255. if (currentBO < marketBid) {
  256. currentBO = -1;
  257. } else {
  258. currentBO = marketBid + 0.01;
  259. }
  260. }
  261.  
  262. if (ETFsAO.containsKey(currentETF) && ETFsBO.containsKey(currentETF)) {
  263. if (currentAO != -1 || currentBO != -1) {
  264. // add to print list quantity 0
  265. ETFsAO.put(currentETF, -1.0);
  266. ETFsBO.put(currentETF, -1.0);
  267. output.add(getUpdateOutput("AO", currentETF, ETFsAO.get(currentETF), "0"));
  268. output.add(getUpdateOutput("BO", currentETF, ETFsBO.get(currentETF), "0"));
  269. } else if (currentAO != ETFsAO.get(currentETF) || currentBO != ETFsBO.get(currentETF)) {
  270. // print new price
  271. ETFsAO.put(currentETF, currentAO);
  272. ETFsBO.put(currentETF, currentBO);
  273. output.add(getUpdateOutput("AO", currentETF, currentAO, "10"));
  274. output.add(getUpdateOutput("BO", currentETF, currentBO, "10"));
  275.  
  276. }
  277. // first time we set the price
  278. } else if (currentAO != -1 && currentBO != -1) {
  279. System.out.println("update output");
  280. ETFsAO.put(currentETF, currentAO);
  281. ETFsBO.put(currentETF, currentBO);
  282. output.add(getUpdateOutput("AO", currentETF, currentAO, "10"));
  283. output.add(getUpdateOutput("BO", currentETF, currentBO, "10"));
  284. }
  285. }
  286.  
  287. return output;
  288. }
  289.  
  290. private List<String> getUpdateOutput(String Operation, String currentETF, Double currentAO, String Quantity) {
  291. ArrayList<String> ETFUpdateOutput = new ArrayList<>();
  292. ETFUpdateOutput.add(Operation);
  293. ETFUpdateOutput.add(currentETF);
  294. ETFUpdateOutput.add(Double.toString(currentAO));
  295. ETFUpdateOutput.add(Quantity);
  296. return ETFUpdateOutput;
  297. }
  298.  
  299. private String flipCurrency(String name){
  300. return name.substring(name.length()/2).concat(name.substring(0, name.length()/2));
  301. }
  302.  
  303. }
  304.  
  305.  
  306. class Result {
  307.  
  308.  
  309. /*
  310. * Complete the 'addEtf' function below.
  311. *
  312. * The function accepts following parameters:
  313. * 1. STRING name
  314. * 2. STRING currency
  315. * 3. 2D_STRING_ARRAY components
  316. */
  317.  
  318. public static Market market = Market.getMarket();
  319.  
  320.  
  321. public static void addEtf(String name, String currency, List<List<String>> components) {
  322. List<EtfComponent> etfComponents = new ArrayList<>();
  323. Map<String, Float> weights = new HashMap<>();
  324. for (List<String> component: components) {
  325.  
  326. String componentName = component.get(0);
  327. float componentWeight = Float.parseFloat(component.get(1));
  328. String componentCurrency = component.get(2);
  329. EtfComponent etfComponent = new EtfComponent(componentName, componentCurrency);
  330. etfComponents.add(etfComponent);
  331. weights.put(componentName, componentWeight);
  332. market.getNameToEtfComponent().put(componentName,etfComponent);
  333.  
  334. }
  335.  
  336. ETF etf = new ETF(etfComponents, name, currency);
  337. etf.setComponentWeight(weights);
  338. market.addEtf(etf);
  339.  
  340. }
  341.  
  342. /*
  343. * Complete the 'playback' function below.
  344. *
  345. * The function accepts 2D_STRING_ARRAY messages as parameter.
  346. */
  347.  
  348. public static void playback(List<List<String>> messages) {
  349. List<List<String>> offers = null;
  350. for (List<String> message:messages
  351. ) {
  352.  
  353. String type = message.get(0);
  354. String symbol = message.get(1);
  355. String exchange_name = message.get(2);
  356. double price = Double.parseDouble(message.get(3));
  357. int quantity = Integer.parseInt(message.get(4));
  358.  
  359. // TODO: Add enums or somthing
  360. if (exchange_name.equals("Stock")){
  361. market.updateComponent(symbol, price, type);
  362. } else if (exchange_name.equals("Currency")){
  363. market.updateCurrencyExchange(symbol, price);
  364. }else{
  365. market.updateEtf(symbol, price, type);
  366. }
  367.  
  368. for (ETF etf: market.getEtfs()){
  369. market.calculateTheo(etf, "A"); // TODO: Optimize to only calculate when there is a change
  370. market.calculateTheo(etf, "B"); // TODO: Optimize to only calculate when there is a change
  371. }
  372.  
  373. offers = market.updateOffers();
  374. makeOffers(offers);
  375.  
  376. }
  377.  
  378. }
  379.  
  380. private static void makeOffers(List<List<String>> offers){
  381. for (List<String> offer: offers
  382. ) {
  383. System.out.println(String.join(" ", offer));
  384. }
  385. }
  386.  
  387. }
  388.  
  389. public class test2 {
  390. public static void main(String[] args) throws IOException {
  391. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  392.  
  393. int NumberOfETF = Integer.parseInt(bufferedReader.readLine().trim());
  394.  
  395. IntStream.range(0, NumberOfETF).forEach(NumberOfETFItr -> {
  396. try {
  397. String ETFSymbol = bufferedReader.readLine();
  398.  
  399. String ETFCurrency = bufferedReader.readLine();
  400.  
  401. int NumberOfListingInETF = Integer.parseInt(bufferedReader.readLine().trim());
  402.  
  403. List<List<String>> listings = new ArrayList<>();
  404.  
  405. IntStream.range(0, NumberOfListingInETF).forEach(i -> {
  406. try {
  407. listings.add(
  408. Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  409. .collect(toList())
  410. );
  411. } catch (IOException ex) {
  412. throw new RuntimeException(ex);
  413. }
  414. });
  415.  
  416. Result.addEtf(ETFSymbol, ETFCurrency, listings);
  417. } catch (IOException ex) {
  418. throw new RuntimeException(ex);
  419. }
  420. });
  421.  
  422. int NumberOfMessages = Integer.parseInt(bufferedReader.readLine().trim());
  423.  
  424. List<List<String>> messages = new ArrayList<>();
  425.  
  426. IntStream.range(0, NumberOfMessages).forEach(i -> {
  427. try {
  428. messages.add(
  429. Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  430. .collect(toList())
  431. );
  432. } catch (IOException ex) {
  433. throw new RuntimeException(ex);
  434. }
  435. });
  436.  
  437. Result.playback(messages);
  438.  
  439. bufferedReader.close();
  440. }
  441. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement