#[Copyright 2024 ITwrx. This file is part of WattstoDollars. WattstoDollars is released under the GNU General Public License 3.0. See COPYING or for details.]# import std/[strutils, math] import termstyle proc vInputInt(inputValue: string, inputPrompt: string, inputName: string): int = try: let intInputValue = parseInt(inputValue) return intInputValue except CatchableError: echo red "\N" & inputName & " must be a whole number.\N" echo inputPrompt return vInputInt(readLine(stdin), inputPrompt, inputName) proc vInputFloat(inputValue: string, inputPrompt: string, inputName: string): float = try: let floatInputValue = parseFloat(inputValue) return floatInputValue except CatchableError: echo red "\N" & inputName & " must be a float/decimal number.\N" echo inputPrompt return vInputFloat(readLine(stdin), inputPrompt, inputName) echo yellow "\N**** Whole numbers required for all inputs except electrical rate, which accepts a float/decimal number. ***\N" var prompt: string #get watts from user prompt = "Watts?\N" echo prompt echo yellow "Hint: You can enter the total watts used by a device, or enter the difference in watts between two devices, to see how much more a second device will cost.\N" var watts = vInputInt(readLine(stdin), prompt, "Watts") #get watt hrs from user. prompt = "\NNumber of hours of usage per day?\N" echo prompt var hours = vInputInt(readLine(stdin), prompt, "Hours") #get days from user. prompt = "\NNumber of days of usage?\N" echo prompt var days = vInputInt(readLine(stdin), prompt, "Days") #get energy rate from user. prompt = "\NElectrical rate per kilowatt hour?\N" echo prompt var kwRate = vInputFloat(readLine(stdin), prompt, "Electrical Rate") let watthrsPerDay: int = watts * hours #convert watts to kw hrs let kwh: float = watthrsPerDay / 1000 #get kwhr usage per # of days. let kwhUsage: float = kwh * parseFloat($days) #get $ usage. let energyCost: float = kwhUsage * parseFloat($kwRate) let roundedEnergyCost = round(energyCost, 2) echo "\N" & $watts & " watts, used " & $hours & " hours a day, for " & $days & " days = $" & $roundedEnergyCost & "\N"