feat(delete): smart delete

This commit is contained in:
DarthMooMancer committed 2026-11-13 13:21:43 -05:00
1 parent 28e836af23
commit d3b4431228
2 files changed
+67 -48

No files matched your search

+24 -6
View File
@@ -1,14 +1,32 @@
[
{
"id": 1,
"string": "hello",
"amount": 0,
"date": "2026-11-12T13:03:20.54894-05:00"
"string": "Gas Station ",
"amount": 17,
"date": "2026-11-13T13:16:45.202792-05:00"
},
{
"id": 2,
"string": "hello",
"amount": 10,
"date": "2026-11-12T13:03:35.810063-05:00"
"string": "Gas Station ",
"amount": 19,
"date": "2026-11-13T13:16:50.830484-05:00"
},
{
"id": 3,
"string": "Gas Station ",
"amount": 22123,
"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": "Gas Station ",
"amount": 22.3143,
"date": "2026-11-13T13:17:02.100182-05:00"
}
]
+43 -42
View File
@@ -6,17 +6,12 @@ import (
"fmt"
"math"
"os"
"slices"
"strconv"
"strings"
"time"
)
/*
Id int
Desc string
Amount float
Date date.now()
*/
type Expense struct {
Id int `json:"id"`
Desc string `json:"string"`
@@ -24,29 +19,7 @@ type Expense struct {
Date time.Time `json:"date"`
}
/*
--description: Label for the expense : string
--amount how much it cost : float
saves date as well
returns an expense table to be saved later
*/
/*
$ expense-tracker add --description "Lunch" --amount 20
# Expense added successfully (ID: 1)
$ expense-tracker add --description "Dinner" --amount 10
# Expense added successfully (ID: 2)
*/
func add(fileName string, desc string, amount float64) error {
/*
1. Takes in a fileName and loades an Expense slice
2. Creates a new Expense object with id++ of last id in Expense slice, date, desc and amount
3. return Expense slice
*/
exp, err := loadExpenses(fileName)
if err != nil {
@@ -68,12 +41,6 @@ func add(fileName string, desc string, amount float64) error {
return saveExpenses(exp, fileName)
}
/*
$ expense-tracker list
# ID Date Description Amount
# 1 2024-08-06 Lunch $20
# 2 2024-08-06 Dinner $10
*/
func list(fileName string) error {
fmt.Println("ID Date Description Amount")
exp, err := loadExpenses(fileName)
@@ -93,8 +60,25 @@ $ expense-tracker delete --id 2
takes Id and loops over loaded tasks to remove object
*/
func delete() {
func delete(fileName string, idx int) error {
exp, err := loadExpenses(fileName)
if err != nil {
panic(err)
}
for i, value := range exp {
if value.Id == idx {
exp = slices.Delete(exp, i, i+1)
}
}
var count int = 1
for i := 0; i < len(exp); i++ {
exp[i].Id = count
count++
}
return saveExpenses(exp, fileName)
}
/*
@@ -176,11 +160,17 @@ func main() {
if args[0] == "add" {
var amount float64
var desc string
var desc strings.Builder
for i := range args {
if args[i] == "--description" {
if len(args) > i {
desc = args[i+1]
for j := i + 1; j < len(args); j++ {
if strings.HasPrefix(args[j], "--") {
break
}
desc.WriteString(args[j])
desc.WriteString(" ")
}
}
}
if args[i] == "--amount" {
@@ -193,14 +183,25 @@ func main() {
}
}
}
add(fileName, desc, amount)
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])
}
}
}
ok := delete(fileName, id)
if ok != nil {
panic(ok)
}
}
// Send expenses slice to functions instead of making them load each time because io is slower than memory management
// fmt.Println("Hello World")
// add(&exp, "test", 10)
}