add csharp version to the exercice

This commit is contained in:
Anis CHAABANI
2019-08-12 18:14:48 +02:00
parent 06fb74cfb3
commit 112854cc8a
10 changed files with 236 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
namespace TheatricalPlayersRefactoringKata
{
public class Invoice
{
private string _customer;
private List<Performance> _performances;
public string Customer { get => _customer; set => _customer = value; }
public List<Performance> Performances { get => _performances; set => _performances = value; }
public Invoice(string customer, List<Performance> performance)
{
this._customer = customer;
this._performances = performance;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Globalization;
namespace TheatricalPlayersRefactoringKata
{
public class StatementPrinter
{
public string Print(Invoice invoice, Dictionary<string, Play> 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;
}
}
}

View File

@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>