From 1ab125985c960b23dbc6d83aeef941b436366f3f Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 27 Sep 2024 01:26:28 +0100 Subject: [PATCH] add Lottery hierarchy. Each lottery now has a generator. --- Lottery/Generator.cs | 8 ++-- Lottery/{KindOfLotto.cs => KindOfLottery.cs} | 5 ++- Lottery/Lottery.cs | 31 ++++++++++++++ Lottery/MainPage.xaml.cs | 43 ++++++++++---------- Lottery/Numbers.cs | 4 +- 5 files changed, 63 insertions(+), 28 deletions(-) rename Lottery/{KindOfLotto.cs => KindOfLottery.cs} (58%) create mode 100644 Lottery/Lottery.cs diff --git a/Lottery/Generator.cs b/Lottery/Generator.cs index 78a9c66..4ae7895 100644 --- a/Lottery/Generator.cs +++ b/Lottery/Generator.cs @@ -35,7 +35,7 @@ public override Numbers Generate() { - Numbers Numbers = new(KindOfLotto.Uk, []); + Numbers Numbers = new(KindOfLottery.Uk, []); Generator.FillNumbers(Numbers.Normal, Limits); return Numbers; } @@ -52,7 +52,7 @@ public override Numbers Generate() { - NumbersWithSpecial Numbers = new(KindOfLotto.Euro, [], []); + NumbersWithSpecial Numbers = new(KindOfLottery.Euro, [], []); Generator.FillNumbers(Numbers.Normal, NormalLimits); Generator.FillNumbers(Numbers.Special, SpecialLimits); return Numbers; @@ -70,7 +70,7 @@ public override Numbers Generate() { - NumbersWithSpecial Numbers = new(KindOfLotto.SetForLife, [], []); + NumbersWithSpecial Numbers = new(KindOfLottery.SetForLife, [], []); Generator.FillNumbers(Numbers.Normal, NormalLimits); Generator.FillNumbers(Numbers.Special, SpecialLimits); return Numbers; @@ -88,7 +88,7 @@ public override Numbers Generate() { - NumbersWithSpecial Numbers = new(KindOfLotto.Thunderball, [], []); + NumbersWithSpecial Numbers = new(KindOfLottery.Thunderball, [], []); Generator.FillNumbers(Numbers.Normal, NormalLimits); Generator.FillNumbers(Numbers.Special, SpecialLimits); return Numbers; diff --git a/Lottery/KindOfLotto.cs b/Lottery/KindOfLottery.cs similarity index 58% rename from Lottery/KindOfLotto.cs rename to Lottery/KindOfLottery.cs index a2b54b2..787e79c 100644 --- a/Lottery/KindOfLotto.cs +++ b/Lottery/KindOfLottery.cs @@ -1,6 +1,9 @@ namespace Lottery { - enum KindOfLotto : int + /// + /// + /// + enum KindOfLottery : int { Uk, Euro, diff --git a/Lottery/Lottery.cs b/Lottery/Lottery.cs new file mode 100644 index 0000000..3ec22ff --- /dev/null +++ b/Lottery/Lottery.cs @@ -0,0 +1,31 @@ +namespace Lottery +{ + /// + /// Base Lottery class, it accepts a Generator + /// SpecialIdentifier may be overriden for Lotteries with Special values. + /// + /// + internal class Lottery(IGenerator Generator) + { + public virtual string? SpecialIdentifier { get; } + + public Numbers GenerateNumbers() => Generator.Generate(); + } + + internal class UKLottoLottery(IGenerator Generator) : Lottery(Generator) { }; + + internal class EuroMillionsLottery(IGenerator Generator) : Lottery(Generator) + { + public override string SpecialIdentifier { get; } = "Lucky Stars"; + } + + internal class SetForLifeLottery(IGenerator Generator) : Lottery(Generator) + { + public override string SpecialIdentifier { get; } = "Life Ball"; + } + + internal class ThunderballLottery(IGenerator Generator) : Lottery(Generator) + { + public override string SpecialIdentifier { get; } = "Thunderball"; + } +} diff --git a/Lottery/MainPage.xaml.cs b/Lottery/MainPage.xaml.cs index ff8a90a..7ebcc54 100644 --- a/Lottery/MainPage.xaml.cs +++ b/Lottery/MainPage.xaml.cs @@ -2,14 +2,14 @@ { public partial class MainPage : ContentPage { - readonly List Generators = [new UKLottoGenerator(), new EuroMillionsGenerator(), new SetForLifeGenerator(), new ThunderBallGenerator()]; - IGenerator Generator; - const KindOfLotto DEFAULT_GENERATOR = KindOfLotto.Uk; - Dictionary SpecialIdentifiers = new() { - { KindOfLotto.Euro, "Lucky Stars" }, - { KindOfLotto.SetForLife, "Life Ball" }, - { KindOfLotto.Thunderball, "Thunderball" } - }; + readonly List Lotteries = [ + new UKLottoLottery(new UKLottoGenerator()), + new EuroMillionsLottery(new EuroMillionsGenerator()), + new SetForLifeLottery(new SetForLifeGenerator()), + new ThunderballLottery(new ThunderBallGenerator()) + ]; + const KindOfLottery DefaultLottery = KindOfLottery.Uk; + Lottery Lottery; public MainPage() { @@ -17,39 +17,40 @@ List lottos = ["UK Lotto", "EuroMillions", "Set For Life", "Thunderball"]; LottoPicker.ItemsSource = lottos; - LottoPicker.SelectedIndex = (int)DEFAULT_GENERATOR; + LottoPicker.SelectedIndex = (int)DefaultLottery; - Generator = Generators[LottoPicker.SelectedIndex]; + Lottery = Lotteries[LottoPicker.SelectedIndex]; } private void SpinButton_Clicked(object sender, EventArgs e) { - Numbers numbers = Generator.Generate(); + Numbers numbers = Lottery.GenerateNumbers(); + List output = []; switch (numbers.Kind) { - case KindOfLotto.Uk: - NumbersLabel.Text = $"Numbers: {string.Join(", ", numbers.Normal)}"; + case KindOfLottery.Uk: + output.Add($"Numbers: {string.Join(", ", numbers.Normal)}"); break; - case KindOfLotto.Euro: - case KindOfLotto.SetForLife: - case KindOfLotto.Thunderball: + case KindOfLottery.Euro: + case KindOfLottery.SetForLife: + case KindOfLottery.Thunderball: if (numbers is NumbersWithSpecial numbersWithSpecial) { - List output = [ + output.AddRange([ $"Numbers: {string.Join(", ", numbersWithSpecial.Normal)}", - $"{SpecialIdentifiers[numbers.Kind]}: {string.Join(", ", numbersWithSpecial.Special)}" - ]; - NumbersLabel.Text = string.Join("\t", output); + $"{Lottery.SpecialIdentifier}: {string.Join(", ", numbersWithSpecial.Special)}" + ]); } break; default: throw new LottoPickerException($"no NumbersLabel output defined for {numbers.Kind}"); }; + NumbersLabel.Text = string.Join("\t", output); } private void LottoPicker_SelectedIndexChanged(object sender, EventArgs e) { - Generator = Generators[LottoPicker.SelectedIndex]; + Lottery = Lotteries[LottoPicker.SelectedIndex]; } } } diff --git a/Lottery/Numbers.cs b/Lottery/Numbers.cs index 98c1edf..e9b224e 100644 --- a/Lottery/Numbers.cs +++ b/Lottery/Numbers.cs @@ -1,6 +1,6 @@ namespace Lottery { - internal record Numbers(KindOfLotto Kind, List Normal); + internal record Numbers(KindOfLottery Kind, List Normal); - internal record NumbersWithSpecial(KindOfLotto Kind, List Normal, List Special) : Numbers(Kind, Normal); + internal record NumbersWithSpecial(KindOfLottery Kind, List Normal, List Special) : Numbers(Kind, Normal); }