简体   繁体   中英

Best practice to compare Object properties in C# Lottery Ticket

I am trying to build a lotto ticket validator in C#.. The progam will allow users to generate a ticket with a collection of multiple chances.. Each chance will consist of 6 numbers. 5 Regular numbers and 1 Power number. The user then inputs the drawn numbers which are compared to each chance on the ticket.

What is the best way to model this and make the comparisons?

As a test, I've been able to use string[] array representations of both models and iterate through them.. But as you can see with the below code, it only tells me the number of matches in all.. I need a way to iterate through custom objects and count how many matches in each chance.. Furthermore, I need a way to make it a bonus if the power number is matched.. And lastly this will be a web application so in my view, the tickets will be displayed in a table. How do i notify which number should be highlighted (ie a match)? Just the logic, i can handle the rest..

I've never used reflection so maybe that's the answer but the only examples I see are comparing objects of the same type and that are not in collections..

Please help.. Thanks..

My Test Code

int matches = 0;

        List<string[]> numbas = new List<string[]>();

        string[] numbers = new string[] { "55", "10", "12", "42" };
        string[] numbers1 = new string[] { "62", "45", "84", "23" };
        string[] numbers2 = new string[] { "2", "23", "45", "43" };
        string[] numbers3 = new string[] { "4", "45", "4", "43" };
        string[] numbers4 = new string[] { "86", "33", "67", "51" };


        numbas.Add(numbers);
        numbas.Add(numbers1);
        numbas.Add(numbers2);
        numbas.Add(numbers3);
        numbas.Add(numbers4);

        string[] drawn = new string[] { "51", "10", "33" };



        for (int rows = 0; rows <= numbas.Count-1; rows++)
        {
            for (int drawNum = 0; drawNum <= drawn.Length-1; drawNum++)
            {
                for (int tickNo = 0; tickNo <= numbas[rows].Length-1; tickNo++)
                {
                    Console.WriteLine("Drawn Number {0} being comparted to number {1}", drawn[drawNum], numbas[rows].ElementAt(tickNo));
                    if (drawn[drawNum] == numbas[rows].ElementAt(tickNo))
                    {
                        matches++;
                    }
                }


            }

        }

        Console.WriteLine("There were " + matches + " matches.");
        Console.ReadLine();

This is what I have so far for the actual program.

My Models

    public class Ticket
{
    public int TicketID { get; set; }
    public string TicketName { get; set; }
    public DateTime DrawDate { get; set; }

    public virtual ICollection<Chance> Chances { get; set; }
    public virtual DrawnNumber DrawnNumber { get; set; }

}


public class Chance
{
    public int RowID { get; set; }
    public int TicketID { get; set; }

    public string Number1 { get; set; }
    public string Number2 { get; set; }
    public string Number3 { get; set; }
    public string Number4 { get; set; }
    public string Number5 { get; set; }

    public string PowerNumber { get; set; }


}

public class DrawnNumber
{
    public int DrawnNumberID { get; set; }
    public int TicketID { get; set; }

    public string DrawnNumber1 { get; set; }
    public string DrawnNumber2 { get; set; }
    public string DrawnNumber3 { get; set; }
    public string DrawnNumber4 { get; set; }
    public string DrawnNumber5 { get; set; }

    public string DrawnPowerNumber { get; set; }

}

After careful considering the scenario I found it best to simply create more complex objects for each lotto number. This way it will give me functionality on each number (ie. set true if number is selected)

Instead of:

public int Number1

Do:

Class Number 

{
   int value;
   bool isTrue;

}

class Number1 : Number 

{

}

Also Make my Chance class which is the fields containing the lotto numbers, derive from IEnumberable in order to implement the GetEnumerator Method to iterate over all fields.. (check against winning numbers)

Like:

 public class Chance : IEnumerable
        {
            public string chanceNumber;
            public FirstNumber FirstNumber;
            public SecondNumber SecondNumber;
            public ThirdNumber ThirdNumber;
            public FourthNumber FourthNumber;
            public FifthNumber FifthNumber;
            public PowerNumber PowerNumber;
            public Number[] numbers;

            public Chance()
            {
            }


            public IEnumerator GetEnumerator()
            {
                numbers = new Number[] { FirstNumber, SecondNumber, ThirdNumber, FourthNumber, FifthNumber, PowerNumber};      
                return new NumberEnumerator(numbers);
            }
}

Works like a charm! Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM