diff --git a/Lottery/Exception.cs b/Lottery/Exception.cs index 26e61ba..201269c 100644 --- a/Lottery/Exception.cs +++ b/Lottery/Exception.cs @@ -3,7 +3,5 @@ /// /// Base Exception class for the Lottery app /// - internal class LotteryException(string message) : Exception(message) - { - } + internal class LotteryException(string message) : Exception(message) { } } diff --git a/Lottery/Generator.cs b/Lottery/Generator.cs index 25c4fcf..f27f7b6 100644 --- a/Lottery/Generator.cs +++ b/Lottery/Generator.cs @@ -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; } diff --git a/Lottery/Lottery.cs b/Lottery/Lottery.cs index e7318b1..8168c05 100644 --- a/Lottery/Lottery.cs +++ b/Lottery/Lottery.cs @@ -1,31 +1,54 @@ namespace Lottery { /// - /// 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. /// - /// + /// A fully formed 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) { }; - - internal class EuroMillionsLottery(IGenerator Generator) : Lottery(Generator) + /// + /// Abstract base class, for lotteries with special values. + /// It subclasses Lottery. + /// + /// A fully formed Generator + 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"; } } diff --git a/Lottery/MainPage.xaml.cs b/Lottery/MainPage.xaml.cs index ffacbeb..1b64ab0 100644 --- a/Lottery/MainPage.xaml.cs +++ b/Lottery/MainPage.xaml.cs @@ -3,10 +3,10 @@ public partial class MainPage : ContentPage { readonly List 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 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); } diff --git a/Lottery/Numbers.cs b/Lottery/Numbers.cs index e9b224e..f6ca9b4 100644 --- a/Lottery/Numbers.cs +++ b/Lottery/Numbers.cs @@ -1,6 +1,6 @@ namespace Lottery { - internal record Numbers(KindOfLottery Kind, List Normal); + internal record Numbers(List Normal); - internal record NumbersWithSpecial(KindOfLottery Kind, List Normal, List Special) : Numbers(Kind, Normal); + internal record NumbersWithSpecial(List Normal, List Special) : Numbers(Normal); }