// Package expense holds the rules of the expense tracker: what a valid expense // is, how amounts are written, and how totals are worked out. It knows nothing // about files, the web or the command line. // // This is the starter. The TODOs are in New, ParseAmount, InMonth and // Summarize. package expense import ( "errors" "fmt" "strings" "time" ) // DateLayout and MonthLayout are the reference layouts from Chapter 35. const DateLayout = "2006-01-02" const MonthLayout = "2006-01" const MaxCategoryLength = 30 const MaxNoteLength = 100 // MaxCents is the largest amount one expense can have: 1000000.00. const MaxCents = 100_000_000 // Expense is one thing that was paid for. Amounts are whole cents, so that // 0.10 + 0.20 is exactly 0.30. The JSON tags give the names used by the API. // omitempty leaves the ID out while it is still 0, so a new expense can be // sent to the API, which gives the ID itself. type Expense struct { ID int `json:"id,omitempty"` Date string `json:"date"` Category string `json:"category"` Note string `json:"note"` Cents int64 `json:"cents"` } // New trims and checks the values, and returns an expense without an ID. The // store gives it an ID when it is added. func New(date string, category string, note string, cents int64) (Expense, error) { // TODO: trim the three texts, and make the category lower case. Then check, // in this order, and return an error that says what is wrong: // - the date parses with time.Parse and DateLayout // - the category is not empty, and has at most MaxCategoryLength // characters (count them with utf8.RuneCountInString) // - the note has at most MaxNoteLength characters // - cents is more than 0 and at most MaxCents return Expense{}, errors.New("TODO: New is not written yet") } // ParseAmount turns text such as "12", "12.5" or "12.50" into cents. func ParseAmount(text string) (int64, error) { // TODO: trim the text, and split it at the dot with strings.Cut. The part // before the dot must be allDigits. If there is a dot, the part after it // must be allDigits too, with at most two digits. Pad that part to two // digits with "0", join the parts, and turn them into a number with // strconv.ParseInt. "12.5" becomes "12" + "50", which is 1250 cents. return 0, errors.New("TODO: ParseAmount is not written yet") } // allDigits reports whether text is not empty and has only the digits 0 to 9. func allDigits(text string) bool { if text == "" { return false } for _, r := range text { if r < '0' || r > '9' { return false } } return true } // FormatCents writes cents as an amount with two decimals: 1250 is "12.50". func FormatCents(cents int64) string { sign := "" if cents < 0 { sign = "-" cents = -cents } return fmt.Sprintf("%s%d.%02d", sign, cents/100, cents%100) } // CheckMonth returns an error if month is not written like 2026-11. func CheckMonth(month string) error { _, err := time.Parse(MonthLayout, month) if err != nil { return fmt.Errorf("the month %q must be written like 2026-11", month) } return nil } // InMonth returns the expenses whose date is in month, written like 2026-11. // An empty month returns all of them. func InMonth(list []Expense, month string) []Expense { // TODO: keep the expenses whose date starts with month + "-". Start with // an empty slice, not nil, so that the API sends [] and not null. return []Expense{} } // Sum is the total of one group, such as one category or one month. type Sum struct { Name string `json:"name"` Cents int64 `json:"cents"` } // Report is the totals of a list of expenses. type Report struct { Count int `json:"count"` TotalCents int64 `json:"total_cents"` ByCategory []Sum `json:"by_category"` ByMonth []Sum `json:"by_month"` } // Summarize adds up the expenses. Categories come largest first, and months // come oldest first. func Summarize(list []Expense) Report { // TODO: add up the total, and the total per category and per month in two // maps. The month is the first len(MonthLayout) characters of the date. // Turn each map into a slice with toSums. Sort the categories with // slices.SortFunc and largestFirst, and the months with byName. return Report{ByCategory: []Sum{}, ByMonth: []Sum{}} } // toSums turns a map of totals into a slice, which can be sorted. func toSums(totals map[string]int64) []Sum { sums := []Sum{} for name, cents := range totals { sums = append(sums, Sum{Name: name, Cents: cents}) } return sums } // largestFirst sorts sums by amount, largest first, and equal amounts by name. func largestFirst(a Sum, b Sum) int { if a.Cents > b.Cents { return -1 } if a.Cents < b.Cents { return 1 } return byName(a, b) } // byName sorts sums by name, A to Z. A month like 2026-11 sorts by date. func byName(a Sum, b Sum) int { return strings.Compare(a.Name, b.Name) }