add base Generator class, it implements IGenerator

Generate() methods call FillNumbers to get number values.
Numbers are randomized, sized according to limits, unique (no duplicates) and sorted.
This commit is contained in:
onyx-and-iris 2024-09-26 21:06:48 +01:00
parent 1b4e0baebb
commit 5b7f06f8b7

View File

@ -1,56 +1,97 @@
namespace Lottery namespace Lottery
{ {
/// <summary> record Limits(int Count, int Lower, int Upper);
///
/// </summary>
class UKGenerator : IGenerator
{
public int Count { get; } = 6;
public List<int> Generate() /// <summary>
/// Abstract base class for generators
/// It implements the IGenerator interface
/// Provides some default implementation
/// </summary>
class Generator : IGenerator
{ {
List<int> numbers = []; public virtual Numbers Generate() => throw new NotImplementedException();
for (int i = 0; i < Count; i++)
protected static void FillNumbers(List<int> NumberList, Limits Limits)
{ {
numbers.Add(Random.Shared.Next(60)); List<int> Candidates = Enumerable.Range(Limits.Lower, Limits.Upper).ToList();
for (int i = 0; i < Limits.Count; i++)
{
int RandomIndex = Random.Shared.Next(Candidates.Count);
NumberList.Add(Candidates[RandomIndex]);
Candidates.RemoveAt(RandomIndex);
} }
return numbers; NumberList.Sort();
} }
} }
/// <summary> /// <summary>
/// /// Concrete UKGenerator class.
/// Generates six balls from 1 to 59
/// </summary> /// </summary>
class EuroGenerator : IGenerator class UKGenerator : Generator
{ {
public int Count { get; } = 8; private readonly Limits Limits = new(6, 1, 59);
public List<int> Generate() public override Numbers Generate()
{ {
List<int> numbers = []; Numbers Numbers = new(KindOfLotto.Uk, []);
for (int i = 0; i < Count; i++) Generator.FillNumbers(Numbers.Normal, Limits);
{ return Numbers;
numbers.Add(Random.Shared.Next(60));
}
return numbers;
} }
} }
/// <summary> /// <summary>
/// /// Concrete EuroGenerator class.
/// Generates five balls from 1 to 50 and two balls from 1 to 12
/// </summary> /// </summary>
class SetForLifeGenerator : IGenerator class EuroGenerator : Generator
{ {
public int Count { get; } = 10; private readonly Limits NormalLimits = new(5, 1, 50);
private readonly Limits SpecialLimits = new(2, 1, 12);
public List<int> Generate() public override Numbers Generate()
{ {
List<int> numbers = []; NumbersWithSpecial Numbers = new(KindOfLotto.Euro, [], []);
for (int i = 0; i < Count; i++) Generator.FillNumbers(Numbers.Normal, NormalLimits);
{ Generator.FillNumbers(Numbers.Special, SpecialLimits);
numbers.Add(Random.Shared.Next(60)); return Numbers;
} }
return numbers; }
/// <summary>
/// Concrete SetForLifeGenerator class.
/// Generates five balls from 1 to 47 and one ball from 1 to 10
/// </summary>
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(KindOfLotto.SetForLife, [], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits);
Generator.FillNumbers(Numbers.Special, SpecialLimits);
return Numbers;
}
}
/// <summary>
/// Concrete ThunderBallGenerator class.
/// Generates fives balls from 1 to 39 and one ball from 1 to 14
/// </summary>
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(KindOfLotto.Thunderball, [], []);
Generator.FillNumbers(Numbers.Normal, NormalLimits);
Generator.FillNumbers(Numbers.Special, SpecialLimits);
return Numbers;
} }
} }
} }