feat(add): can add new expenses to list
This commit is contained in:
1 parent
fa02041867
commit
0eb44d4bc4
2 files changed
+105
-7
No files matched your search
@@ -0,0 +1,14 @@
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"string": "hello",
|
||||
"amount": 0,
|
||||
"date": "2026-11-12T13:03:20.54894-05:00"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"string": "hello",
|
||||
"amount": 10,
|
||||
"date": "2026-11-12T13:03:35.810063-05:00"
|
||||
}
|
||||
]
|
||||
@@ -1,7 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -10,7 +14,12 @@ Desc string
|
||||
Amount float
|
||||
Date date.now()
|
||||
*/
|
||||
|
||||
type Expense struct {
|
||||
Id int `json:"id"`
|
||||
Desc string `json:"string"`
|
||||
Amount float64 `json:"amount"`
|
||||
Date time.Time `json:"date"`
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -29,12 +38,32 @@ $ expense-tracker add --description "Lunch" --amount 20
|
||||
$ expense-tracker add --description "Dinner" --amount 10
|
||||
# Expense added successfully (ID: 2)
|
||||
*/
|
||||
func add(fileName string, desc string, amount float32) []Expense {
|
||||
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 {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var e Expense
|
||||
e.Desc = desc
|
||||
e.Amount = amount
|
||||
e.Date = time.Now()
|
||||
if len(exp) == 0 {
|
||||
e.Id = 1
|
||||
|
||||
} else {
|
||||
e.Id = 1 + exp[len(exp)-1].Id
|
||||
}
|
||||
|
||||
exp = append(exp, e)
|
||||
return saveExpenses(exp, fileName)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -101,15 +130,70 @@ func summary() {
|
||||
}
|
||||
|
||||
// Saves expenses table to a json file <expenses.json>
|
||||
func saveExpenses() {
|
||||
|
||||
func saveExpenses(exp []Expense, fileName string) error {
|
||||
file, err := os.Create(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.SetIndent("", " ")
|
||||
encoder.Encode(exp)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Loades expenses table from a json file <expenses.json>
|
||||
func loadExpenses() {
|
||||
func loadExpenses(fileName string) ([]Expense, error) {
|
||||
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) {
|
||||
return []Expense{}, nil
|
||||
}
|
||||
file, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var expenses []Expense
|
||||
decoder := json.NewDecoder(file)
|
||||
decoder.DisallowUnknownFields()
|
||||
|
||||
if err := decoder.Decode(&expenses); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return expenses, err
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello World")
|
||||
args := os.Args[1:]
|
||||
|
||||
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 {
|
||||
desc = args[i+1]
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
// 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)
|
||||
|
||||
// for _, i := range exp {
|
||||
// y, m, d := i.Date.Date()
|
||||
// fmt.Printf("[%d], %s: %.2f at %d-%s-%d", i.Id, i.Desc, math.Round(float64(i.Amount)*100)/100, y, m, d)
|
||||
// }
|
||||
}
|
||||
Reference in new issue
Block a user