get our ducks in order!

generating Output() is now the concern of each Lottery class.
This commit is contained in:
onyx-and-iris 2024-09-27 14:30:12 +01:00
parent 34b99c1f52
commit 7b6e3f6419
5 changed files with 47 additions and 47 deletions

View File

@ -3,7 +3,5 @@
/// <summary> /// <summary>
/// Base Exception class for the Lottery app /// Base Exception class for the Lottery app
/// </summary> /// </summary>
internal class LotteryException(string message) : Exception(message) internal class LotteryException(string message) : Exception(message) { }
{
}
} }

View File

@ -43,7 +43,7 @@
public override Numbers Generate() public override Numbers Generate()
{ {
Numbers Numbers = new(KindOfLottery.Uk, []); Numbers Numbers = new([]);
Generator.FillNumbers(Numbers.Normal, Limits); Generator.FillNumbers(Numbers.Normal, Limits);
return Numbers; return Numbers;
} }
@ -60,7 +60,7 @@
public override Numbers Generate() public override Numbers Generate()
{ {
NumbersWithSpecial Numbers = new(KindOfLottery.Euro, [], []); NumbersWithSpecial Numbers = new([], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits); Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits);
return Numbers; return Numbers;
} }
@ -77,7 +77,7 @@
public override Numbers Generate() public override Numbers Generate()
{ {
NumbersWithSpecial Numbers = new(KindOfLottery.SetForLife, [], []); NumbersWithSpecial Numbers = new([], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits); Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits);
return Numbers; return Numbers;
} }
@ -94,7 +94,7 @@
public override Numbers Generate() public override Numbers Generate()
{ {
NumbersWithSpecial Numbers = new(KindOfLottery.Thunderball, [], []); NumbersWithSpecial Numbers = new([], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits); Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits);
return Numbers; return Numbers;
} }

View File

@ -1,31 +1,54 @@
namespace Lottery namespace Lottery
{ {
/// <summary> /// <summary>
/// Base Lottery class, it accepts a Generator /// Base Lottery class, use it for lotteries that generate a single set of numbers.
/// SpecialIdentifier may be overridden for Lotteries with Special values. /// SpecialIdentifier may be overridden for Lotteries with Special values.
/// </summary> /// </summary>
/// <param name="Generator"></param> /// <param name="Generator">A fully formed Generator</param>
internal class Lottery(IGenerator Generator) internal class Lottery(IGenerator Generator)
{ {
public virtual string? SpecialIdentifier { get; } protected Numbers GenerateNumbers() => Generator.Generate();
public Numbers GenerateNumbers() => Generator.Generate(); public virtual string Output()
{
var numbers = GenerateNumbers();
return $"Numbers: {string.Join(", ", numbers.Normal)}";
}
} }
internal class UKLottoLottery(IGenerator Generator) : Lottery(Generator) { }; /// <summary>
/// Abstract base class, for lotteries with special values.
internal class EuroMillionsLottery(IGenerator Generator) : Lottery(Generator) /// It subclasses Lottery.
/// </summary>
/// <param name="Generator">A fully formed Generator</param>
internal class LotteryWithSpecial(IGenerator Generator) : Lottery(Generator)
{ {
public override string SpecialIdentifier { get; } = "Lucky Stars"; protected virtual string SpecialIdentifier => throw new NotImplementedException();
public override string Output()
{
var numbers = GenerateNumbers() as NumbersWithSpecial
?? throw new LotteryException($"Unable to generate numbers for {this}");
return string.Join("\t", [
$"Numbers: {string.Join(", ", numbers.Normal)}",
$"{SpecialIdentifier}: {string.Join(", ", numbers.Special)}",
]);
}
} }
internal class SetForLifeLottery(IGenerator Generator) : Lottery(Generator) internal class EuroMillionsLotteryWithSpecial(IGenerator Generator) : LotteryWithSpecial(Generator)
{ {
public override string SpecialIdentifier { get; } = "Life Ball"; protected override string SpecialIdentifier => "Lucky Stars";
} }
internal class ThunderballLottery(IGenerator Generator) : Lottery(Generator) internal class SetForLifeLotteryWithSpecial(IGenerator Generator) : LotteryWithSpecial(Generator)
{ {
public override string SpecialIdentifier { get; } = "Thunderball"; protected override string SpecialIdentifier => "Life Ball";
}
internal class ThunderballLotteryWithSpecial(IGenerator Generator) : LotteryWithSpecial(Generator)
{
protected override string SpecialIdentifier => "Thunderball";
} }
} }

View File

@ -3,10 +3,10 @@
public partial class MainPage : ContentPage public partial class MainPage : ContentPage
{ {
readonly List<Lottery> Lotteries = [ readonly List<Lottery> Lotteries = [
new UKLottoLottery(new UKLottoGenerator()), new Lottery(new UKLottoGenerator()),
new EuroMillionsLottery(new EuroMillionsGenerator()), new EuroMillionsLotteryWithSpecial(new EuroMillionsGenerator()),
new SetForLifeLottery(new SetForLifeGenerator()), new SetForLifeLotteryWithSpecial(new SetForLifeGenerator()),
new ThunderballLottery(new ThunderBallGenerator()) new ThunderballLotteryWithSpecial(new ThunderBallGenerator())
]; ];
const KindOfLottery DefaultLottery = KindOfLottery.Uk; const KindOfLottery DefaultLottery = KindOfLottery.Uk;
Lottery Lottery; Lottery Lottery;
@ -24,28 +24,7 @@
private void SpinButton_Clicked(object sender, EventArgs e) private void SpinButton_Clicked(object sender, EventArgs e)
{ {
Numbers numbers = Lottery.GenerateNumbers(); NumbersLabel.Text = string.Join("\t", Lottery.Output());
List<string> output = [];
switch (numbers.Kind)
{
case KindOfLottery.Uk:
output.Add($"Numbers: {string.Join(", ", numbers.Normal)}");
break;
case KindOfLottery.Euro:
case KindOfLottery.SetForLife:
case KindOfLottery.Thunderball:
if (numbers is NumbersWithSpecial numbersWithSpecial)
{
output.AddRange([
$"Numbers: {string.Join(", ", numbersWithSpecial.Normal)}",
$"{Lottery.SpecialIdentifier}: {string.Join(", ", numbersWithSpecial.Special)}"
]);
}
break;
default:
throw new LotteryException($"no NumbersLabel output defined for {numbers.Kind}");
};
NumbersLabel.Text = string.Join("\t", output);
SemanticScreenReader.Announce(NumbersLabel.Text); SemanticScreenReader.Announce(NumbersLabel.Text);
} }

View File

@ -1,6 +1,6 @@
namespace Lottery namespace Lottery
{ {
internal record Numbers(KindOfLottery Kind, List<int> Normal); internal record Numbers(List<int> Normal);
internal record NumbersWithSpecial(KindOfLottery Kind, List<int> Normal, List<int> Special) : Numbers(Kind, Normal); internal record NumbersWithSpecial(List<int> Normal, List<int> Special) : Numbers(Normal);
} }