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()
{
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;

View File

@ -1,6 +1,9 @@
namespace Lottery
{
enum KindOfLotto : int
/// <summary>
///
/// </summary>
enum KindOfLottery : int
{
Uk,
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
{
readonly List<IGenerator> Generators = [new UKLottoGenerator(), new EuroMillionsGenerator(), new SetForLifeGenerator(), new ThunderBallGenerator()];
IGenerator Generator;
const KindOfLotto DEFAULT_GENERATOR = KindOfLotto.Uk;
Dictionary<KindOfLotto, string> SpecialIdentifiers = new() {
{ KindOfLotto.Euro, "Lucky Stars" },
{ KindOfLotto.SetForLife, "Life Ball" },
{ KindOfLotto.Thunderball, "Thunderball" }
};
readonly List<Lottery> 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<string> 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<string> 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<string> 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];
}
}
}

View File

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