Last active
July 3, 2024 17:24
-
-
Save dfinke/141b6b02174c3d205e60983dbac99cb9 to your computer and use it in GitHub Desktop.
PowerShell script using the Template Method pattern to generate financial reports in plain text and HTML formats. Extensible and flexible.
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
class ReportTemplate { | |
hidden $data | |
GenerateReport() { | |
$this.RetrieveFinancialData() | |
$this.FormatReport() | |
$this.SendToStakeholders() | |
} | |
RetrieveFinancialData() { | |
# Pull the data | |
$this.data = $( | |
[PSCustomObject]@{Region = 'North'; 'Product' = 'Pear'; Cost = .75 } | |
[PSCustomObject]@{Region = 'South'; 'Product' = 'Banana'; Cost = .35 } | |
[PSCustomObject]@{Region = 'East'; 'Product' = 'Lime'; Cost = .85 } | |
[PSCustomObject]@{Region = 'West'; 'Product' = 'Apple'; Cost = .15 } | |
) | |
} | |
FormatReport() { | |
throw "FormatReport Not Implemented" | |
} | |
SendToStakeholders() { | |
"Sending email" | out-host | |
'' | out-host | |
} | |
} | |
class TextReport : ReportTemplate { | |
FormatReport() { | |
$r = @("[Plain Text] Report as of $((Get-Date).ToShortDateString())") | |
$r += $this.data | |
$r | out-host | |
} | |
} | |
class HtmlReport : ReportTemplate { | |
FormatReport() { | |
$r = @("<h2>[Html] Report as of $((Get-Date).ToShortDateString()) </h2>") | |
$r += "<ul>" | |
$r += $this.data | ForEach-Object { "<li>{0} {1} {2}</li>" -f $_.Region, $_.Product, $_.Cost } | |
$r += "</ul>" | |
$r += "" | |
$r | out-host | |
} | |
} | |
[TextReport]::new().GenerateReport() | |
[HtmlReport]::new().GenerateReport() |
Author
dfinke
commented
Jun 6, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment