mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-07-26 09:11:26 +00:00
Conjured items degrade twice as fast: -2 before sell-by, -4 after, never below 0. Registered in ItemUpdaterFactory.Default() and resolved by the Conjured prefix rule, so the whole Conjured family is covered. The four Conjured spec tests go green. The ThirtyDays approval snapshot now shows the intended Conjured-only diff; it is re-approved in the next commit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace GildedRoseKata;
|
|
|
|
public class ItemUpdaterFactory : IItemUpdaterFactory
|
|
{
|
|
public const string ConjuredPrefix = "Conjured";
|
|
private const string DefaultName = "default";
|
|
|
|
private readonly Dictionary<string, IItemUpdater> _updatersByName;
|
|
private readonly IItemUpdater _fallback;
|
|
|
|
public ItemUpdaterFactory(IEnumerable<IItemUpdater> updaters)
|
|
{
|
|
_updatersByName = new Dictionary<string, IItemUpdater>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (var updater in updaters)
|
|
{
|
|
_updatersByName[updater.Name] = updater;
|
|
}
|
|
|
|
if (!_updatersByName.TryGetValue(DefaultName, out _fallback))
|
|
{
|
|
throw new ArgumentException($"A \"{DefaultName}\" fallback updater is required.", nameof(updaters));
|
|
}
|
|
}
|
|
|
|
public static ItemUpdaterFactory Default() => new(new IItemUpdater[]
|
|
{
|
|
new NormalItemUpdater(),
|
|
new AgedBrieUpdater(),
|
|
new SulfurasUpdater(),
|
|
new BackstagePassUpdater(),
|
|
new ConjuredItemUpdater(),
|
|
});
|
|
|
|
public IItemUpdater Get(Item item)
|
|
{
|
|
if (_updatersByName.TryGetValue(item.Name, out var updater))
|
|
{
|
|
return updater;
|
|
}
|
|
|
|
if (item.Name.StartsWith(ConjuredPrefix, StringComparison.OrdinalIgnoreCase) &&
|
|
_updatersByName.TryGetValue(ConjuredPrefix, out var conjured))
|
|
{
|
|
return conjured;
|
|
}
|
|
|
|
return _fallback;
|
|
}
|
|
}
|