Advertisement
Guest User

Untitled

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