Answer to Question #213855 in C# for Hassam

Question #213855

Q.No.1. There are total 50 employees working in XYZ organization who have a policy of Bonus via lucky draw. They want to automate the process to select employees randomly for lucky draw.     

Write code for a console application where you have to declare an integer array that store employee numbers. Employee number will be provided as input to a function fillEmployee(int eNum). Make sure, employee number, must be 4 digit unique number otherwise repeat the process till you get valid input.  

Now the company wants to announce employee through a Lucky Draw. You are required to generate random 4 digit employee number and compare with existing employee numbers (using Random class of C#). Repeat the process till you match a valid employee number and then print “congratulation” message along with winner’s employee number. 



1
Expert's answer
2021-07-05T13:29:21-0400
using System;
using System.Linq;


namespace LuckyDraw
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] employeeNumbers = FillEmployee(3); //You can change number here, to check the solution


            var random = new Random();


            int winner = 0;


            while (!employeeNumbers.Contains(winner))
            {
                winner = random.Next(9999);
                Console.Write($"{winner}\r");
            }


            PrintLine();
            Console.WriteLine($"Congratulations! The Winner is number {winner}!");
        }


        public static int[] FillEmployee(int eNum)
        {
            var numbers = new int[eNum];
            int employeeCount = 0;


            Console.WriteLine("Hello! We need to fill the list of employees.");
            PrintLine();


            while(employeeCount < eNum)
            {
                Console.WriteLine($"Please, enter 4 digit unique number. {eNum - employeeCount} left.");


                string input = string.Empty;
                while (input.Length < 4)
                {
                    char symbol = Console.ReadKey().KeyChar;


                    if (char.IsDigit(symbol))
                    { 
                        input += symbol;
                    }
                    else
                    {
                        ShowErrorMessage("\nError! Only digits is allowed!");
                        input = string.Empty;
                    }
                }


                int parsedNumber = int.Parse(input);


                if (numbers.Contains(parsedNumber))
                {
                    ShowErrorMessage("\nThis number is already registered!");
                    continue;
                }
                
                numbers[employeeCount++] = parsedNumber;


                Console.WriteLine();
                PrintLine();
                PrintNumberAccepted(input);
                PrintLine();
            }


            PrintAllNumbersAccepted();


            return numbers;


        }


        public static void PrintMessage(string message, ConsoleColor color)
        {
            Console.ForegroundColor = color;
            Console.WriteLine(message);
            Console.ResetColor();
        }


        public static void PrintAllNumbersAccepted()
        {
            PrintMessage("All numbers accepted! Thank you!", ConsoleColor.Yellow);
        }


        public static void ShowErrorMessage(string message)
        {
            PrintMessage(message, ConsoleColor.Red);
        }


        public static void PrintNumberAccepted(string num)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Number {num} accepted!");
            Console.ResetColor();
        }


        public static void PrintLine()
        {
            Console.WriteLine(new string('-', 50));
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS