public ITransaction CreateTransaction(string transaction, SupportedTransactionTypes transactionType) { if (string.IsNullOrWhiteSpace(transaction)) { throw new ArgumentNullException(nameof(transaction), $"!string.IsNullOrWhiteSpace({nameof(transaction)})"); } string[] sections = transaction.Split(','); switch (transactionType) { case SupportedTransactionTypes.TdBankAccount: return(CreateTdBankAccountTransaction(sections)); case SupportedTransactionTypes.TdCreditCard: return(CreateTdCreditCardTransaction(sections)); case SupportedTransactionTypes.MoneyVisualizer: return(CreateMoneyVisualizerTransaction(sections)); case SupportedTransactionTypes.CapitalOneCreditCard: return(CreateCaptialOneTransaction(sections)); default: return(NoneTransaction.Instance); } }
private void ReplaceTransactions(Func <ITransaction, bool> toReplaceFunc, SupportedTransactionTypes supportedTransactionType) { string transactionsFile = GetTransactionsFilePath(); _transactionsManager.ReplaceWithTransactionsFromFile( toReplaceFunc, transactionsFile, _transactions, supportedTransactionType); }
private void LoadTransactions(SupportedTransactionTypes transactionsType) { var transactionsFilePath = GetTransactionsFilePath(); if (string.IsNullOrWhiteSpace(transactionsFilePath)) { return; } var transactions = _transactionsManager.CreateTransactionsFromFile(transactionsFilePath, transactionsType); var observableTransactions = new ObservableCollection <ITransaction>(transactions); Transactions = observableTransactions; LineGraphViewModel = new LineGraphViewModel(Transactions); QuickInfoPageViewModel = new QuickInfoPageViewModel(Transactions); TransactionsListViewModel = new TransactionsListViewModel(Transactions); PieChartViewModel = new PieChartViewModel(Transactions); }
/// <inheritdoc /> public void ReplaceWithTransactionsFromFile( Func <ITransaction, bool> toReplaceFunc, string filePath, ObservableCollection <ITransaction> transactions, SupportedTransactionTypes transactionsType) { var transactionsToReplace = new List <ITransaction>(transactions.Where(x => toReplaceFunc(x))); if (!transactionsToReplace.Any(x => toReplaceFunc(x)) || string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath)) { // There are no transactions to replace, or the given file path is empty or doesn't exist. // Return the original collection. return; } foreach (var transactionToReplace in transactionsToReplace) { transactions.Remove(transactionToReplace); } var transactionsByDate = transactions.GroupBy(x => x.DateTime); var replacementTransactions = CreateTransactionsFromFile(filePath, transactionsType); // Find the Date/Time grouping the replacement transactions belong to, and add them to that grouping. foreach (var replacementTransaction in replacementTransactions) { ITransaction lastTransactionForDate = null; if (transactionsByDate.Any(x => x.Key.Equals(replacementTransaction.DateTime))) { lastTransactionForDate = transactionsByDate.Last(x => x.Key.Equals(replacementTransaction.DateTime)).Last(); } else { var dateTimeToCheckFor = replacementTransaction.DateTime.AddDays(-1); while (lastTransactionForDate == null) { if (transactionsByDate.Any(x => x.Key.Equals(dateTimeToCheckFor))) { lastTransactionForDate = transactionsByDate.Last(x => x.Key.Equals(dateTimeToCheckFor)).Last(); break; } if (dateTimeToCheckFor.Month != replacementTransaction.DateTime.Month) { // If the transaction to replace/add comes before any existing transactions // just add it to the list of transactions. lastTransactionForDate = replacementTransaction; } dateTimeToCheckFor = dateTimeToCheckFor.AddDays(-1); } } // use '+' because all of the values will be negative var newAccountBalance = lastTransactionForDate.AccountBalance + replacementTransaction.Value; // Make a new Transaction because Transactions are immutable var newTransaction = new Transaction( replacementTransaction.DateTime, replacementTransaction.Vendor, replacementTransaction.Value, newAccountBalance, replacementTransaction.Description, replacementTransaction.Category); int indexOfLastTransaction = transactions.IndexOf(lastTransactionForDate); if (indexOfLastTransaction == transactions.Count) { transactions.Add(newTransaction); } else { transactions.Insert(indexOfLastTransaction + 1, newTransaction); } } }
public IEnumerable <ITransaction> CreateTransactionsFromFile(string filePath, SupportedTransactionTypes transactionsType) { var stringTransactions = LoadTransactionsFromFile(filePath); ICollection <ITransaction> transactions = new List <ITransaction>(); stringTransactions.ForEach(x => { var transaction = _transactionsFactory.CreateTransaction(x, transactionsType); if (transaction != NoneTransaction.Instance) { transactions.Add(transaction); } }); return(transactions); }