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.
- Do I compare the arrays in the controller?
- If so how do I pass the _winningNumber array to the controller to be compared?
- 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?
- 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?
}
}
}