/// <summary> /// Tries to parse transaction amount from string /// </summary> /// <param name="transactionAmountString">String with amount data</param> /// <param name="amount">Output amount</param> /// <returns>True if extracting of amount succeeded</returns> protected virtual bool TryParseTransactionAmount(string transactionAmountString, out long amount) { amount = 0; try { amount = long.Parse(transactionAmountString); } catch (ArgumentNullException e) { _logger.WriteLine("TransactionAmount is null, exception: " + e.ToString()); return(false); } catch (FormatException e) { _logger.WriteLine("TransactionAmount " + transactionAmountString + " is not in correct format, exception: " + e.ToString()); return(false); } catch (OverflowException e) { _logger.WriteLine("TransactionAmount " + transactionAmountString + " is too big, exception: " + e.ToString()); return(false); } return(true); }
/// <summary> /// Opens source for reading transactions, processes these one by one and writes the result into output source /// </summary> /// <returns>true if transactions source and destination opening succeeded and could start to read transactions</returns> public bool ProcessAllTransactions() { //Transaction reader had error during construction if (_transactionsReader.HasError()) { return(false); } if (!TryOpenTransactionReader()) { return(false); } if (!TryOpenTransactionWriter()) { return(false); } int errorCount = 0; while (_transactionsReader.HasMoreTransactions()) { if (!ProcessNextTransaction() && TransactionErrorCountExceeded(ref errorCount)) { _logger.WriteLine("Abort going through transactions"); break; } } _transactionsReader.Close(); _transactionsWriter.Close(); return(true); }
/// <summary> /// Reads next payment transaction object from text line if there are possibly more transactions left /// </summary> /// <returns></returns> public virtual TransactionObject ReadNextTransaction() { if (HasMoreTransactions()) { try { return(GetTransactionFromLine(GetTransactionFileReader().ReadLine())); } catch (Exception e) { FileOperationsHaveErrors = true; _logger.WriteLine(e.ToString()); } } return(null); }
static bool ParseArguments(string[] args) { if (args == null || args.Length < 1) { _logger.WriteLine("No path provided where to read transactions from, use current path"); _filePath = Path.Combine(Directory.GetCurrentDirectory(), TransactionsFileName); return(true); } string fileDirectory = args[0]; if (!Directory.Exists(fileDirectory)) { _logger.WriteLine("Directory does not exist - " + fileDirectory); return(false); } _filePath = Path.Combine(fileDirectory, TransactionsFileName); return(true); }
/// <summary> /// Class constructor. Sets FileOperationsHaveErrors to true if payment transactions file name is empty or does not exist /// </summary> /// <param name="logger">Logger object</param> /// <param name="transactionStringReader">Payment transactions reader from specific format</param> /// <param name="transactionFileName">Payment transactions file name</param> public TransactionsTextFileReader(ICalcFeesLogger logger, ITransactionStringReader transactionStringReader, string transactionFileName) { _logger = logger ?? throw new ArgumentNullException("logger"); _transactionStringReader = transactionStringReader ?? throw new ArgumentNullException("transactionStringReader"); _transactionFileName = transactionFileName; FileOperationsHaveErrors = false; /* * Sets FileOperationsHaveErrors to true for empty name or file not found already here */ if (string.IsNullOrWhiteSpace(transactionFileName)) { _logger.WriteLine("Cannot read from file if file name is empty"); FileOperationsHaveErrors = true; } if (!File.Exists(transactionFileName)) { _logger.WriteLine("File does not exist - " + transactionFileName); FileOperationsHaveErrors = true; } }
static void Main(string[] args) { _logger = new CalcFeesConsoleLogger(); if (!ParseArguments(args)) { return; } var fileReader = new TransactionsTextFileReader(_logger, new TransactionString3ColumnReader(_logger), _filePath); if (fileReader.HasError()) { _logger.WriteLine("Could not initialise transactions file for reading - " + _filePath); return; } var processor = new TransactionsProcessor(_logger, fileReader, new TransactionsConsoleWriter(_logger, new TransactionString3ColumnWriter(_logger))); if (!processor.ProcessAllTransactions()) { _logger.WriteLine("Could not process all transactions - " + _filePath); } }
/// <summary> /// Method to actually write payment transaction data into string /// </summary> /// <param name="transactionObject">Payment transaction object</param> /// <returns>String with transaction date, merchant name and percentage fee</returns> public string WriteTransactionData(TransactionObject transactionObject) { if (transactionObject == null) { return(""); } try { return(transactionObject.TransactionDate.ToString(OutgoingDateFormat) + Separator + transactionObject.MerchantName + Separator + transactionObject.TransactionPercentageFee.ToString("0.00")); } catch (Exception e) { _logger.WriteLine("Could not read next string line from transaction object, exception: " + e.ToString()); return(""); } }
/// <summary> /// Transforms payment transaction object into string representation using predefined transactionStringWriter /// </summary> /// <param name="transactionObject"></param> /// <returns>true if writing to the console succeeded</returns> public bool WriteNextTransaction(TransactionObject transactionObject) { string line = _transactionStringWriter.WriteTransactionData(transactionObject); try { Console.WriteLine(line); } catch (Exception e) { _logger.WriteLine("Writing to console encountered exception: " + e.ToString()); WriteOperationsHaveErrors = true; return(false); } return(true); }