diff --git a/Lottery/Generator.cs b/Lottery/Generator.cs index f27f7b6..c399367 100644 --- a/Lottery/Generator.cs +++ b/Lottery/Generator.cs @@ -1,101 +1,19 @@ namespace Lottery { - record Limits(int Count, int Lower, int Upper); - - /// - /// Abstract base class for generators - /// All subclasses must implement the IGenerator interface - /// Provides some default implementation - /// - class Generator : IGenerator - { - public virtual Numbers Generate() => throw new NotImplementedException(); - - protected static void FillNumbers(List NumberList, Limits Limits) + internal class Generator + { + public static List Generate(Limits Limits) { List Candidates = Enumerable.Range(Limits.Lower, Limits.Upper).ToList(); + List Numbers = []; for (int i = 0; i < Limits.Count; i++) { int RandomIndex = Random.Shared.Next(Candidates.Count); - NumberList.Add(Candidates[RandomIndex]); + Numbers.Add(Candidates[RandomIndex]); Candidates.RemoveAt(RandomIndex); } - NumberList.Sort(); - } - - protected static void FillNumbers( - List NormalList, Limits NormalLimits, - List SpecialList, Limits SpecialLimits) - { - FillNumbers(NormalList, NormalLimits); - FillNumbers(SpecialList, SpecialLimits); - } - } - - /// - /// Concrete UKLottoGenerator class. - /// Generates six balls from 1 to 59 - /// - class UKLottoGenerator : Generator - { - private readonly Limits Limits = new(6, 1, 59); - - public override Numbers Generate() - { - Numbers Numbers = new([]); - Generator.FillNumbers(Numbers.Normal, Limits); - return Numbers; - } - } - - /// - /// Concrete EuroMillionsGenerator class. - /// Generates five balls from 1 to 50 and two balls from 1 to 12 - /// - class EuroMillionsGenerator : Generator - { - private readonly Limits NormalLimits = new(5, 1, 50); - private readonly Limits SpecialLimits = new(2, 1, 12); - - public override Numbers Generate() - { - NumbersWithSpecial Numbers = new([], []); - Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits); - return Numbers; - } - } - - /// - /// Concrete SetForLifeGenerator class. - /// Generates five balls from 1 to 47 and one ball from 1 to 10 - /// - class SetForLifeGenerator : Generator - { - private readonly Limits NormalLimits = new(5, 1, 47); - private readonly Limits SpecialLimits = new(1, 1, 10); - - public override Numbers Generate() - { - NumbersWithSpecial Numbers = new([], []); - Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits); - return Numbers; - } - } - - /// - /// Concrete ThunderBallGenerator class. - /// Generates fives balls from 1 to 39 and one ball from 1 to 14 - /// - class ThunderBallGenerator : Generator - { - private readonly Limits NormalLimits = new(5, 1, 39); - private readonly Limits SpecialLimits = new(1, 1, 14); - - public override Numbers Generate() - { - NumbersWithSpecial Numbers = new([], []); - Generator.FillNumbers(Numbers.Normal, NormalLimits, Numbers.Special, SpecialLimits); + Numbers.Sort(); return Numbers; } } diff --git a/Lottery/IGenerator.cs b/Lottery/IGenerator.cs deleted file mode 100644 index f086499..0000000 --- a/Lottery/IGenerator.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Lottery -{ - /// - /// All Generators must implement the Generate method - /// - interface IGenerator - { - Numbers Generate(); - } -} diff --git a/Lottery/Lottery.cs b/Lottery/Lottery.cs index 8168c05..13671a4 100644 --- a/Lottery/Lottery.cs +++ b/Lottery/Lottery.cs @@ -1,53 +1,36 @@ namespace Lottery { - /// - /// 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) + internal class Lottery(Limits limits) { - protected Numbers GenerateNumbers() => Generator.Generate(); - - public virtual string Output() - { - var numbers = GenerateNumbers(); - return $"Numbers: {string.Join(", ", numbers.Normal)}"; - } + protected Limits Limits => limits; + public virtual string Play() => $"Numbers: {string.Join(", ", Generator.Generate(Limits))}"; } - /// - /// Abstract base class, for lotteries with special values. - /// It subclasses Lottery. - /// - /// A fully formed Generator - internal class LotteryWithSpecial(IGenerator Generator) : Lottery(Generator) + internal class LotteryWithSpecial(Limits limits, Limits specialLimits) : Lottery(limits) { + protected Limits SpecialLimits => specialLimits; protected virtual string SpecialIdentifier => throw new NotImplementedException(); - public override string Output() + public override string Play() { - 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)}", + $"Numbers: {string.Join(", ", Generator.Generate(Limits))}", + $"{SpecialIdentifier}: {string.Join(", ", Generator.Generate(SpecialLimits))}", ]); } } - internal class EuroMillionsLotteryWithSpecial(IGenerator Generator) : LotteryWithSpecial(Generator) + internal class EuroMillionsLottery(Limits limits, Limits specialLimits) : LotteryWithSpecial(limits, specialLimits) { protected override string SpecialIdentifier => "Lucky Stars"; } - internal class SetForLifeLotteryWithSpecial(IGenerator Generator) : LotteryWithSpecial(Generator) + internal class SetForLifeLottery(Limits limits, Limits specialLimits) : LotteryWithSpecial(limits, specialLimits) { protected override string SpecialIdentifier => "Life Ball"; } - internal class ThunderballLotteryWithSpecial(IGenerator Generator) : LotteryWithSpecial(Generator) + internal class ThunderballLottery(Limits limits, Limits specialLimits) : LotteryWithSpecial(limits, specialLimits) { protected override string SpecialIdentifier => "Thunderball"; } diff --git a/Lottery/MainPage.xaml b/Lottery/MainPage.xaml index 3412cf7..83ec55f 100644 --- a/Lottery/MainPage.xaml +++ b/Lottery/MainPage.xaml @@ -9,9 +9,9 @@ Spacing="25"> -