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>
/// Base Exception class for the Lottery app
/// </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()
{
Numbers Numbers = new(KindOfLottery.Uk, []);
Numbers Numbers = new([]);
Generator.FillNumbers(Numbers.Normal, Limits);
return Numbers;
}
@ -60,7 +60,7 @@
public override Numbers Generate()
{
NumbersWithSpecial Numbers = new(KindOfLottery.Euro, [], []);
NumbersWithSpecial Numbers = new([], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits);
return Numbers;
}
@ -77,7 +77,7 @@
public override Numbers Generate()
{
NumbersWithSpecial Numbers = new(KindOfLottery.SetForLife, [], []);
NumbersWithSpecial Numbers = new([], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits);
return Numbers;
}
@ -94,7 +94,7 @@
public override Numbers Generate()
{
NumbersWithSpecial Numbers = new(KindOfLottery.Thunderball, [], []);
NumbersWithSpecial Numbers = new([], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits);
return Numbers;
}

View File

@ -1,31 +1,54 @@
namespace Lottery
{
/// <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.
/// </summary>
/// <param name="Generator"></param>
/// <param name="Generator">A fully formed Generator</param>
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) { };
internal class EuroMillionsLottery(IGenerator Generator) : Lottery(Generator)
/// <summary>
/// Abstract base class, for lotteries with special values.
/// 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
{
readonly List<Lottery> Lotteries = [
new UKLottoLottery(new UKLottoGenerator()),
new EuroMillionsLottery(new EuroMillionsGenerator()),
new SetForLifeLottery(new SetForLifeGenerator()),
new ThunderballLottery(new ThunderBallGenerator())
new Lottery(new UKLottoGenerator()),
new EuroMillionsLotteryWithSpecial(new EuroMillionsGenerator()),
new SetForLifeLotteryWithSpecial(new SetForLifeGenerator()),
new ThunderballLotteryWithSpecial(new ThunderBallGenerator())
];
const KindOfLottery DefaultLottery = KindOfLottery.Uk;
Lottery Lottery;
@ -24,28 +24,7 @@
private void SpinButton_Clicked(object sender, EventArgs e)
{
Numbers numbers = Lottery.GenerateNumbers();
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);
NumbersLabel.Text = string.Join("\t", Lottery.Output());
SemanticScreenReader.Announce(NumbersLabel.Text);
}

View File

@ -1,6 +1,6 @@
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);
}