Skip to content

Instantly share code, notes, and snippets.

@dfinke
Last active July 3, 2024 17:24
Show Gist options
  • Save dfinke/141b6b02174c3d205e60983dbac99cb9 to your computer and use it in GitHub Desktop.
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.
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()
@dfinke
Copy link
Author

dfinke commented Jun 6, 2024

[Plain Text] Report as of Thu 06 06 2024

Region Product Cost
------ ------- ----
North  Pear    0.75
South  Banana  0.35
East   Lime    0.85
West   Apple   0.15

Sending email

<h2>[Html] Report as of Thu 06 06 2024 </h2>
<ul>
<li>North Pear 0.75</li>
<li>South Banana 0.35</li>
<li>East Lime 0.85</li>
<li>West Apple 0.15</li>
</ul>

Sending email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment