1
1
using System . Globalization ;
2
2
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 ) ;
16
4
17
5
public static class Ledger
18
6
{
19
7
private const int TruncateLength = 25 ;
20
8
private const string TruncateSuffix = "..." ;
21
9
22
10
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 ) ) ;
24
12
25
13
private static DateTime ParseDate ( string date ) => DateTime . Parse ( date , System . Globalization . CultureInfo . InvariantCulture ) ;
26
14
27
15
private static decimal ParseChange ( int change ) => change / 100.0m ;
28
16
29
- private static CultureInfo CultureInfo ( string locale )
30
- {
31
- switch ( locale )
17
+ private static CultureInfo CultureInfo ( string locale ) =>
18
+ locale switch
32
19
{
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
+ } ;
38
24
39
- private static string CurrencySymbol ( string currency )
40
- {
41
- switch ( currency )
25
+ private static string CurrencySymbol ( string currency ) =>
26
+ currency switch
42
27
{
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
+ } ;
48
32
49
- private static int CurrencyNegativePattern ( string locale )
50
- {
51
- switch ( locale )
33
+ private static int CurrencyNegativePattern ( string locale ) =>
34
+ locale switch
52
35
{
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
+ } ;
58
40
59
- private static string ShortDateFormat ( string locale )
60
- {
61
- switch ( locale )
41
+ private static string ShortDateFormat ( string locale ) =>
42
+ locale switch
62
43
{
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
+ } ;
68
48
69
49
private static CultureInfo getCulture ( string currency , string locale )
70
50
{
@@ -75,15 +55,13 @@ private static CultureInfo getCulture(string currency, string locale)
75
55
return culture ;
76
56
}
77
57
78
- private static string FormatHeader ( CultureInfo culture )
79
- {
80
- switch ( culture . Name )
58
+ private static string FormatHeader ( CultureInfo culture ) =>
59
+ culture . Name switch
81
60
{
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
+ } ;
87
65
88
66
private static string FormatDate ( IFormatProvider culture , DateTime date ) => date . ToString ( "d" , culture ) ;
89
67
0 commit comments