public void TransferInfo(ITradeRecordSource source, ITradeRecordDestination destination, ITradeRecordValidator validator, IRecordFactory recordFactory) { var records = source.ReadRecords(validator, recordFactory); destination.WriteRecords(records); }
public IEnumerable <TradeRecord> ReadRecords(ITradeRecordValidator validator, IRecordFactory recordFactory) { if (validator == null) { throw new ArgumentNullException($"{nameof(validator)} can't be null"); } if (recordFactory == null) { throw new ArgumentNullException($"{nameof(recordFactory)} can't be null"); } var lineNumber = 0; using (var sr = new StreamReader(_sourcePath)) { while (true) { var recordLine = sr.ReadLine(); if (recordLine != null) { TradeRecord record = null; try { record = CreateValidRecord(recordLine.Split(",".ToCharArray()), recordFactory, validator); } catch (ArgumentException e) { var logMessage = $"{e.Message}. Invalid record. Line:{lineNumber}"; LoggerService.Warning(logMessage); } if (record != null) { yield return(record); } lineNumber++; } else { yield break; } } } }
private TradeRecord CreateValidRecord(string[] fields, IRecordFactory recordFactory, ITradeRecordValidator validator) { if (fields.Length != StandartFieldLength) { throw new ArgumentException("Not valid amount of fields"); } if (fields[0].Length != StandartCurrencyLength) { throw new ArgumentException("Not valid currency codes"); } var source = fields[0].Substring(0, 3); var destination = fields[0].Substring(3, 3); return(recordFactory.CreateNewRecord(destination, source, fields[2], fields[1], validator)); }
public TradeRecord CreateNewRecord(string destinationCurrency, string sourceCurrency, string price, string lots, ITradeRecordValidator validator) { validator.CheckInfo(destinationCurrency, sourceCurrency, price, lots); return(new TradeRecord( destinationCurrency, sourceCurrency, float.Parse(lots, NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, CultureInfo.InvariantCulture), decimal.Parse(price, NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, CultureInfo.InvariantCulture) )); }