Exemplo n.º 1
0
        private TradeRecord MapTradeDataToTradeRecord(string[] fields)
        {
            var   sourceCurrencyCode      = fields[0].Substring(0, 3);
            var   destinationCurrencyCode = fields[0].Substring(3, 3);
            var   tradeAmount             = int.Parse(fields[1]);
            var   tradePrice  = decimal.Parse(fields[2]);
            float LotSize     = 100000f;
            var   tradeRecord = new TradeRecord
            {
                SourceCurrency      = sourceCurrencyCode,
                DestinationCurrency = destinationCurrencyCode,
                Lots  = tradeAmount / LotSize,
                Price = tradePrice
            };

            return(tradeRecord);
        }
Exemplo n.º 2
0
        public void ProcessTrades(System.IO.Stream stream)
        {
            // read rows
            var lines = new List <string>();

            using (var reader = new System.IO.StreamReader(stream))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    lines.Add(line);
                }
            }

            var trades = new List <TradeRecord>();

            var lineCount = 1;

            foreach (var line in lines)
            {
                var fields = line.Split(new char[] { ',' });

                if (fields.Length != 3)
                {
                    Console.WriteLine($"WARN: Line {lineCount} malformed. Only {fields.Length} field(s) found.");
                    continue;
                }

                if (fields[0].Length != 6)
                {
                    Console.WriteLine($"WARN: Trade currencies on line {lineCount} malformed: '{fields[0]}' ");
                    continue;
                }

                int tradeAmount;
                if (!int.TryParse(fields[1], out tradeAmount))
                {
                    Console.WriteLine($"WARN: Trade amount on line {lineCount} not a valid integer: '{fields[1]}'");
                }

                decimal tradePrice;
                if (!decimal.TryParse(fields[2], out tradePrice))
                {
                    Console.WriteLine("WARN: Trade price on line {0} not a valid decimal: '{1}'", lineCount, fields[2]);
                }

                var sourceCurrencyCode      = fields[0].Substring(0, 3);
                var destinationCurrencyCode = fields[0].Substring(3, 3);

                // Calculate values.
                var trade = new TradeRecord
                {
                    SourceCurrency          = sourceCurrencyCode,
                    destinationCurrencyCode = destinationCurrencyCode,
                    Lots  = tradeAmount / LotSize,
                    Price = tradePrice
                };

                trades.Add(trade);

                lineCount++;
            }

            using (var connection = new System.Data.SqlClient.SqlConnection("DataSource=(local);Initial Catalog = TradeDatabase; Integrated Security=True"))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    foreach (var trade in trades)
                    {
                        var command = connection.CreateCommand();
                        command.Transaction = transaction;
                        command.CommandType = System.Data.CommandType.StoredProcedure;
                        command.CommandText = "dbo.insert_trade";
                        command.Parameters.AddWithValue("@sourceCurrency", trade.SourceCurrency);
                        command.Parameters.AddWithValue("@destinationCurrency", trade.DestinationCurrency);
                        command.Parameters.AddWithValue("@lots", trade.Lots);
                        command.Parameters.AddWithValue("@price", trade.Price);
                        command.ExecuteNonQuery();
                    }

                    transaction.Commit();
                }
                connection.Close();
            }

            Console.WriteLine("INFO: {0} trades processed", trades.Count);
        }