0

I am working on a ASP.NET MVC web app. The web app checks a lottery ticket to match it with the winning numbers. The winning numbers are to be randomly generated. The user inputs their numbers in the form and checks to see if it is a match. I currently have the winning lottery number generator in the model as an array of 4 elements. I have an array created in the controller to collect the user input.

  1. Do I compare the arrays in the controller?
  2. If so how do I pass the _winningNumber array to the controller to be compared?
  3. Also I am going to create four labels in the view to allow the user to input the numbers, how do I connect that to the array in the controller?
  4. Do I need to sort both arrays then compare?

Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LottoNumbersCampbell.Models
{
    public class LottoNumbersCampbellModel
    {
        private int _matchingNumber;
        private int[] _winningNumbers;

        public int MatchingNumber { get => _matchingNumber; set => _matchingNumber = value; }

        public LottoNumbersCampbellModel()
        {
            
        }

        public LottoNumbersCampbellModel(int matchingNumber, int[] winningNumbers)
        {
            _matchingNumber = matchingNumber;
            _winningNumbers = winningNumbers;
        }

        public void GenerateLottoNumbers(int[] winningNumbers)
        {
            const int MIN = 1, MAX = 72;
            var random = new Random();
            for(int i = 0; i < 4; i++)
            {
                _winningNumbers[i] = random.Next(MIN, MAX);
            }
            // sort random numbers

        }

        public override bool Equals(object obj)
        {
            return obj is LottoNumbersCampbellModel campbell &&
                   EqualityComparer<int[]>.Default.Equals(_winningNumbers, campbell._winningNumbers);
        }
    }
    
}

Controller

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LottoNumbersCampbell.Models;

namespace LottoNumbersCampbell.Controllers
{
    public class HomeController : Controller
    {
        LottoNumbersCampbellModel ComparisonClass;

        public IActionResult Index()
        {
            int[] UserNumbers = new int[4]; // needs to be sorted.
            ComparisonClass.GenerateLottoNumbers(UserNumbers);
           //boolean to compare? 
            
         
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • _"The web app checks a lottery ticket to match it with the winning numbers."_ Define "match". Is [1, 2, 3, 4] a "match" to [4, 3, 2, 1], for instance? – Jasen Nov 16 '21 at 20:33
  • In the view there will be for labels four user input. [input one, input two, input 3, input 4]. Those numbers have to be compared to the random number array in the same order. [1, 2, 3, 4]. – Kcampbell22 Nov 16 '21 at 20:59
  • So that answers #4 -- you don't need to sort. #1 see https://stackoverflow.com/questions/3232744/easiest-way-to-compare-arrays-in-c-sharp #2 see https://stackoverflow.com/questions/28325456/posting-a-string-array. And #3, you don't really need to associate the labels on the controller side if you're handling a 4 element array -- just use the built-in index. For display, sure go ahead an insert ` – Jasen Nov 16 '21 at 21:15

0 Answers0