diff --git a/src/pantry/core/inventory.go b/src/pantry/core/inventory.go index 30a3af7..4de6fe5 100644 --- a/src/pantry/core/inventory.go +++ b/src/pantry/core/inventory.go @@ -90,6 +90,24 @@ func (p *Inventory) RemoveItem(name string, quantity float64, expiry *time.Time) fmt.Printf("Item not found or no matching expiry.\n") } +func (p *Inventory) RemoveExpired() int { + removed := 0 + today := time.Now() + + for i := 0; i < len(p.Items); { + item := p.Items[i] + + if item.Expiry != nil && item.Expiry.Before(today) { + p.Items = append(p.Items[:i], p.Items[i+1:]...) + fmt.Printf("Removed expired %s (expiry: %s)\n", item.Name, item.Expiry.Format("2006-01-02")) + removed++ + } else { + i++ + } + } + return removed +} + func (p *Inventory) ListItems() { if len(p.Items) == 0 { fmt.Printf("Pantry is empty\n") diff --git a/src/pantry/main.go b/src/pantry/main.go index ffcb5b3..d692790 100644 --- a/src/pantry/main.go +++ b/src/pantry/main.go @@ -102,6 +102,15 @@ func main() { fmt.Println("Failed to save inventory:", err) } + case "remove-expired": + removed := inventory.RemoveExpired() + err = core.SaveInventory(dataFile, inventory) + if err != nil { + fmt.Println("Failed to save inventory:", err) + } else { + fmt.Printf("Removed %d expired items\n", removed) + } + case "list": inventory.ListItems()