feat|chore(summary|list): added summary with month filtering, fixed list

to show in a clean table
This commit is contained in:
DarthMooMancer committed 2026-11-13 14:17:35 -05:00
1 parent 988da3a978
commit 5743501447
2 files changed
+54 -41

No files matched your search

+3 -3
View File
@@ -3,7 +3,7 @@
"id": 1, "id": 1,
"string": "Gas Station ", "string": "Gas Station ",
"amount": 17, "amount": 17,
"date": "2026-11-13T13:16:45.202792-05:00" "date": "2026-10-13T13:16:45.202792-05:00"
}, },
{ {
"id": 2, "id": 2,
@@ -15,7 +15,7 @@
"id": 3, "id": 3,
"string": "Groceries ", "string": "Groceries ",
"amount": 100, "amount": 100,
"date": "2026-11-13T13:16:56.0525-05:00" "date": "2026-09-13T13:16:56.0525-05:00"
}, },
{ {
"id": 4, "id": 4,
@@ -27,7 +27,7 @@
"id": 5, "id": 5,
"string": "Not Food ", "string": "Not Food ",
"amount": 40, "amount": 40,
"date": "2026-11-13T13:17:02.100182-05:00" "date": "2026-01-13T13:17:02.100182-05:00"
}, },
{ {
"id": 6, "id": 6,
+51 -38
View File
@@ -9,6 +9,7 @@ import (
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
"text/tabwriter"
"time" "time"
) )
@@ -23,7 +24,7 @@ func add(fileName string, desc string, amount float64) error {
exp, err := loadExpenses(fileName) exp, err := loadExpenses(fileName)
if err != nil { if err != nil {
panic(err) return err
} }
var e Expense var e Expense
@@ -42,22 +43,27 @@ func add(fileName string, desc string, amount float64) error {
} }
func list(fileName string) error { func list(fileName string) error {
fmt.Println("ID Date Description Amount")
exp, err := loadExpenses(fileName) exp, err := loadExpenses(fileName)
if err != nil { if err != nil {
panic(err) return err
} }
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "ID\tDate\tDescription\tAmount\t")
for _, i := range exp { for _, i := range exp {
y, m, d := i.Date.Date() y, m, d := i.Date.Date()
fmt.Printf("%d %d-%s-%d %s $%.2f\n", i.Id, y, m, d, i.Desc, math.Round(float64(i.Amount)*100)/100) dateStr := fmt.Sprintf("%s %d, %d", m, d, y)
amount := math.Round(i.Amount*100) / 100
fmt.Fprintf(w, "%d\t%s\t%s\t$%.2f\n", i.Id, dateStr, i.Desc, amount)
} }
return nil return w.Flush()
} }
func delete(fileName string, idx int) error { func delete(fileName string, idx int) error {
exp, err := loadExpenses(fileName) exp, err := loadExpenses(fileName)
if err != nil { if err != nil {
panic(err) return err
} }
for i, value := range exp { for i, value := range exp {
@@ -75,25 +81,11 @@ func delete(fileName string, idx int) error {
return saveExpenses(exp, fileName) return saveExpenses(exp, fileName)
} }
/*
$ expense-tracker update --id 2 --description "new desc"
# Expense updated successfully
$ expense-tracker update --id 2 --amount 10
# Expense updated successfully
$ expense-tracker update --id 2 --description "new desc" --amount 10
# Expense updated successfully
takes in an id, a string for desc (if "" then it is blank and dont update), a new amount (if -1 then it is blank and dont update)
*/
func update(fileName string, idx int, desc string, amount float64) error { func update(fileName string, idx int, desc string, amount float64) error {
exp, err := loadExpenses(fileName) exp, err := loadExpenses(fileName)
if err != nil { if err != nil {
panic(err) return err
} }
var expense Expense var expense Expense
@@ -116,25 +108,31 @@ func update(fileName string, idx int, desc string, amount float64) error {
} }
/* func summary(fileName string, month string) error {
$ expense-tracker summary exp, err := loadExpenses(fileName)
# Total expenses: $30 if err != nil {
return err
}
$ expense-tracker summary var total float64
# Total expenses: $20 if month != "" {
for _, i := range exp {
$ expense-tracker summary --month 8 if strings.ToLower(i.Date.Month().String()) == month {
# Total expenses for August: $20 total += i.Amount
*/ }
func summary() { }
/* total = math.Round(total*100) / 100
total_title := strings.ToUpper(month[:1]) + month[1:]
1. Loads expenses to slice fmt.Printf("Total expenses for %s: $%.2f", total_title, total)
2. Count all amounts from each expense } else {
3. print expenses for _, i := range exp {
3.5 If month passed in then print all expenses for that month and only count for those months total += i.Amount
*/ }
total = math.Round(total*100) / 100
fmt.Printf("Total expenses: $%.2f", total)
}
return nil
} }
func saveExpenses(exp []Expense, fileName string) error { func saveExpenses(exp []Expense, fileName string) error {
@@ -258,5 +256,20 @@ func main() {
if ok != nil { if ok != nil {
panic(ok) panic(ok)
} }
} else if args[0] == "summary" {
var month string
for i := range args {
if args[i] == "--month" {
if len(args) > i {
month = args[i+1]
month = strings.ToLower(month)
}
}
}
ok := summary(fileName, month)
if ok != nil {
panic(ok)
}
} }
} }