Files
expense-tracker/main.go
T

200 lines
3.8 KiB
Go

package main
import (
"encoding/json"
"errors"
"os"
"strconv"
"time"
)
/*
Id int
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"`
}
/*
--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 {
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)
}
/*
$ expense-tracker list
# ID Date Description Amount
# 1 2024-08-06 Lunch $20
# 2 2024-08-06 Dinner $10
*/
func list() {
/*
1. Load tasks
2. Print a single newline statement for section headers
3. Loop over tasks and print id, date, descript, amount
*/
}
/*
$ expense-tracker delete --id 2
# Expense deleted successfully
takes Id and loops over loaded tasks to remove object
*/
func delete() {
}
/*
$ 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() {
}
/*
$ expense-tracker summary
# Total expenses: $30
$ 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
*/
}
// Saves expenses table to a json file <expenses.json>
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
}
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() {
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)
// }
}