module Grocery.Data.Grocery where type Category = String data Recurrence = Low | Medium | High deriving (Show, Eq, Ord, Read) data Grocery = Grocery {name::String, inStock::Bool, recurrence::Recurrence, categories::[Category]} deriving (Eq, Ord, Show, Read) -- default Grocery data grocery = Grocery {name="", inStock=False, recurrence=High, categories=[]} -- Create a default Grocery item with the specified name createGrocery :: String -> Grocery createGrocery s = grocery {name=s} -- Creates a default initialized list of Grocery items from a list of names createList :: [String] -> [Grocery] createList = map createGrocery -- Saves list of Grocery items to a file saveGroceryList :: FilePath -> [Grocery] -> IO () saveGroceryList f groceries = do let output = map show groceries writeFile f $ unlines output -- Loads list of Grocery items from a file loadGroceryList :: FilePath -> IO [Grocery] loadGroceryList f = do contents <- readFile f let items = lines contents return $ map read items -- Returns list of Groceries "in stock" getInStock :: [Grocery] -> [Grocery] getInStock = filter inStock -- Returns a list of Groceries "not in stock" getNotInStock :: [Grocery] -> [Grocery] getNotInStock = filter (not . inStock) -- Converts string to Recurrence stringToRecurrence :: String -> Recurrence stringToRecurrence "high" = High stringToRecurrence "med" = Medium stringToRecurrence "low" = Low