add Lottery hierarchy.

Each lottery now has a generator.
This commit is contained in:
onyx-and-iris 2024-09-27 01:26:28 +01:00
parent f8bc3a7edc
commit 1ab125985c
5 changed files with 63 additions and 28 deletions

View File

@ -35,7 +35,7 @@
public override Numbers Generate() public override Numbers Generate()
{ {
Numbers Numbers = new(KindOfLotto.Uk, []); Numbers Numbers = new(KindOfLottery.Uk, []);
Generator.FillNumbers(Numbers.Normal, Limits); Generator.FillNumbers(Numbers.Normal, Limits);
return Numbers; return Numbers;
} }
@ -52,7 +52,7 @@
public override Numbers Generate() public override Numbers Generate()
{ {
NumbersWithSpecial Numbers = new(KindOfLotto.Euro, [], []); NumbersWithSpecial Numbers = new(KindOfLottery.Euro, [], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits); Generator.FillNumbers(Numbers.Normal, NormalLimits);
Generator.FillNumbers(Numbers.Special, SpecialLimits); Generator.FillNumbers(Numbers.Special, SpecialLimits);
return Numbers; return Numbers;
@ -70,7 +70,7 @@
public override Numbers Generate() public override Numbers Generate()
{ {
NumbersWithSpecial Numbers = new(KindOfLotto.SetForLife, [], []); NumbersWithSpecial Numbers = new(KindOfLottery.SetForLife, [], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits); Generator.FillNumbers(Numbers.Normal, NormalLimits);
Generator.FillNumbers(Numbers.Special, SpecialLimits); Generator.FillNumbers(Numbers.Special, SpecialLimits);
return Numbers; return Numbers;
@ -88,7 +88,7 @@
public override Numbers Generate() public override Numbers Generate()
{ {
NumbersWithSpecial Numbers = new(KindOfLotto.Thunderball, [], []); NumbersWithSpecial Numbers = new(KindOfLottery.Thunderball, [], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits); Generator.FillNumbers(Numbers.Normal, NormalLimits);
Generator.FillNumbers(Numbers.Special, SpecialLimits); Generator.FillNumbers(Numbers.Special, SpecialLimits);
return Numbers; return Numbers;

View File

@ -1,6 +1,9 @@
namespace Lottery namespace Lottery
{ {
enum KindOfLotto : int /// <summary>
///
/// </summary>
enum KindOfLottery : int
{ {
Uk, Uk,
Euro, Euro,

31
Lottery/Lottery.cs Normal file
View File

@ -0,0 +1,31 @@
namespace Lottery
{
/// <summary>
/// Base Lottery class, it accepts a Generator
/// SpecialIdentifier may be overriden for Lotteries with Special values.
/// </summary>
/// <param name="Generator"></param>
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";
}
}

View File

@ -2,14 +2,14 @@
{ {
public partial class MainPage : ContentPage public partial class MainPage : ContentPage
{ {
readonly List<IGenerator> Generators = [new UKLottoGenerator(), new EuroMillionsGenerator(), new SetForLifeGenerator(), new ThunderBallGenerator()]; readonly List<Lottery> Lotteries = [
IGenerator Generator; new UKLottoLottery(new UKLottoGenerator()),
const KindOfLotto DEFAULT_GENERATOR = KindOfLotto.Uk; new EuroMillionsLottery(new EuroMillionsGenerator()),
Dictionary<KindOfLotto, string> SpecialIdentifiers = new() { new SetForLifeLottery(new SetForLifeGenerator()),
{ KindOfLotto.Euro, "Lucky Stars" }, new ThunderballLottery(new ThunderBallGenerator())
{ KindOfLotto.SetForLife, "Life Ball" }, ];
{ KindOfLotto.Thunderball, "Thunderball" } const KindOfLottery DefaultLottery = KindOfLottery.Uk;
}; Lottery Lottery;
public MainPage() public MainPage()
{ {
@ -17,39 +17,40 @@
List<string> lottos = ["UK Lotto", "EuroMillions", "Set For Life", "Thunderball"]; List<string> lottos = ["UK Lotto", "EuroMillions", "Set For Life", "Thunderball"];
LottoPicker.ItemsSource = lottos; 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) private void SpinButton_Clicked(object sender, EventArgs e)
{ {
Numbers numbers = Generator.Generate(); Numbers numbers = Lottery.GenerateNumbers();
List<string> output = [];
switch (numbers.Kind) switch (numbers.Kind)
{ {
case KindOfLotto.Uk: case KindOfLottery.Uk:
NumbersLabel.Text = $"Numbers: {string.Join(", ", numbers.Normal)}"; output.Add($"Numbers: {string.Join(", ", numbers.Normal)}");
break; break;
case KindOfLotto.Euro: case KindOfLottery.Euro:
case KindOfLotto.SetForLife: case KindOfLottery.SetForLife:
case KindOfLotto.Thunderball: case KindOfLottery.Thunderball:
if (numbers is NumbersWithSpecial numbersWithSpecial) if (numbers is NumbersWithSpecial numbersWithSpecial)
{ {
List<string> output = [ output.AddRange([
$"Numbers: {string.Join(", ", numbersWithSpecial.Normal)}", $"Numbers: {string.Join(", ", numbersWithSpecial.Normal)}",
$"{SpecialIdentifiers[numbers.Kind]}: {string.Join(", ", numbersWithSpecial.Special)}" $"{Lottery.SpecialIdentifier}: {string.Join(", ", numbersWithSpecial.Special)}"
]; ]);
NumbersLabel.Text = string.Join("\t", output);
} }
break; break;
default: default:
throw new LottoPickerException($"no NumbersLabel output defined for {numbers.Kind}"); throw new LottoPickerException($"no NumbersLabel output defined for {numbers.Kind}");
}; };
NumbersLabel.Text = string.Join("\t", output);
} }
private void LottoPicker_SelectedIndexChanged(object sender, EventArgs e) private void LottoPicker_SelectedIndexChanged(object sender, EventArgs e)
{ {
Generator = Generators[LottoPicker.SelectedIndex]; Lottery = Lotteries[LottoPicker.SelectedIndex];
} }
} }
} }

View File

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