mirror of
https://github.com/ysoftdevs/Theatrical-Players-Refactoring-Kata.git
synced 2026-05-07 00:14:17 +02:00
Merge pull request #1 from ysoftdevs/init_changes
java and .net kata preparation
This commit is contained in:
@@ -2,6 +2,7 @@ Theatrical Players Refactoring Kata
|
||||
====================================
|
||||
|
||||
The first chapter of ['Refactoring' by Martin Fowler, 2nd Edition](https://www.thoughtworks.com/books/refactoring2) contains a worked example of this exercise, in javascript. That chapter is available to download for free. This repo contains the starting point for this exercise in several languages, with tests, so you can try it out for yourself.
|
||||
This repo was forked from https://github.com/emilybache/Theatrical-Players-Refactoring-Kata
|
||||
|
||||
What you need to change
|
||||
-----------------------
|
||||
@@ -13,4 +14,5 @@ In his book Fowler mentions that the first step in refactoring is always the sam
|
||||
|
||||
Acknowledgements
|
||||
----------------
|
||||
Thankyou to Martin Fowler for kindly giving permission to use his code.
|
||||
Thank you to Martin Fowler for kindly giving permission to use his code.
|
||||
Thank you to Emily Bache for creation of this kata.
|
||||
|
||||
@@ -2,15 +2,16 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using ApprovalTests;
|
||||
using ApprovalTests.Reporters;
|
||||
using Xunit;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace TheatricalPlayersRefactoringKata.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class StatementPrinterTests
|
||||
{
|
||||
[Fact]
|
||||
[Test]
|
||||
[UseReporter(typeof(DiffReporter))]
|
||||
public void test_statement_example()
|
||||
public void test_statement_plain_text_example()
|
||||
{
|
||||
var plays = new Dictionary<string, Play>();
|
||||
plays.Add("hamlet", new Play("Hamlet", "tragedy"));
|
||||
@@ -23,10 +24,32 @@ namespace TheatricalPlayersRefactoringKata.Tests
|
||||
|
||||
StatementPrinter statementPrinter = new StatementPrinter();
|
||||
var result = statementPrinter.Print(invoice, plays);
|
||||
|
||||
|
||||
Approvals.Verify(result);
|
||||
}
|
||||
[Fact]
|
||||
|
||||
[Test]
|
||||
[UseReporter(typeof(DiffReporter))]
|
||||
public void test_statement_html_example()
|
||||
{
|
||||
var plays = new Dictionary<string, Play>();
|
||||
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<Performance>{new Performance("hamlet", 55),
|
||||
new Performance("as-like", 35),
|
||||
new Performance("othello", 40)});
|
||||
|
||||
StatementPrinter statementPrinter = new StatementPrinter();
|
||||
|
||||
// Not implemented yet
|
||||
// var result = statementPrinter.PrintAsHtml(invoice, plays);
|
||||
// Approvals.Verify(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[UseReporter(typeof(DiffReporter))]
|
||||
public void test_statement_with_new_play_types()
|
||||
{
|
||||
var plays = new Dictionary<string, Play>();
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<h1>Statement for BigCo</h1>
|
||||
<table>
|
||||
<tr><th>play</th><th>seats</th><th>cost</th></tr>
|
||||
<tr><td>Hamlet</td><td>55</td><td>$650.00</td></tr>
|
||||
<tr><td>As You Like It</td><td>35</td><td>$580.00</td></tr>
|
||||
<tr><td>Othello</td><td>40</td><td>$500.00</td></tr>
|
||||
</table>
|
||||
<p>Amount owed is <em>$1,730.00</em></p>
|
||||
<p>You earned <em>47</em> credits</p>
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
Statement for BigCo
|
||||
Henry V: $100.00 (53 seats)
|
||||
As You Like It: $100.00 (55 seats)
|
||||
Amount owed is $200.00
|
||||
You earned 48 credits
|
||||
@@ -1,20 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ApprovalTests" Version="4.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="FakeItEasy" Version="6.2.1" />
|
||||
<PackageReference Include="FluentAssertions" Version="5.10.3" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
|
||||
<PackageReference Include="NBuilder" Version="6.1.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="NUnit" Version="3.13.1" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TheatricalPlayersRefactoringKata\TheatricalPlayersRefactoringKata.csproj" />
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TheatricalPlayersRefactoringKata\TheatricalPlayersRefactoringKata.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=95df3736_002D330d_002D4608_002D8d90_002D535fe8f6aaa2/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" IsActive="True" Name="StatementPrinterTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||
<TestAncestor>
|
||||
<TestId>NUnit3x::47FAD5CE-0FB3-4C9F-A430-68E58A1B094F::net5.0::TheatricalPlayersRefactoringKata.Tests.StatementPrinterTests</TestId>
|
||||
</TestAncestor>
|
||||
</SessionState></s:String></wpf:ResourceDictionary>
|
||||
@@ -1,7 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
5
csharp/global.json
Normal file
5
csharp/global.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"sdk": {
|
||||
"version": "5.0.103"
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,19 @@ import java.util.List;
|
||||
|
||||
public class Invoice {
|
||||
|
||||
public String customer;
|
||||
public List<Performance> performances;
|
||||
private String customer;
|
||||
private List<Performance> performances;
|
||||
|
||||
public Invoice(String customer, List<Performance> performances) {
|
||||
this.customer = customer;
|
||||
this.performances = performances;
|
||||
}
|
||||
|
||||
public String getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
|
||||
public List<Performance> getPerformances() {
|
||||
return performances;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
public class Performance {
|
||||
|
||||
public String playID;
|
||||
public int audience;
|
||||
private String playID;
|
||||
private int audience;
|
||||
|
||||
public Performance(String playID, int audience) {
|
||||
this.playID = playID;
|
||||
this.audience = audience;
|
||||
}
|
||||
|
||||
public String getPlayID() {
|
||||
return playID;
|
||||
}
|
||||
|
||||
public int getAudience() {
|
||||
return audience;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
public class Play {
|
||||
|
||||
public String name;
|
||||
public String type;
|
||||
private String name;
|
||||
private String type;
|
||||
|
||||
public Play(String name, String type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,39 +7,39 @@ public class StatementPrinter {
|
||||
public String print(Invoice invoice, Map<String, Play> plays) {
|
||||
var totalAmount = 0;
|
||||
var volumeCredits = 0;
|
||||
var result = String.format("Statement for %s\n", invoice.customer);
|
||||
var result = String.format("Statement for %s\n", invoice.getCustomer());
|
||||
|
||||
NumberFormat frmt = NumberFormat.getCurrencyInstance(Locale.US);
|
||||
|
||||
for (var perf : invoice.performances) {
|
||||
var play = plays.get(perf.playID);
|
||||
for (var perf : invoice.getPerformances()) {
|
||||
var play = plays.get(perf.getPlayID());
|
||||
var thisAmount = 0;
|
||||
|
||||
switch (play.type) {
|
||||
switch (play.getType()) {
|
||||
case "tragedy":
|
||||
thisAmount = 40000;
|
||||
if (perf.audience > 30) {
|
||||
thisAmount += 1000 * (perf.audience - 30);
|
||||
if (perf.getAudience() > 30) {
|
||||
thisAmount += 1000 * (perf.getAudience() - 30);
|
||||
}
|
||||
break;
|
||||
case "comedy":
|
||||
thisAmount = 30000;
|
||||
if (perf.audience > 20) {
|
||||
thisAmount += 10000 + 500 * (perf.audience - 20);
|
||||
if (perf.getAudience() > 20) {
|
||||
thisAmount += 10000 + 500 * (perf.getAudience() - 20);
|
||||
}
|
||||
thisAmount += 300 * perf.audience;
|
||||
thisAmount += 300 * perf.getAudience();
|
||||
break;
|
||||
default:
|
||||
throw new Error("unknown type: ${play.type}");
|
||||
}
|
||||
|
||||
// add volume credits
|
||||
volumeCredits += Math.max(perf.audience - 30, 0);
|
||||
volumeCredits += Math.max(perf.getAudience() - 30, 0);
|
||||
// add extra credit for every ten comedy attendees
|
||||
if ("comedy".equals(play.type)) volumeCredits += Math.floor(perf.audience / 5);
|
||||
if ("comedy".equals(play.getType())) volumeCredits += Math.floor(perf.getAudience() / 5);
|
||||
|
||||
// print line for this order
|
||||
result += String.format(" %s: %s (%s seats)\n", play.name, frmt.format(thisAmount / 100), perf.audience);
|
||||
result += String.format(" %s: %s (%s seats)\n", play.getName(), frmt.format(thisAmount / 100), perf.getAudience());
|
||||
totalAmount += thisAmount;
|
||||
}
|
||||
result += String.format("Amount owed is %s\n", frmt.format(totalAmount / 100));
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<h1>Statement for BigCo</h1>
|
||||
<table>
|
||||
<tr><th>play</th><th>seats</th><th>cost</th></tr>
|
||||
<tr><td>Hamlet</td><td>55</td><td>$650.00</td></tr>
|
||||
<tr><td>As You Like It</td><td>35</td><td>$580.00</td></tr>
|
||||
<tr><td>Othello</td><td>40</td><td>$500.00</td></tr>
|
||||
</table>
|
||||
<p>Amount owed is <em>$1,730.00</em></p>
|
||||
<p>You earned <em>47</em> credits</p>
|
||||
</html>
|
||||
@@ -9,7 +9,7 @@ import static org.approvaltests.Approvals.verify;
|
||||
public class StatementPrinterTests {
|
||||
|
||||
@Test
|
||||
void exampleStatement() {
|
||||
void exampleStatementPlainText() {
|
||||
Map<String, Play> plays = Map.of(
|
||||
"hamlet", new Play("Hamlet", "tragedy"),
|
||||
"as-like", new Play("As You Like It", "comedy"),
|
||||
@@ -26,6 +26,25 @@ public class StatementPrinterTests {
|
||||
verify(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void exampleStatementHtml() {
|
||||
Map<String, Play> plays = Map.of(
|
||||
"hamlet", new Play("Hamlet", "tragedy"),
|
||||
"as-like", new Play("As You Like It", "comedy"),
|
||||
"othello", new Play("Othello", "tragedy"));
|
||||
|
||||
Invoice invoice = new Invoice("BigCo", List.of(
|
||||
new Performance("hamlet", 55),
|
||||
new Performance("as-like", 35),
|
||||
new Performance("othello", 40)));
|
||||
|
||||
StatementPrinter statementPrinter = new StatementPrinter();
|
||||
|
||||
// Not implemented yet
|
||||
// var result = statementPrinter.printAsHtml(invoice, plays);
|
||||
// verify(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void statementWithNewPlayTypes() {
|
||||
Map<String, Play> plays = Map.of(
|
||||
@@ -37,6 +56,8 @@ public class StatementPrinterTests {
|
||||
new Performance("as-like", 55)));
|
||||
|
||||
StatementPrinter statementPrinter = new StatementPrinter();
|
||||
|
||||
// Not implemented yet
|
||||
Assertions.assertThrows(Error.class, () -> {
|
||||
statementPrinter.print(invoice, plays);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
Statement for BigCo
|
||||
Henry V: $100.00 (53 seats)
|
||||
As You Like It: $100.00 (55 seats)
|
||||
Amount owed is $200.00
|
||||
You earned 48 credits
|
||||
Reference in New Issue
Block a user