Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.IO;
  5.  
  6. namespace SRP.Processor
  7. {
  8. public class TradeProcessor
  9. {
  10. private static float LotSize = 100000f;
  11.  
  12. private List<TradeRecord> ValidateTrades(List<string> lines)
  13. {
  14. var trades = new List<TradeRecord>();
  15.  
  16. var lineCount = 1;
  17. foreach (var line in lines)
  18. {
  19. var fields = line.Split(new char[] { ';' });
  20.  
  21. if (fields.Length != 3)
  22. {
  23. Console.WriteLine($"WARN: Line {lineCount} malformed. Only {fields.Length} field(s) found.");
  24. continue;
  25. }
  26.  
  27. if (fields[0].Length != 6)
  28. {
  29. Console.WriteLine($"WARN: Trade currencies on line {lineCount} malformed: '{fields[0]}'");
  30. continue;
  31. }
  32.  
  33. int tradeAmount;
  34. if (!int.TryParse(fields[1], out tradeAmount))
  35. {
  36. Console.WriteLine($"WARN: Trade amount on line {lineCount} not a valid integer: '{fields[1]}'");
  37. }
  38.  
  39. decimal tradePrice;
  40. if (!decimal.TryParse(fields[2], out tradePrice))
  41. {
  42. Console.WriteLine($"WARN: Trade price on line {lineCount} not a valid decimal: '{fields[2]}'");
  43. }
  44.  
  45. trades.Add(CreateTrade(fields[0], tradeAmount, tradePrice));
  46.  
  47. lineCount++;
  48. }
  49.  
  50. return trades;
  51. }
  52.  
  53. private TradeRecord CreateTrade(string name, int tradeAmount, decimal tradePrice)
  54. {
  55. var sourceCurrencyCode = name.Substring(0, 3);
  56. var destinationCurrencyCode = name.Substring(3, 3);
  57.  
  58. // calculate values
  59. var trade = new TradeRecord
  60. {
  61. SourceCurrency = sourceCurrencyCode,
  62. DestinationCurrency = destinationCurrencyCode,
  63. Lots = tradeAmount / LotSize,
  64. Price = tradePrice
  65. };
  66.  
  67. return trade;
  68. }
  69.  
  70. private List<string> ReadTrades(Stream stream)
  71. {
  72. // read rows
  73. var lines = new List<string>();
  74. using (var reader = new StreamReader(stream))
  75. {
  76. string line;
  77. while ((line = reader.ReadLine()) != null)
  78. {
  79. lines.Add(line);
  80. }
  81. }
  82.  
  83. return lines;
  84. }
  85.  
  86. private void WriteTradesToDatabase(List<TradeRecord> trades)
  87. {
  88. using (var connection = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=TradeProcessor;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False"))
  89. {
  90. connection.Open();
  91. using (var transaction = connection.BeginTransaction())
  92. {
  93. foreach (var trade in trades)
  94. {
  95. var command = connection.CreateCommand();
  96. command.Transaction = transaction;
  97. command.CommandType = System.Data.CommandType.StoredProcedure;
  98. command.CommandText = "dbo.insert_trade";
  99. command.Parameters.AddWithValue("@sourceCurrency", trade.SourceCurrency);
  100. command.Parameters.AddWithValue("@destinationCurrency", trade.DestinationCurrency);
  101. command.Parameters.AddWithValue("@lots", trade.Lots);
  102. command.Parameters.AddWithValue("@price", trade.Price);
  103.  
  104. command.ExecuteNonQuery();
  105. }
  106.  
  107. transaction.Commit();
  108. }
  109. connection.Close();
  110. }
  111. }
  112.  
  113. public void ProcessTrades(Stream stream)
  114. {
  115. var lines = ReadTrades(stream);
  116.  
  117. var trades = ValidateTrades(lines);
  118.  
  119. WriteTradesToDatabase(trades);
  120.  
  121. Console.WriteLine($"INFO: {trades.Count} trades processed");
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement