From 112854cc8a12c8e64af73ee3610236ce75e27d5e Mon Sep 17 00:00:00 2001 From: Anis CHAABANI Date: Mon, 12 Aug 2019 18:14:48 +0200 Subject: [PATCH] add csharp version to the exercice --- csharp/.gitignore | 5 ++ .../StatementPrinterTests.cs | 44 ++++++++++++++++ ...rTests.test_statement_exemple.approved.txt | 6 +++ ...atricalPlayersRefactoringKata.Tests.csproj | 20 +++++++ csharp/TheatricalPlayersRefactoringKata.sln | 48 +++++++++++++++++ .../Invoice.cs | 20 +++++++ .../Performance.cs | 18 +++++++ .../TheatricalPlayersRefactoringKata/Play.cs | 16 ++++++ .../StatementPrinter.cs | 52 +++++++++++++++++++ .../TheatricalPlayersRefactoringKata.csproj | 7 +++ 10 files changed, 236 insertions(+) create mode 100644 csharp/.gitignore create mode 100644 csharp/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs create mode 100644 csharp/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.test_statement_exemple.approved.txt create mode 100644 csharp/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj create mode 100644 csharp/TheatricalPlayersRefactoringKata.sln create mode 100644 csharp/TheatricalPlayersRefactoringKata/Invoice.cs create mode 100644 csharp/TheatricalPlayersRefactoringKata/Performance.cs create mode 100644 csharp/TheatricalPlayersRefactoringKata/Play.cs create mode 100644 csharp/TheatricalPlayersRefactoringKata/StatementPrinter.cs create mode 100644 csharp/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj diff --git a/csharp/.gitignore b/csharp/.gitignore new file mode 100644 index 0000000..40d62f2 --- /dev/null +++ b/csharp/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +bin/ +obj/ +.vs/ +.vscode/ \ No newline at end of file diff --git a/csharp/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs b/csharp/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs new file mode 100644 index 0000000..1dbe569 --- /dev/null +++ b/csharp/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using ApprovalTests; +using ApprovalTests.Reporters; +using Xunit; + +namespace TheatricalPlayersRefactoringKata.Tests +{ + public class StatementPrinterTests + { + [Fact] + [UseReporter(typeof(DiffReporter))] + public void test_statement_exemple() + { + var plays = new Dictionary(); + plays.Add("hamlet", new Play("Hamlet", "tragedy")); + plays.Add("as-like", new Play("As You Like It", "comedy")); + plays.Add("othello", new Play("Othello", "tragedy")); + + Invoice invoice = new Invoice("BigCo", new List{new Performance("hamlet", 55), + new Performance("as-like", 35), + new Performance("othello", 40)}); + + StatementPrinter statementPrinter = new StatementPrinter(); + var result = statementPrinter.Print(invoice, plays); + + Approvals.Verify(result); + } + [Fact] + public void test_statement_with_new_play_types() + { + var plays = new Dictionary(); + plays.Add("henry-v", new Play("Henry V", "history")); + plays.Add("as-like", new Play("As You Like It", "pastoral")); + + Invoice invoice = new Invoice("BigCoII", new List{new Performance("henry-v", 53), + new Performance("as-like", 55)}); + + StatementPrinter statementPrinter = new StatementPrinter(); + + Assert.Throws(() => statementPrinter.Print(invoice, plays)); + } + } +} diff --git a/csharp/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.test_statement_exemple.approved.txt b/csharp/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.test_statement_exemple.approved.txt new file mode 100644 index 0000000..d0b9143 --- /dev/null +++ b/csharp/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.test_statement_exemple.approved.txt @@ -0,0 +1,6 @@ +Statement for BigCo + Hamlet: $650.00 (55 seats) + As You Like It: $580.00 (35 seats) + Othello: $500.00 (40 seats) +Amount owed is $1,730.00 +You earned 47 credits diff --git a/csharp/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj b/csharp/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj new file mode 100644 index 0000000..8ee87f1 --- /dev/null +++ b/csharp/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp2.2 + + false + + + + + + + + + + + + + + diff --git a/csharp/TheatricalPlayersRefactoringKata.sln b/csharp/TheatricalPlayersRefactoringKata.sln new file mode 100644 index 0000000..58d13ac --- /dev/null +++ b/csharp/TheatricalPlayersRefactoringKata.sln @@ -0,0 +1,48 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata", "TheatricalPlayersRefactoringKata\TheatricalPlayersRefactoringKata.csproj", "{B380BB58-50A5-435D-9B52-5045A2F93761}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata.Tests", "TheatricalPlayersRefactoringKata.Tests\TheatricalPlayersRefactoringKata.Tests.csproj", "{47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B380BB58-50A5-435D-9B52-5045A2F93761}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Debug|x64.ActiveCfg = Debug|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Debug|x64.Build.0 = Debug|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Debug|x86.ActiveCfg = Debug|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Debug|x86.Build.0 = Debug|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Release|Any CPU.Build.0 = Release|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Release|x64.ActiveCfg = Release|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Release|x64.Build.0 = Release|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Release|x86.ActiveCfg = Release|Any CPU + {B380BB58-50A5-435D-9B52-5045A2F93761}.Release|x86.Build.0 = Release|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Debug|x64.ActiveCfg = Debug|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Debug|x64.Build.0 = Debug|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Debug|x86.ActiveCfg = Debug|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Debug|x86.Build.0 = Debug|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Release|Any CPU.Build.0 = Release|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Release|x64.ActiveCfg = Release|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Release|x64.Build.0 = Release|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Release|x86.ActiveCfg = Release|Any CPU + {47FAD5CE-0FB3-4C9F-A430-68E58A1B094F}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/csharp/TheatricalPlayersRefactoringKata/Invoice.cs b/csharp/TheatricalPlayersRefactoringKata/Invoice.cs new file mode 100644 index 0000000..b8a512b --- /dev/null +++ b/csharp/TheatricalPlayersRefactoringKata/Invoice.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; + +namespace TheatricalPlayersRefactoringKata +{ + public class Invoice + { + private string _customer; + private List _performances; + + public string Customer { get => _customer; set => _customer = value; } + public List Performances { get => _performances; set => _performances = value; } + + public Invoice(string customer, List performance) + { + this._customer = customer; + this._performances = performance; + } + + } +} diff --git a/csharp/TheatricalPlayersRefactoringKata/Performance.cs b/csharp/TheatricalPlayersRefactoringKata/Performance.cs new file mode 100644 index 0000000..6e190fc --- /dev/null +++ b/csharp/TheatricalPlayersRefactoringKata/Performance.cs @@ -0,0 +1,18 @@ +namespace TheatricalPlayersRefactoringKata +{ + public class Performance + { + private string _playID; + private int _audience; + + public string PlayID { get => _playID; set => _playID = value; } + public int Audience { get => _audience; set => _audience = value; } + + public Performance(string playID, int audience) + { + this._playID = playID; + this._audience = audience; + } + + } +} diff --git a/csharp/TheatricalPlayersRefactoringKata/Play.cs b/csharp/TheatricalPlayersRefactoringKata/Play.cs new file mode 100644 index 0000000..f52dc66 --- /dev/null +++ b/csharp/TheatricalPlayersRefactoringKata/Play.cs @@ -0,0 +1,16 @@ +namespace TheatricalPlayersRefactoringKata +{ + public class Play + { + private string _name; + private string _type; + + public string Name { get => _name; set => _name = value; } + public string Type { get => _type; set => _type = value; } + + public Play(string name, string type) { + this._name = name; + this._type = type; + } + } +} diff --git a/csharp/TheatricalPlayersRefactoringKata/StatementPrinter.cs b/csharp/TheatricalPlayersRefactoringKata/StatementPrinter.cs new file mode 100644 index 0000000..5dc980f --- /dev/null +++ b/csharp/TheatricalPlayersRefactoringKata/StatementPrinter.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace TheatricalPlayersRefactoringKata +{ + public class StatementPrinter + { + public string Print(Invoice invoice, Dictionary plays) + { + var totalAmount = 0; + var volumeCredits = 0; + var result = string.Format("Statement for {0}\n", invoice.Customer); + CultureInfo cultureInfo = new CultureInfo("en-US"); + + foreach(var perf in invoice.Performances) + { + var play = plays[perf.PlayID]; + var thisAmount = 0; + switch (play.Type) + { + case "tragedy": + thisAmount = 40000; + if (perf.Audience > 30) { + thisAmount += 1000 * (perf.Audience - 30); + } + break; + case "comedy": + thisAmount = 30000; + if (perf.Audience > 20) { + thisAmount += 10000 + 500 * (perf.Audience - 20); + } + thisAmount += 300 * perf.Audience; + break; + default: + throw new Exception("unknown type: " + play.Type); + } + // add volume credits + volumeCredits += Math.Max(perf.Audience - 30, 0); + // add extra credit for every ten comedy attendees + if ("comedy" == play.Type) volumeCredits += (int)Math.Floor((decimal)perf.Audience / 5); + + // print line for this order + result += String.Format(cultureInfo, " {0}: {1:C} ({2} seats)\n", play.Name, Convert.ToDecimal(thisAmount / 100), perf.Audience); + totalAmount += thisAmount; + } + result += String.Format(cultureInfo, "Amount owed is {0:C}\n", Convert.ToDecimal(totalAmount / 100)); + result += String.Format("You earned {0} credits\n", volumeCredits); + return result; + } + } +} diff --git a/csharp/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj b/csharp/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj new file mode 100644 index 0000000..72764a6 --- /dev/null +++ b/csharp/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + +