You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.5 KiB
Haskell

module RecipeCost where
-- * Data types definition
data Ingredient = Ingredient { ingredientName :: String
, unit_cost :: Double
}deriving (Show)
data UsedIngredient = UsedIngredient { ingredient :: Ingredient, quantityUsed :: Double }deriving (Show)
data Recipe = Recipe { recipeName :: String
, billOfMaterial :: [ UsedIngredient ]
, finalQuantity :: Double
, useNumber :: Double
}deriving (Show)
-- * Calculations
recipeCost :: Recipe -> Double
recipeCost (Recipe _ bom _ _) = sum . map ingredientCost $ bom
ingredientCost :: UsedIngredient -> Double
ingredientCost (UsedIngredient (Ingredient _ uCost) qtt) = uCost * qtt
unitCost :: Recipe -> Double -> Double
unitCost (Recipe _ _ qtt _) totalCost = totalCost / qtt
useCost :: Recipe -> Double -> Double
useCost (Recipe _ _ _ uses) totalCost = totalCost / uses
() :: Ingredient -> Double -> UsedIngredient
() ingredient qtt = UsedIngredient ingredient qtt
-- * Presentation
printRecipeCost :: Recipe -> IO ()
printRecipeCost recipe = do
putStrLn $ "* " ++ recipeName recipe ++" :"
print recipe
let recipeTotalCost = recipeCost recipe
recipeUnitCost = unitCost recipe recipeTotalCost
recipeUseCost = useCost recipe recipeTotalCost
putStrLn $ " * Prix Total = " ++ show recipeTotalCost ++ ""
putStrLn $ " * Prix à l'unité = " ++ show recipeUnitCost ++ ""
putStrLn $ " * Prix par utilisation = " ++ show recipeUseCost ++ ""