Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

teste/lucas-vilar-celestino #87

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

using System.Globalization;
using TheatricalPlayersRefactoringKata.Application.UseCases.Factories;
using TheatricalPlayersRefactoringKata.Domain.Entities;
using TheatricalPlayersRefactoringKata.Domain.Interfaces;

namespace TheatricalPlayersRefactoringKata.Application.Services.Printers
{
public class TextStatementPrinterService : IStatementPrinter
{
public string Print(Invoice invoice, Dictionary<string, Play> plays)
{
var totalAmount = 0m;
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 calculator = TheatricalCalculatorFactory.GetCalculator(play.Type);

var thisAmount = calculator.CalculateAmount(perf, play);
totalAmount += thisAmount;

volumeCredits += calculator.CalculateVolumeCredits(perf);

result += string.Format(cultureInfo, " {0}: {1:C} ({2} seats)\n", play.Name, Convert.ToDecimal(thisAmount / 100), perf.Audience);
}

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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Globalization;
using System.Text;
using TheatricalPlayersRefactoringKata.Application.UseCases.Factories;
using TheatricalPlayersRefactoringKata.Domain.Entities;
using TheatricalPlayersRefactoringKata.Domain.Interfaces;

namespace TheatricalPlayersRefactoringKata.Application.Services.Printers
{
public class XmlStatementPrinterService : IStatementPrinter
{
public string Print(Invoice invoice, Dictionary<string, Play> plays)
{
var totalAmount = 0m;
var totalVolumeCredits = 0;
var xml = new StringBuilder();

xml.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
xml.AppendLine("<Statement xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">");
xml.AppendLine($" <Customer>{invoice.Customer}</Customer>");
xml.AppendLine(" <Items>");

foreach (var perf in invoice.Performances)
{
var play = plays[perf.PlayId];
var calculator = TheatricalCalculatorFactory.GetCalculator(play.Type);

var thisAmount = calculator.CalculateAmount(perf, play);
var volumeCredits = calculator.CalculateVolumeCredits(perf);

totalAmount += thisAmount;
totalVolumeCredits += volumeCredits;

xml.AppendLine(" <Item>");
xml.AppendLine($" <AmountOwed>{(thisAmount / 100).ToString(new CultureInfo("en-US"))}</AmountOwed>");
xml.AppendLine($" <EarnedCredits>{volumeCredits}</EarnedCredits>");
xml.AppendLine($" <Seats>{perf.Audience}</Seats>");
xml.AppendLine(" </Item>");
}

xml.AppendLine(" </Items>");
xml.AppendLine($" <AmountOwed>{(totalAmount / 100).ToString(new CultureInfo("en-US"))}</AmountOwed>");
xml.AppendLine($" <EarnedCredits>{totalVolumeCredits}</EarnedCredits>");
xml.Append("</Statement>");

return xml.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\TheatricalPlayersRefactoringKata.Domain\TheatricalPlayersRefactoringKata.Domain.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using TheatricalPlayersRefactoringKata.Domain.Entities;
using TheatricalPlayersRefactoringKata.Domain.Interfaces;

namespace TheatricalPlayersRefactoringKata.Application.UseCases.Calculators
{
public class ComedyCalculator : ITheatricalCalculator
{
public decimal CalculateAmount(Performance perf, Play play)
{
var lines = play.Lines;
if (lines < 1000) lines = 1000;
if (lines > 4000) lines = 4000;
var thisAmount = lines * 10;

if (perf.Audience > 20)
{
thisAmount += 10000 + 500 * (perf.Audience - 20);
}
thisAmount += 300 * perf.Audience;
return thisAmount;
}

public int CalculateVolumeCredits(Performance perf)
{
int volumeCredits = Math.Max(perf.Audience - 30, 0);
volumeCredits += (int)Math.Floor((decimal)perf.Audience / 5); // Extra credits for comedy
return volumeCredits;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using TheatricalPlayersRefactoringKata.Domain.Entities;
using TheatricalPlayersRefactoringKata.Domain.Interfaces;

namespace TheatricalPlayersRefactoringKata.Application.UseCases.Calculators
{
public class HistoryCalculator : ITheatricalCalculator
{
private readonly TragedyCalculator _tragedyCalculator;
private readonly ComedyCalculator _comedyCalculator;

public HistoryCalculator()
{
_tragedyCalculator = new TragedyCalculator();
_comedyCalculator = new ComedyCalculator();
}

public decimal CalculateAmount(Performance perf, Play play)
{
return _tragedyCalculator.CalculateAmount(perf, play) + _comedyCalculator.CalculateAmount(perf, play);
}
public int CalculateVolumeCredits(Performance perf)
{
return Math.Max(perf.Audience - 30, 0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using TheatricalPlayersRefactoringKata.Domain.Entities;
using TheatricalPlayersRefactoringKata.Domain.Interfaces;

namespace TheatricalPlayersRefactoringKata.Application.UseCases.Calculators
{
public class TragedyCalculator : ITheatricalCalculator
{
public decimal CalculateAmount(Performance perf, Play play)
{
var lines = play.Lines;
if (lines < 1000) lines = 1000;
if (lines > 4000) lines = 4000;
var thisAmount = lines * 10;

if (perf.Audience > 30)
{
thisAmount += 1000 * (perf.Audience - 30);
}
return thisAmount;
}

public int CalculateVolumeCredits(Performance perf)
{
return Math.Max(perf.Audience - 30, 0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using TheatricalPlayersRefactoringKata.Application.Services.Printers;
using TheatricalPlayersRefactoringKata.Domain.Interfaces;

namespace TheatricalPlayersRefactoringKata.Application.UseCases.Factories
{
public static class StatementPrinterFactory
{
public static IStatementPrinter GetPrinter(string format)
{
switch (format.ToLower())
{
case "text":
return new TextStatementPrinterService();
case "xml":
return new XmlStatementPrinterService();
// Adicione mais casos conforme necessário para novos formatos
default:
throw new ArgumentException("Invalid format", nameof(format));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using TheatricalPlayersRefactoringKata.Application.UseCases.Calculators;
using TheatricalPlayersRefactoringKata.Domain.Interfaces;

namespace TheatricalPlayersRefactoringKata.Application.UseCases.Factories
{
public class TheatricalCalculatorFactory
{
public static ITheatricalCalculator GetCalculator(string genreType)
{
return genreType switch
{
"tragedy" => new TragedyCalculator(),
"comedy" => new ComedyCalculator(),
"history" => new HistoryCalculator(),
_ => throw new Exception("unknown genre: " + genreType),
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;

namespace TheatricalPlayersRefactoringKata;

namespace TheatricalPlayersRefactoringKata.Domain.Entities;
public class Invoice
{
private string _customer;
Expand All @@ -12,8 +9,8 @@ public class Invoice

public Invoice(string customer, List<Performance> performance)
{
this._customer = customer;
this._performances = performance;
_customer = customer;
_performances = performance;
}

}
13 changes: 13 additions & 0 deletions TheatricalPlayersRefactoringKata.Domain/Entities/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace TheatricalPlayersRefactoringKata.Domain.Entities

{
public class Item
{
public int Id { get; set; }
public decimal AmountOwed { get; set; }
public int EarnedCredits { get; set; }
public int Seats { get; set; }
public int? StatementId { get; set; }
public Statement? Statement { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TheatricalPlayersRefactoringKata;
namespace TheatricalPlayersRefactoringKata.Domain.Entities;

public class Performance
{
Expand All @@ -10,8 +10,8 @@ public class Performance

public Performance(string playID, int audience)
{
this._playId = playID;
this._audience = audience;
_playId = playID;
_audience = audience;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TheatricalPlayersRefactoringKata;
namespace TheatricalPlayersRefactoringKata.Domain.Entities;

public class Play
{
Expand All @@ -10,9 +10,10 @@ public class Play
public int Lines { get => _lines; set => _lines = value; }
public string Type { get => _type; set => _type = value; }

public Play(string name, int lines, string type) {
this._name = name;
this._lines = lines;
this._type = type;
public Play(string name, int lines, string type)
{
_name = name;
_lines = lines;
_type = type;
}
}
11 changes: 11 additions & 0 deletions TheatricalPlayersRefactoringKata.Domain/Entities/Statement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace TheatricalPlayersRefactoringKata.Domain.Entities
{
public class Statement
{
public int Id { get; set; }
public required string Customer { get; set; }
public required List<Item> Items { get; set; }
public decimal TotalAmountOwed { get; set; }
public int TotalEarnedCredits { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TheatricalPlayersRefactoringKata.Domain.Entities;

namespace TheatricalPlayersRefactoringKata.Domain.Interfaces
{
public interface IStatementPrinter
{
string Print(Invoice invoice, Dictionary<string, Play> plays);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using TheatricalPlayersRefactoringKata.Domain.Entities;

namespace TheatricalPlayersRefactoringKata.Domain.Interfaces
{
public interface ITheatricalCalculator
{
decimal CalculateAmount(Performance perf, Play play);
int CalculateVolumeCredits(Performance perf);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
using TheatricalPlayersRefactoringKata.Domain.Entities;

namespace TheatricalPlayersRefactoringKata.Infrastructure.Data
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{ }

public DbSet<Statement> Statements { get; set; }
public DbSet<Item> Items { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=theatrical.db");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="8.0.8" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TheatricalPlayersRefactoringKata.Domain\TheatricalPlayersRefactoringKata.Domain.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="DbContext.tt">
<Generator>TextTemplatingFileGenerator</Generator>
</None>
</ItemGroup>

<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>

</Project>
Loading