-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [New Exercise]: Ledger * Update Ledger.ps1 Update base file to pass all tests * Update Ledger.ps1 change from using culture to simply currency format * update example file * Update Ledger.example.ps1
- Loading branch information
Showing
7 changed files
with
652 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Instructions | ||
|
||
Refactor a ledger printer. | ||
|
||
The ledger exercise is a refactoring exercise. | ||
There is code that prints a nicely formatted ledger, given a locale (American or Dutch) and a currency (US dollar or euro). | ||
The code however is rather badly written, though (somewhat surprisingly) it consistently passes the test suite. | ||
|
||
Rewrite this code. | ||
Remember that in refactoring the trick is to make small steps that keep the tests passing. | ||
That way you can always quickly go back to a working version. | ||
Version control tools like git can help here as well. | ||
|
||
Please keep a log of what changes you've made and make a comment on the exercise containing that log, this will help reviewers. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<# | ||
.SYNOPSIS | ||
Refactor a ledger printer. | ||
.DESCRIPTION | ||
The code below is an attempt of creating a printer for a ledger. | ||
It barely works (only passed some of the tests), and is generally quite messy. | ||
Your job here is to refactor the code. | ||
.EXAMPLE | ||
$entry1 = CreateEntry -Date '2011-12-13' -Desc 'Birthday present' -Amount 1234 | ||
$entry2 = CreateEntry -Date '2011-11-19' -Desc 'Party prep & catering services' -Amount 98765 | ||
FormatEntries -Currency "EUR" -Locale "en-US" -Entries @($entry1, $entry2) | ||
Returns: | ||
@" | ||
Date | Description | Change | ||
11/19/2011 | Party prep & catering ... | €987.65 | ||
12/13/2011 | Birthday present | €12.34 | ||
"@ | ||
#> | ||
|
||
Class LedgerEntry{ | ||
[datetime] $Date | ||
[string] $Desc | ||
[int] $Change | ||
|
||
LedgerEntry($date, $desc, $change) { | ||
$this.Date = $date | ||
$this.Desc = $desc | ||
$this.Change = $change | ||
} | ||
|
||
[string] Format([string] $Currency, [string] $Locale) { | ||
$format = $this.GetFrame($Locale, $this.Change) | ||
$dateFormat = $this.GetDateFormat($Currency, $Locale) | ||
|
||
$dateStr = $this.Date.ToString($dateFormat) | ||
$moneyStr = ($this.Change / 100).ToString("C2", [cultureinfo]::new($Locale)) | ||
|
||
if ($Currency -eq "EUR" -and $Locale -eq "en-US") { | ||
$moneyStr = $moneyStr -replace '\$', '€' | ||
} | ||
if ($Currency -eq "USD" -and $Locale -eq "nl-NL") { | ||
$moneyStr = $moneyStr -replace '€', '$' | ||
} | ||
if ($this.Desc.Length -gt 25) { | ||
$this.Desc = $this.Desc.Substring(0,22) + "..." | ||
} | ||
return $format -f @($dateStr, $this.Desc, $moneyStr) | ||
} | ||
|
||
[string] hidden GetFrame([string] $Locale, [int] $Change) { | ||
if ($Locale -eq "nl-NL" -or $Change -ge 0) { | ||
return "{0,-10} | {1,-25} | {2,12} " | ||
} | ||
return "{0,-10} | {1,-25} | {2,13}" | ||
} | ||
|
||
[object] hidden GetDateFormat([string] $Currency, [string] $Locale) { | ||
$dateFrm = switch ($Locale) { | ||
"en-US" { "MM\/dd\/yyyy" } | ||
"nl-NL" { "dd-MM-yyyy" } | ||
Default {Throw "Locale not supported"} | ||
} | ||
return $dateFrm | ||
} | ||
|
||
[string] static Header($Locale) { | ||
$format = "{0,-10} | {1,-25} | {2,-13}" | ||
$info = switch ($Locale) { | ||
"en-US" { @("Date", "Description", "Change") } | ||
"nl-NL" { @("Datum", "Omschrijving", "Verandering") } | ||
Default {Throw "Locale not supported"} | ||
} | ||
return $format -f $info | ||
} | ||
} | ||
|
||
Function CreateEntry { | ||
<# | ||
.DESCRIPTION | ||
A function to create an entry for the ledger. | ||
This function is required for the test suite. | ||
.PARAMETER Date | ||
String represent the date. | ||
.PARAMETER Desc | ||
String represent the description of the entry. | ||
.PARAMETER Amount | ||
Integer represent the amount of money in cents. | ||
#> | ||
param ( | ||
[string] $Date, | ||
[string] $Desc, | ||
[int] $Amount | ||
) | ||
[LedgerEntry]::new($Date, $Desc, $Amount) | ||
} | ||
|
||
Function FormatEntries { | ||
<# | ||
.DESCRIPTION | ||
A function to formats the entries of the ledger based on other info. | ||
This function is required for the test suite. | ||
.PARAMETER Currency | ||
String represent the currency symbol. | ||
.PARAMETER Locale | ||
String represent the region and culture to be followed. | ||
.PARAMETER Entries | ||
Array of entries, each is an instance of the class LedgerEntry, created via CreateEntry function. | ||
#> | ||
param ( | ||
[string] $Currency, | ||
[string] $Locale, | ||
[LedgerEntry[]] $Entries | ||
) | ||
$table = @() | ||
$table += [LedgerEntry]::Header($Locale) | ||
$Entries = $Entries | Sort-Object Date, Desc, Change | ||
foreach ($entry in $Entries) { | ||
$table += $entry.Format($Currency, $Locale) | ||
} | ||
$table -join "`n" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"authors": [ | ||
"glaxxie" | ||
], | ||
"files": { | ||
"solution": [ | ||
"Ledger.ps1" | ||
], | ||
"test": [ | ||
"Ledger.tests.ps1" | ||
], | ||
"example": [ | ||
".meta/Ledger.example.ps1" | ||
] | ||
}, | ||
"blurb": "Refactor a ledger printer." | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[d131ecae-a30e-436c-b8f3-858039a27234] | ||
description = "empty ledger" | ||
|
||
[ce4618d2-9379-4eca-b207-9df1c4ec8aaa] | ||
description = "one entry" | ||
|
||
[8d02e9cb-e6ee-4b77-9ce4-e5aec8eb5ccb] | ||
description = "credit and debit" | ||
|
||
[502c4106-0371-4e7c-a7d8-9ce33f16ccb1] | ||
description = "multiple entries on same date ordered by description" | ||
|
||
[29dd3659-6c2d-4380-94a8-6d96086e28e1] | ||
description = "final order tie breaker is change" | ||
|
||
[9b9712a6-f779-4f5c-a759-af65615fcbb9] | ||
description = "overlong description is truncated" | ||
|
||
[67318aad-af53-4f3d-aa19-1293b4d4c924] | ||
description = "euros" | ||
|
||
[bdc499b6-51f5-4117-95f2-43cb6737208e] | ||
description = "Dutch locale" | ||
|
||
[86591cd4-1379-4208-ae54-0ee2652b4670] | ||
description = "Dutch locale and euros" | ||
|
||
[876bcec8-d7d7-4ba4-82bd-b836ac87c5d2] | ||
description = "Dutch negative number with 3 digits before decimal point" | ||
|
||
[29670d1c-56be-492a-9c5e-427e4b766309] | ||
description = "American negative number with 3 digits before decimal point" |
Oops, something went wrong.