digits-2023-10-15/digits-work-sample-go.d/analyzer.go

89 lines
2.7 KiB
Go

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 {
// TODO: Not implemented.
return nil
}
// 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{}}
}