From e771a40901b82d65392d14438e952bf1ac056177 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 15 Oct 2023 11:39:31 -0600 Subject: [PATCH] implement analyzer.go:Analyzer:LargestTransaction to return the first largest transaction in slice --- digits-work-sample-go.d/analyzer.go | 18 ++++++++----- digits-work-sample-go.d/errors.go | 7 ++++++ todo.yaml | 39 ++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 digits-work-sample-go.d/errors.go diff --git a/digits-work-sample-go.d/analyzer.go b/digits-work-sample-go.d/analyzer.go index 48d3363..ad14b95 100644 --- a/digits-work-sample-go.d/analyzer.go +++ b/digits-work-sample-go.d/analyzer.go @@ -1,7 +1,5 @@ package analyzer -import "errors" - // Analyzer processes Transactions and generates reports. type Analyzer struct { // The list of Transactions available to analyze. @@ -28,11 +26,19 @@ func (anz *Analyzer) TransactionsAmount() Amount { // 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. +// - Note: Large negative transactions are still large--it just means the +// money moved the other direction. func (anz *Analyzer) LargestTransaction() (Transaction, error) { - // TODO: Not implemented. - return Transaction{}, errors.New("not implemented") + 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 diff --git a/digits-work-sample-go.d/errors.go b/digits-work-sample-go.d/errors.go new file mode 100644 index 0000000..06d6ecf --- /dev/null +++ b/digits-work-sample-go.d/errors.go @@ -0,0 +1,7 @@ +package analyzer + +import "errors" + +var ( + ErrNoTransactionsToAnalyze = errors.New("no transactions to analyze") +) diff --git a/todo.yaml b/todo.yaml index bee9fef..7d51758 100755 --- a/todo.yaml +++ b/todo.yaml @@ -2,7 +2,6 @@ todo: - review readme tail to finish // answer 3 questions, gofmt - todo: go test subtasks: - - TestAnalyzer_LargestTransaction - TestAnalyzer_DuplicatesExcluded - TestAnalyzer_GroupByCategory - TestAnalyzer_BigSpendersReport @@ -16,6 +15,8 @@ todo: not be changed, or even what currency Amount is - transaction.go:Transaction:Sum again doesnt care about Amount currency or vendor/vendee drift + - analyzer.go:Analyzer:LargestTransaction doesn't specify how to break ties; stable + or latest? scheduled: [] done: - todo: hello world @@ -451,3 +452,39 @@ done: - transaction.go:Transaction:Sum again doesnt care about Amount currency or vendor/vendee drift ts: Sun Oct 15 11:36:45 MDT 2023 +- todo: go test + subtasks: + - TestAnalyzer_LargestTransaction + - TestAnalyzer_DuplicatesExcluded + - TestAnalyzer_GroupByCategory + - TestAnalyzer_BigSpendersReport + - TestAnalyzer_TransactionsFromURLs + - TestAnalyzer_TransactionsFromURLsConcurrent + - amount.go:Rounded probably does NOT handle float precision well... it is float64 + tho... + - my `go mod tidy` actually cleared `go.mod` file, probably weird localhost backwards + compatilble stuff + - transaction.go:Transaction:String not clear if FormatUSD or amount currency should + not be changed, or even what currency Amount is + - transaction.go:Transaction:Sum again doesnt care about Amount currency or vendor/vendee + drift + ts: Sun Oct 15 11:38:22 MDT 2023 +- todo: go test + subtasks: + - TestAnalyzer_LargestTransaction + - TestAnalyzer_DuplicatesExcluded + - TestAnalyzer_GroupByCategory + - TestAnalyzer_BigSpendersReport + - TestAnalyzer_TransactionsFromURLs + - TestAnalyzer_TransactionsFromURLsConcurrent + - amount.go:Rounded probably does NOT handle float precision well... it is float64 + tho... + - my `go mod tidy` actually cleared `go.mod` file, probably weird localhost backwards + compatilble stuff + - transaction.go:Transaction:String not clear if FormatUSD or amount currency should + not be changed, or even what currency Amount is + - transaction.go:Transaction:Sum again doesnt care about Amount currency or vendor/vendee + drift + - analyzer.go:Analyzer:LargestTransaction doesn't specify how to break ties; stable + or latest? + ts: Sun Oct 15 11:38:46 MDT 2023