public override void TakeUserInputs() { var isRunning = true; while (isRunning) { Console.Write("\nOptions: \n: List All \n: List [Account] \n: Import File [Filepath] \n: Quit\n> "); var userInput = Console.ReadLine(); if (userInput == "" || userInput.ToLower() == "quit" || userInput.ToLower() == "q") { isRunning = false; } else if (userInput.ToLower() == "list all") { Display.DisplayAllInformation(); } else if (userInput.ToLower().StartsWith("list ")) { try { string inputtedName = userInput.Remove(0, 5); Display.DisplaySpecificPersonInformation(inputtedName); } catch (ArgumentException e) { Console.WriteLine(e.Message); } } else if (userInput.ToLower().StartsWith("import file ")) { try { string inputtedFilepath = userInput.Remove(0, 12); FileReader fileReader = FileReader.GetFileReaderForInput(inputtedFilepath, Display); List <Payment> successfulFilePayments = fileReader.GetPayments(); BankSystem.UpdateAccountPayments(successfulFilePayments); Display.DisplaySuccessfulImport(inputtedFilepath); } catch (FileNotFoundException e) { Display.DisplayMessage(e.Message); } catch (FormatException e) { Display.DisplayMessage(e.Message); } } } }
public override List <Payment> GetPayments() { logger.Debug("Reading in JSON payments."); var JSONLines = (System.IO.File.ReadAllText(Filepath)); List <Payment> correctFormatPayments = new List <Payment>(); var allNewPayments = JsonConvert.DeserializeObject <List <Payment> >(JSONLines); var lineCounter = 0; foreach (var payment in allNewPayments) { lineCounter++; if (payment.FromAccount == null || payment.ToAccount == null || payment.Narrative == null || payment.Date == null) { Display.DisplayMessage($"Warning: There was an error importing data from object {lineCounter} of this JSON file: this didn't have the correct format.\nAs a result, this specific transaction has not been read in."); } else { correctFormatPayments.Add(payment); } } logger.Debug("Finished reading in JSON payments."); return(correctFormatPayments); }
public override List <Payment> GetPayments() { logger.Debug("Reading in CSV payments."); IEnumerable <string> transactionLines = System.IO.File.ReadAllLines(Filepath).Skip(1); List <Payment> newPayments = new List <Payment>(); var lineCounter = 0; foreach (string line in transactionLines) { lineCounter++; var values = line.Split(','); if (values.Length != 5) { logger.Error($"Error in adding new payment, triggered on CSV line {lineCounter}. Not the correct number of values."); logger.Debug($"Failed payment had {values.Length} values, but needs exactly 5 values."); Display.DisplayMessage($"Warning: There was an error importing data on line {lineCounter} of this CSV file: there should be exactly 5 entries per line; this line had {values.Length}.\nAs a result, this specific transaction has not been read in."); } else { try { newPayments.Add(new Payment(DateTime.Parse(values[0]), values[1], values[2], values[3], decimal.Parse(values[4]))); } catch (Exception e) { logger.Error($"Error in adding new payment, triggered on CSV line {lineCounter}. Error message: {e.Message}"); logger.Debug($"Failed payment had values: DATE {values[0]} FROM {values[1]} TO {values[2]} NARRATIVE {values[3]} AMOUNT {values[4]} "); Display.DisplayMessage($"Warning: There was an error importing data on line {lineCounter} of this CSV file: this didn't have the correct format.\nAs a result, this specific transaction has not been read in."); } } } logger.Debug("Finished reading in CSV payments."); return(newPayments); }
public override List <Payment> GetPayments() { logger.Debug("Reading in XML payments."); XmlDocument doc = new XmlDocument(); doc.Load(Filepath); List <Payment> allNewPayments = new List <Payment>(); XmlNode node = doc.DocumentElement.SelectSingleNode("/TransactionList"); if (node == null) { logger.Error("Error: The XML file did not have a <TransactionList> tag present."); throw new FormatException("No enclosing 'TransactionList' tag - the file cannot be opened."); } var lineCounter = 0; foreach (XmlNode childNode in node.ChildNodes) { lineCounter++; try { Payment newPayment = new Payment( DateTime.FromOADate(double.Parse(childNode.Attributes["Date"]?.InnerText)), childNode["Parties"]["From"].InnerText, childNode["Parties"]["To"].InnerText, childNode["Description"].InnerText, decimal.Parse(childNode["Value"].InnerText)); allNewPayments.Add(newPayment); } catch (Exception e) { Display.DisplayMessage($"Warning: There was an error importing data from transaction {lineCounter} of this XML file: this didn't have the correct format.\nAs a result, this specific transaction has not been read in."); logger.Error($"Error in adding new payment, triggered on transaction number {lineCounter} in XML file. Error message: {e.Message}"); } } logger.Debug("Finished reading in XML payments."); return(allNewPayments); }