Last active
December 30, 2015 05:29
-
-
Save jessearmand/7783275 to your computer and use it in GitHub Desktop.
A simple haskell module to calculate utilties cost.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module UtilitiesCost | |
( electricityCost | |
, gasCost | |
, waterCost | |
, utilitiesCost | |
) where | |
import Debug.Trace | |
data Reading = Reading Double Double deriving (Eq, Show, Read) | |
data ReadingRange = ReadingRange (Double, Double) deriving (Eq, Show, Read) | |
data WaterExtraCost = WaterExtraCost Double Double Double deriving (Eq, Show, Read) | |
electricityCost :: Reading -> Double -> Double -> Double -> Double | |
electricityCost (Reading initialReading finalReading) rate days daysPerMonth = | |
let usage = finalReading - initialReading | |
usagePerDay = usage / days | |
in usagePerDay * daysPerMonth * rate | |
gasCost :: Reading -> Double -> Double -> Double -> Double | |
gasCost (Reading initialReading finalReading) rate days daysPerMonth = | |
let usage = finalReading - initialReading | |
usagePerDay = usage / days | |
in usagePerDay * daysPerMonth * rate | |
waterCost :: Reading -> Double -> WaterExtraCost -> Double -> Double -> Double | |
waterCost (Reading initialReading finalReading) rate (WaterExtraCost waterborneRate taxRate sanitaryFee) days daysPerMonth = | |
let usage = finalReading - initialReading | |
usagePerDay = usage / days | |
waterFee = usagePerDay * daysPerMonth * rate | |
waterborneFee = usagePerDay * daysPerMonth * waterborneRate | |
tax = waterFee * taxRate | |
in waterFee + waterborneFee + sanitaryFee + tax | |
utilitiesCost :: ReadingRange -> ReadingRange -> ReadingRange -> Double -> Double -> Double | |
utilitiesCost (ReadingRange electricityReading) (ReadingRange gasReading) (ReadingRange waterReading) days daysPerMonth = | |
traceShow (electricityFee, gasFee, waterFee, refuseFee, gst) $ totalFee + gst | |
where | |
totalFee = electricityFee + gasFee + waterFee + refuseFee | |
refuseFee = 29 | |
gstRate = 0.07 | |
gst = gstRate * totalFee | |
electricityFee = electricityCost (Reading initialReading finalReading) rate days daysPerMonth | |
where initialReading = fst electricityReading | |
finalReading = snd electricityReading | |
rate = 0.2608 | |
gasFee = gasCost (Reading initialReading finalReading) rate days daysPerMonth | |
where initialReading = fst gasReading | |
finalReading = snd gasReading | |
rate = 0.2118 | |
waterFee = waterCost (Reading initialReading finalReading) rate (WaterExtraCost waterborneRate taxRate sanitaryFee) days daysPerMonth | |
where initialReading = fst waterReading | |
finalReading = snd waterReading | |
(rate, waterborneRate, taxRate) = (1.17, 0.5607, 0.3) | |
sanitaryFee = | |
let months = ceiling (daysPerMonth / 31.0) | |
in 2.28037 * fromIntegral months |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, @sovanlandy2 we need to maximize our time to avoid complexity. So little time, so much to do.