feat|chore(summary|list): added summary with month filtering, fixed list
to show in a clean table
This commit is contained in:
1 parent
988da3a978
commit
5743501447
2 files changed
+87
-74
No files matched your search
+36
-36
@@ -1,38 +1,38 @@
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"string": "Gas Station ",
|
||||
"amount": 17,
|
||||
"date": "2026-11-13T13:16:45.202792-05:00"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"string": "Gas Station ",
|
||||
"amount": 19,
|
||||
"date": "2026-11-13T13:16:50.830484-05:00"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"string": "Groceries ",
|
||||
"amount": 100,
|
||||
"date": "2026-11-13T13:16:56.0525-05:00"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"string": "Gas Station ",
|
||||
"amount": 22.21312,
|
||||
"date": "2026-11-13T13:16:58.881653-05:00"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"string": "Not Food ",
|
||||
"amount": 40,
|
||||
"date": "2026-11-13T13:17:02.100182-05:00"
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"string": "Not Groceries ",
|
||||
"amount": 100,
|
||||
"date": "2026-11-13T13:35:58.552665-05:00"
|
||||
}
|
||||
{
|
||||
"id": 1,
|
||||
"string": "Gas Station ",
|
||||
"amount": 17,
|
||||
"date": "2026-10-13T13:16:45.202792-05:00"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"string": "Gas Station ",
|
||||
"amount": 19,
|
||||
"date": "2026-11-13T13:16:50.830484-05:00"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"string": "Groceries ",
|
||||
"amount": 100,
|
||||
"date": "2026-09-13T13:16:56.0525-05:00"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"string": "Gas Station ",
|
||||
"amount": 22.21312,
|
||||
"date": "2026-11-13T13:16:58.881653-05:00"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"string": "Not Food ",
|
||||
"amount": 40,
|
||||
"date": "2026-01-13T13:17:02.100182-05:00"
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"string": "Not Groceries ",
|
||||
"amount": 100,
|
||||
"date": "2026-11-13T13:35:58.552665-05:00"
|
||||
}
|
||||
]
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -23,7 +24,7 @@ func add(fileName string, desc string, amount float64) error {
|
||||
exp, err := loadExpenses(fileName)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
|
||||
var e Expense
|
||||
@@ -42,22 +43,27 @@ func add(fileName string, desc string, amount float64) error {
|
||||
}
|
||||
|
||||
func list(fileName string) error {
|
||||
fmt.Println("ID Date Description Amount")
|
||||
exp, err := loadExpenses(fileName)
|
||||
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 {
|
||||
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 {
|
||||
exp, err := loadExpenses(fileName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
|
||||
for i, value := range exp {
|
||||
@@ -75,25 +81,11 @@ func delete(fileName string, idx int) error {
|
||||
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 {
|
||||
|
||||
exp, err := loadExpenses(fileName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
|
||||
var expense Expense
|
||||
@@ -116,25 +108,31 @@ func update(fileName string, idx int, desc string, amount float64) error {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
$ expense-tracker summary
|
||||
# Total expenses: $30
|
||||
func summary(fileName string, month string) error {
|
||||
exp, err := loadExpenses(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
$ expense-tracker summary
|
||||
# Total expenses: $20
|
||||
|
||||
$ expense-tracker summary --month 8
|
||||
# Total expenses for August: $20
|
||||
*/
|
||||
func summary() {
|
||||
/*
|
||||
|
||||
1. Loads expenses to slice
|
||||
2. Count all amounts from each expense
|
||||
3. print expenses
|
||||
3.5 If month passed in then print all expenses for that month and only count for those months
|
||||
*/
|
||||
var total float64
|
||||
if month != "" {
|
||||
for _, i := range exp {
|
||||
if strings.ToLower(i.Date.Month().String()) == month {
|
||||
total += i.Amount
|
||||
}
|
||||
}
|
||||
total = math.Round(total*100) / 100
|
||||
total_title := strings.ToUpper(month[:1]) + month[1:]
|
||||
fmt.Printf("Total expenses for %s: $%.2f", total_title, total)
|
||||
} else {
|
||||
for _, i := range exp {
|
||||
total += i.Amount
|
||||
}
|
||||
total = math.Round(total*100) / 100
|
||||
fmt.Printf("Total expenses: $%.2f", total)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveExpenses(exp []Expense, fileName string) error {
|
||||
@@ -258,5 +256,20 @@ func main() {
|
||||
if ok != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user