11using System . Globalization ;
22
3- public class LedgerEntry
4- {
5- public LedgerEntry ( DateTime date , string description , decimal change )
6- {
7- Date = date ;
8- Description = description ;
9- Change = change ;
10- }
11-
12- public DateTime Date { get ; }
13- public string Description { get ; }
14- public decimal Change { get ; }
15- }
3+ public record LedgerEntry ( DateTime Date , string Description , decimal Change ) ;
164
175public static class Ledger
186{
197 private const int TruncateLength = 25 ;
208 private const string TruncateSuffix = "..." ;
219
2210 public static LedgerEntry CreateEntry ( string date , string description , int change ) =>
23- new LedgerEntry ( ParseDate ( date ) , description , ParseChange ( change ) ) ;
11+ new ( ParseDate ( date ) , description , ParseChange ( change ) ) ;
2412
2513 private static DateTime ParseDate ( string date ) => DateTime . Parse ( date , System . Globalization . CultureInfo . InvariantCulture ) ;
2614
2715 private static decimal ParseChange ( int change ) => change / 100.0m ;
2816
29- private static CultureInfo CultureInfo ( string locale )
30- {
31- switch ( locale )
17+ private static CultureInfo CultureInfo ( string locale ) =>
18+ locale switch
3219 {
33- case "en-US" : return new CultureInfo ( "en-US" , false ) ;
34- case "nl-NL" : return new CultureInfo ( "nl-NL" , false ) ;
35- default : throw new ArgumentException ( "Invalid locale" ) ;
36- }
37- }
20+ "en-US" => new CultureInfo ( "en-US" , false ) ,
21+ "nl-NL" => new CultureInfo ( "nl-NL" , false ) ,
22+ _ => throw new ArgumentException ( "Invalid locale" )
23+ } ;
3824
39- private static string CurrencySymbol ( string currency )
40- {
41- switch ( currency )
25+ private static string CurrencySymbol ( string currency ) =>
26+ currency switch
4227 {
43- case "USD" : return "$" ;
44- case "EUR" : return "€" ;
45- default : throw new ArgumentException ( "Invalid currency" ) ;
46- }
47- }
28+ "USD" => "$" ,
29+ "EUR" => "€" ,
30+ _ => throw new ArgumentException ( "Invalid currency" )
31+ } ;
4832
49- private static int CurrencyNegativePattern ( string locale )
50- {
51- switch ( locale )
33+ private static int CurrencyNegativePattern ( string locale ) =>
34+ locale switch
5235 {
53- case "en-US" : return 0 ;
54- case "nl-NL" : return 12 ;
55- default : throw new ArgumentException ( "Invalid locale" ) ;
56- }
57- }
36+ "en-US" => 0 ,
37+ "nl-NL" => 12 ,
38+ _ => throw new ArgumentException ( "Invalid locale" )
39+ } ;
5840
59- private static string ShortDateFormat ( string locale )
60- {
61- switch ( locale )
41+ private static string ShortDateFormat ( string locale ) =>
42+ locale switch
6243 {
63- case "en-US" : return "MM/dd/yyyy" ;
64- case "nl-NL" : return "dd/MM/yyyy" ;
65- default : throw new ArgumentException ( "Invalid locale" ) ;
66- }
67- }
44+ "en-US" => "MM/dd/yyyy" ,
45+ "nl-NL" => "dd/MM/yyyy" ,
46+ _ => throw new ArgumentException ( "Invalid locale" )
47+ } ;
6848
6949 private static CultureInfo getCulture ( string currency , string locale )
7050 {
@@ -75,15 +55,13 @@ private static CultureInfo getCulture(string currency, string locale)
7555 return culture ;
7656 }
7757
78- private static string FormatHeader ( CultureInfo culture )
79- {
80- switch ( culture . Name )
58+ private static string FormatHeader ( CultureInfo culture ) =>
59+ culture . Name switch
8160 {
82- case "en-US" : return "Date | Description | Change " ;
83- case "nl-NL" : return "Datum | Omschrijving | Verandering " ;
84- default : throw new ArgumentException ( "Invalid locale" ) ;
85- }
86- }
61+ "en-US" => "Date | Description | Change " ,
62+ "nl-NL" => "Datum | Omschrijving | Verandering " ,
63+ _ => throw new ArgumentException ( "Invalid locale" )
64+ } ;
8765
8866 private static string FormatDate ( IFormatProvider culture , DateTime date ) => date . ToString ( "d" , culture ) ;
8967
0 commit comments