static void Main(string[] args) { logger.Info("==========================="); logger.Info("Start SupportBank session"); ConfigLogger(); InterfaceHandler.PrintWelcome(); // Parse CSV and generate transactions list and accounts list // TODO Move to new Importer class string defaultImport = "Transactions2012.xml"; transactionsHandler = new TransactionsHandler(); transactionsHandler.Parse(defaultImport); // Start program loop // TODO move to new CommandHandler class while (true) { InterfaceHandler.PrintAvailableCommands(transactionsHandler.GetStatus()); string command = Console.ReadLine(); logger.Info("User command: " + command); switch (command.ToLower()) { case "list transactions": InterfaceHandler.Print(transactionsHandler.GetTransactions()); break; case "list accounts": InterfaceHandler.Print(transactionsHandler.GetAccounts()); break; case "list all": InterfaceHandler.Print(transactionsHandler.GetTransactions(), transactionsHandler.GetAccounts()); break; default: if (command.ToLower().StartsWith("list ")) { string selectedAccount = command.Substring("list ".Length); InterfaceHandler.Print(selectedAccount, transactionsHandler.GetTransactions(), transactionsHandler.GetAccounts()); } else if (command.ToLower().StartsWith("import file ")) { string filename = command.Substring("import file ".Length); transactionsHandler.Parse(filename); } else { Console.WriteLine("Command not recognised. Please try again."); } break; } } }
private void Save(List <string> accounts, List <Transaction> transactions) { if (transactions != null && accounts != null) { if (transactions.Any()) { this.accounts = accounts; this.transactions = transactions; } else { InterfaceHandler.PrintError(InterfaceHandler.ERROR_EMPTY); } } }
private void ParseCSV(StreamReader streamReader) { logger.Info("Start parsing CSV"); List <string> accounts = new List <string>(); List <Transaction> transactions = new List <Transaction>(); // Read from CSV with filename logger.Info("Open CSV from filename"); string line; if ((line = streamReader.ReadLine()) != null) { // Get indices from header List <string> headers = line.ToLower().Split(',').ToList(); int dateIndex = headers.IndexOf(DATE); int senderNameIndex = headers.IndexOf(SENDER); int recipientNameIndex = headers.IndexOf(RECIPIENT); int narrativeIndex = headers.IndexOf(NARRATIVE); int amountIndex = headers.IndexOf(AMOUNT); // Create transcation and add to list // Also add new account names to list int lineCount = 2; List <KeyValuePair <int, int> > errorList = new List <KeyValuePair <int, int> >(); while ((line = streamReader.ReadLine()) != null) { string[] data = line.Split(','); string senderName = data[senderNameIndex]; string recipientName = data[recipientNameIndex]; // Load new account into accounts if (!accounts.Contains(senderName)) { accounts.Add(senderName); } if (!accounts.Contains(recipientName)) { accounts.Add(recipientName); } // Parse strings with Try/Catch DateTime dateTime = new DateTime(); float amount = 0; try { dateTime = Convert.ToDateTime(data[dateIndex]); } catch { logger.Warn("Failed to parse date on line " + lineCount); errorList.Add(new KeyValuePair <int, int>(lineCount, InterfaceHandler.ERROR_DATE)); continue; } try { amount = float.Parse(data[amountIndex]); } catch { logger.Warn("Failed to parse amount on line " + lineCount); errorList.Add(new KeyValuePair <int, int>(lineCount, InterfaceHandler.ERROR_AMOUNT)); continue; } // Add new transaction to transactions list transactions.Add(new Transaction( transactions.Count, dateTime, senderName, recipientName, data[narrativeIndex], amount )); lineCount++; } InterfaceHandler.PrintImportErrors(errorList); // Save on successful parsing Save(accounts, transactions); logger.Info("Finish parsing CSV"); } }