Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/practice/bank-account/BankAccountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public async Task Can_handle_concurrent_transactions()
account.Deposit(1m);
account.Withdraw(1m);
}
}));
}, TestContext.Current.CancellationToken));
await Task.WhenAll(tasks.ToArray());
}
Assert.Equal(0m, account.Balance);
Expand Down
86 changes: 32 additions & 54 deletions exercises/practice/ledger/.meta/Example.cs
Original file line number Diff line number Diff line change
@@ -1,70 +1,50 @@
using System.Globalization;

public class LedgerEntry
{
public LedgerEntry(DateTime date, string description, decimal change)
{
Date = date;
Description = description;
Change = change;
}

public DateTime Date { get; }
public string Description { get; }
public decimal Change { get; }
}
public record LedgerEntry(DateTime Date, string Description, decimal Change);

public static class Ledger
{
private const int TruncateLength = 25;
private const string TruncateSuffix = "...";

public static LedgerEntry CreateEntry(string date, string description, int change) =>
new LedgerEntry(ParseDate(date), description, ParseChange(change));
new(ParseDate(date), description, ParseChange(change));

private static DateTime ParseDate(string date) => DateTime.Parse(date, System.Globalization.CultureInfo.InvariantCulture);

private static decimal ParseChange(int change) => change / 100.0m;

private static CultureInfo CultureInfo(string locale)
{
switch (locale)
private static CultureInfo CultureInfo(string locale) =>
locale switch
{
case "en-US": return new CultureInfo("en-US", false);
case "nl-NL": return new CultureInfo("nl-NL", false);
default: throw new ArgumentException("Invalid locale");
}
}
"en-US" => new CultureInfo("en-US", false),
"nl-NL" => new CultureInfo("nl-NL", false),
_ => throw new ArgumentException("Invalid locale")
};

private static string CurrencySymbol(string currency)
{
switch (currency)
private static string CurrencySymbol(string currency) =>
currency switch
{
case "USD": return "$";
case "EUR": return "€";
default: throw new ArgumentException("Invalid currency");
}
}
"USD" => "$",
"EUR" => "€",
_ => throw new ArgumentException("Invalid currency")
};

private static int CurrencyNegativePattern(string locale)
{
switch (locale)
private static int CurrencyNegativePattern(string locale) =>
locale switch
{
case "en-US": return 0;
case "nl-NL": return 12;
default: throw new ArgumentException("Invalid locale");
}
}
"en-US" => 0,
"nl-NL" => 12,
_ => throw new ArgumentException("Invalid locale")
};

private static string ShortDateFormat(string locale)
{
switch (locale)
private static string ShortDateFormat(string locale) =>
locale switch
{
case "en-US": return "MM/dd/yyyy";
case "nl-NL": return "dd/MM/yyyy";
default: throw new ArgumentException("Invalid locale");
}
}
"en-US" => "MM/dd/yyyy",
"nl-NL" => "dd/MM/yyyy",
_ => throw new ArgumentException("Invalid locale")
};

private static CultureInfo getCulture(string currency, string locale)
{
Expand All @@ -75,15 +55,13 @@ private static CultureInfo getCulture(string currency, string locale)
return culture;
}

private static string FormatHeader(CultureInfo culture)
{
switch (culture.Name)
private static string FormatHeader(CultureInfo culture) =>
culture.Name switch
{
case "en-US": return "Date | Description | Change ";
case "nl-NL": return "Datum | Omschrijving | Verandering ";
default: throw new ArgumentException("Invalid locale");
}
}
"en-US" => "Date | Description | Change ",
"nl-NL" => "Datum | Omschrijving | Verandering ",
_ => throw new ArgumentException("Invalid locale")
};

private static string FormatDate(IFormatProvider culture, DateTime date) => date.ToString("d", culture);

Expand Down
8 changes: 4 additions & 4 deletions exercises/practice/ledger/Ledger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static LedgerEntry CreateEntry(string date, string desc, int chng)

private static CultureInfo CreateCulture(string cur, string loc)
{
string curSymb = null;
string? curSymb = null;
int curNeg = 0;
string datPat = null;
string? datPat = null;

if (cur != "USD" && cur != "EUR")
{
Expand Down Expand Up @@ -70,9 +70,9 @@ private static CultureInfo CreateCulture(string cur, string loc)
}

var culture = new CultureInfo(loc, false);
culture.NumberFormat.CurrencySymbol = curSymb;
culture.NumberFormat.CurrencySymbol = curSymb!;
culture.NumberFormat.CurrencyNegativePattern = curNeg;
culture.DateTimeFormat.ShortDatePattern = datPat;
culture.DateTimeFormat.ShortDatePattern = datPat!;
return culture;
}

Expand Down
1 change: 0 additions & 1 deletion exercises/practice/markdown/.meta/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public static class Markdown
private static string OpeningTag(string tag) => $"<{tag}>";
private static string ClosingTag(string tag) => $"</{tag}>";
private static string WrapInTag(this string text, string tag) => $"{OpeningTag(tag)}{text}{ClosingTag(tag)}";
private static bool StartsWithTag(this string text, string tag) => text.StartsWith(OpeningTag(tag));

private const string HeaderMarkdown = "#";
private const string BoldMarkdown = "__";
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/markdown/Markdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static string ParseText(string markdown, bool list)
}
}

private static string ParseHeader(string markdown, bool list, out bool inListAfter)
private static string? ParseHeader(string markdown, bool list, out bool inListAfter)
{
var count = 0;

Expand Down Expand Up @@ -68,7 +68,7 @@ private static string ParseHeader(string markdown, bool list, out bool inListAft
}
}

private static string ParseLineItem(string markdown, bool list, out bool inListAfter)
private static string? ParseLineItem(string markdown, bool list, out bool inListAfter)
{
if (markdown.StartsWith("*"))
{
Expand Down
Loading