diff --git a/expenses.json b/expenses.json index 559a914..d0d16b0 100644 --- a/expenses.json +++ b/expenses.json @@ -13,8 +13,8 @@ }, { "id": 3, - "string": "Gas Station ", - "amount": 22123, + "string": "Groceries ", + "amount": 100, "date": "2026-11-13T13:16:56.0525-05:00" }, { @@ -25,8 +25,14 @@ }, { "id": 5, - "string": "Gas Station ", - "amount": 22.3143, + "string": "Food ", + "amount": 40, "date": "2026-11-13T13:17:02.100182-05:00" + }, + { + "id": 6, + "string": "Groceries", + "amount": 100, + "date": "2026-11-13T13:35:58.552665-05:00" } ] diff --git a/main.go b/main.go index 53f5493..44c06fc 100644 --- a/main.go +++ b/main.go @@ -54,12 +54,6 @@ func list(fileName string) error { return nil } -/* -$ expense-tracker delete --id 2 -# Expense deleted successfully - -takes Id and loops over loaded tasks to remove object -*/ func delete(fileName string, idx int) error { exp, err := loadExpenses(fileName) if err != nil { @@ -95,7 +89,30 @@ $ expense-tracker update --id 2 --description "new desc" --amount 10 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() { +func update(fileName string, idx int, desc string, amount float64) error { + + exp, err := loadExpenses(fileName) + if err != nil { + panic(err) + } + + var expense Expense + + for c, i := range exp { + if i.Id == idx { + expense = i + if desc != "" { + expense.Desc = desc + } + + if amount != -1 { + expense.Amount = amount + } + exp[c] = expense + } + } + + return saveExpenses(exp, fileName) } @@ -120,7 +137,6 @@ func summary() { } -// Saves expenses table to a json file func saveExpenses(exp []Expense, fileName string) error { file, err := os.Create(fileName) if err != nil { @@ -159,8 +175,56 @@ func main() { var fileName string = "expenses.json" if args[0] == "add" { + var amount float64 + var desc string + for i := range args { + if args[i] == "--description" { + if len(args) > i { + var build_desc strings.Builder + for j := i + 1; j < len(args); j++ { + if strings.HasPrefix(args[j], "--") { + break + } + build_desc.WriteString(args[j]) + build_desc.WriteString(" ") + } + desc = strings.Trim(build_desc.String(), " ") + } + } + if args[i] == "--amount" { + if len(args) > i { + a, err := strconv.ParseFloat(args[i+1], 64) + if err != nil { + panic(err) + } + amount = a + } + } + } + add(fileName, desc, amount) + } else if args[0] == "list" { + err := list(fileName) + if err != nil { + panic(err) + } + } else if args[0] == "delete" { + var id int + for i := range args { + if args[i] == "--id" { + if len(args) > i { + id, _ = strconv.Atoi(args[i+1]) + } + } + } + + ok := delete(fileName, id) + if ok != nil { + panic(ok) + } + } else if args[0] == "update" { var amount float64 var desc strings.Builder + var id int for i := range args { if args[i] == "--description" { if len(args) > i { @@ -182,24 +246,15 @@ func main() { amount = a } } - } - add(fileName, desc.String(), amount) - } else if args[0] == "list" { - err := list(fileName) - if err != nil { - panic(err) - } - } else if args[0] == "delete" { - var id int - for i := range args { - if args[i] == "--id" { - if len(args) > i { - id, _ = strconv.Atoi(args[i+1]) + for i := range args { + if args[i] == "--id" { + if len(args) > i { + id, _ = strconv.Atoi(args[i+1]) + } } } } - - ok := delete(fileName, id) + ok := update(fileName, id, desc.String(), amount) if ok != nil { panic(ok) }