Answer - C# Event Handling Exercises

Qu: Write a program for Online Attendance. The conditions are as follow:
  1. User provides their name as Input and then application show message to “Welcome to their Name”.
  2. Jack, Steven, and Mathew are banned for the organization. So, when any user enters Jack, Steven, and Mathew as username, the application raised an event and fire alarm as well as sends email to administration.
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Online_Application
{
    class Program
    {
        static void Main(string[] args)
        {
            EnterName ename = new EnterName();
            ename.ev_BannedUser += WarningAlarm;
            ename.User();
            Console.Read();
        }
        static void WarningAlarm(object sender, BannedUserEventArgs e)
        {
            Console.WriteLine("{0} Users Found. Sending Email to Administration.", e.Name);
            Console.WriteLine("Email Sent.");
            Console.WriteLine("Warning Alarm Started.");
            Console.WriteLine("Press Ctrl + c to stop the alarm");
            for (;;)
            {
                Console.Beep();
                System.Threading.Thread.Sleep(100);
            }
        }
    }

    public class EnterName
    {
        public event EventHandler<BannedUserEventArgs> ev_BannedUser;
        public void User()
        {
            Console.Write("Enter You Name : ");
            string user = Console.ReadLine();

            if ((user == "Jack" || user == "Steven" || user == "Mathew") && (ev_BannedUser!=null))
            {
                ev_BannedUser(this, new BannedUserEventArgs(user));
            }
            else
            {
                Console.WriteLine("Welcome to " + user);
            }
        }
    }

    public class BannedUserEventArgs : EventArgs
    {
        public BannedUserEventArgs(string s)
        {
            Name = s;
        }
        public string Name { get; set; }
    }
}

When this program gets executed and user enters any of the banned user name, the program started beeping for infinite time. You must press ctrl + c to stop this beep sound.

More Articles

 

Share your thought



PLEASE DISABLE ADBLOCKER AND SUPPORT US!

Please support us by enabling ads on this page. Refresh

YOU DON'T LIKE ADS, WE ALSO DON'T LIKE ADS!
 
But we have to show ads on our site to keep it free and updated. We have to pay huge server costs, domain costs, CDN Costs, Developer Costs, Electricity and Internet Bill. Your little contribution will encourage us to regularly update this site.