package analyzer // Analyzer processes Transactions and generates reports. type Analyzer struct { // The list of Transactions available to analyze. transactions Transactions } // Add adds unique transactions to Analyzer. func (anz *Analyzer) Add(transactions Transactions) int { for i := range transactions { dupe := false for j := range anz.transactions { if transactions[i] == anz.transactions[j] { dupe = true } } if !dupe { anz.transactions = append(anz.transactions, transactions[i]) } } return len(transactions) } // TransactionCount is the number of unique Transactions. func (anz *Analyzer) TransactionCount() int { return len(anz.transactions) } // TransactionsAmount is the total amount of all the transactions. func (anz *Analyzer) TransactionsAmount() Amount { return anz.transactions.Sum() } // LargestTransaction is the single largest transaction, ranked by // the absolute value of the transaction amount. Returns an error if there // are no transactions. // // - Note: Large negative transactions are still large--it just means the // money moved the other direction. func (anz *Analyzer) LargestTransaction() (Transaction, error) { if anz == nil || len(anz.transactions) == 0 { return Transaction{}, ErrNoTransactionsToAnalyze } largestIdx := 0 for i := 1; i < len(anz.transactions); i++ { if anz.transactions[largestIdx].Amount.AbsoluteValue() < anz.transactions[i].Amount.AbsoluteValue() { largestIdx = i } } return anz.transactions[largestIdx], nil } // ByCategory groups the transactions by category and returns a map of // category name to the list of transactions that are part of that category. func (anz *Analyzer) ByCategory() map[string]Transactions { result := map[string]Transactions{} for i := range anz.transactions { transactionsInCategory := result[anz.transactions[i].Category] transactionsInCategory = append(transactionsInCategory, anz.transactions[i]) result[anz.transactions[i].Category] = transactionsInCategory } return result } // BigSpenderReport generates our Big Spender Report. // // "Big spenders" are defined as those people who, in aggregate // across their transactions, have spent more than 2 standard // deviations above the mean spender. // // The method determines who they are and generates a well-formatted // report to call them out. func (anz *Analyzer) BigSpenderReport() string { report := ` Digits Big Spenders Report ------------------------------------------------------ Name Amount ------------------------------------------------------` // TODO: Not implemented. // TODO: As a reminder, we're looking for well-factored, well-organized // code. We encourage you to refactor, change, or add to any part // of the non-test code as you see fit! report += "" return report } // New creates a new Analyzer with an empty set of transactions func New() *Analyzer { return &Analyzer{transactions: Transactions{}} }