none
email id validation example RRS feed

  • Question

  • EmployeeValidator 
    Employee
    -------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EmployeeValidation
    {
        public class Employee
        {
            /*
            * Do not modify the return types of the below properties
            * 
            */
    
            public string EmployeeId { get; set; }
            public string EmployeeName { get; set; }
            public string EmailId { get; set; }
            public string DateOfJoining { get; set; }
        }
    
        // Do not add new constructors
    }
    
    
    -------------------------------------------------------------------------------------------------------------------
    EmployeeValidator
    -------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Data;
    using System.Collections;
    
    namespace EmployeeValidation
    {
        public class EmployeeValidator
        {
            /*
            * Do not remove the attached TestProject. It is meant for auto evaluation of your source code.
            * Do not attach any test classess to the attached test project.
            * Do not attach any new test projects.
            * You are not required to write any automated test cases. You are supposed to write only the code.
            */
            #region "ProcessData"
            //process the xml data
            //Parameters are  xmlFilePath, xmlFileName, connection
            public void ProcessData(string xmlFilePath, string xmlFileName, SqlConnection connection)
            {
                //Do your logic here
                //Step 1           
                //ReadAllEmployeesFromXmlFile
                //Step 2           
                //PickValidEmployees
                //Step 3
                //SaveValidEmployeesToDataBase           
    
            }
            #endregion
    
            #region "ReadAllEmployeesFromXmlFile"
            //Read all the employees in XML File
            //xmlFilePath and xmlFileName are passed as parameter
            public List<Employee> ReadAllEmployeesFromXmlFile(string xmlFilePath, string xmlFileName)
            {
                //Read the employee details from the xml file and return it in List collection
                //Do not hardcode the filename and the file path here
                
                //Do not return the date with time appended to it.
    
                return null;
            }
            #endregion
           
            #region "PickValidEmployees" 
            // Pick the vaild employees from given employee List
            //Employee List passed as Parameter 
            public List<Employee> PickValidEmployees(List<Employee> employees)
            {
                //Pick the valid employees from the List collection
                //Return the valid employees in a List
                
                return null;//Return only valid employees in List
            }
            #endregion
            
            #region "SaveValidEmployeesToDB"
            // Save the valid employee in database
            //parameter List of employees and connection object
            public void SaveValidEmployeesToDB(List<Employee> employees, SqlConnection connection)
            {
                //Do not Prefix Database name in the SQL Query. Query should be "Insert into SBA.TableName"
                //Should not be "Insert into DatabaseName.SBA.TableName"
                //Do not hardcode the connection string here
            }
            #endregion
    
            #region "SortEmployeebyEmployeeName"
            // Sort the EmployeeName from Employee List
            public void SortEmployeebyEmployeeName()
            {
                Employee e1 = new Employee { EmployeeId = "101", EmployeeName = "John", EmailId = "John@hotmail.com", DateOfJoining = "8/2/2011" };
                Employee e2 = new Employee { EmployeeId = "102", EmployeeName = "Preethi", EmailId = "Preethi@gmail.com", DateOfJoining = "10/6/2008" };
                Employee e3 = new Employee { EmployeeId = "103", EmployeeName = "Jenifer", EmailId = "Jenifer@yahoo.com", DateOfJoining = "6/10/2004" };
    
                // Add the employee details in the employee List 
                //sort the employee details by EmployeeName
            }
            #endregion
    
        }
    }
    
    -------------------------------------------------------------------------------------------------------------------
    EmployeeValidatorException
    -------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace EmployeeValidation
    {
       
        public class EmployeeValidatorException : Exception
        {
            public EmployeeValidatorException()
                : base()
            {
    
            }
    
            public EmployeeValidatorException(string message)
                : base(message)
            {
    
            }
        }
    }
    
    
    -------------------------------------------------------------------------------------------------------------------
    Program
    -------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    namespace EmployeeValidation
    {
        public class Program
        {
            public static void Main()
            {
                  /*
                   * Pass the file path, file names and connection string if any in this method alone. 
                   * Do not hardcode in any other methods
                   */
                SqlConnection connection = new SqlConnection(@"Data Source=PCName\\SQLEXPRESS;Initial Catalog=DBEmployeeValidation;Integrated Security=True");
                EmployeeValidator empValidator = new EmployeeValidator();
                empValidator.ProcessData(@"D:\Dotnet\EmployeeValidator\Input File\", "Emp_12201.xml", connection);
                           
            }
        }
    }
    
    
    -------------------------------------------------------------------------------------------------------------------

    Friday, December 22, 2017 9:36 PM

Answers

  • Employee
    -------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace EmployeeValidation
    {
        public class Employee
        {
            /*
            * Do not modify the return types of the below properties
            * 
            */
    
            public string EmployeeId { get; set; }
            public string EmployeeName { get; set; }
            public string EmailId { get; set; }
            public string DateOfJoining { get; set; }
        }
    
        // Do not add new constructors
    }
    
    -------------------------------------------------------------------------------------------------------------------
    EmployeeValidator
    -------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Data;
    using System.Xml.Serialization;
    using System.Text.RegularExpressions;
    using System.Xml;
    
    namespace EmployeeValidation
    {
        public class EmployeeValidator
        {
            /*
            * Do not remove the attached TestProject. It is meant for auto evaluation of your source code.
            * Do not attach any test classess to the attached test project.
            * Do not attach any new test projects.
            * You are not required to write any automated test cases. You are supposed to write only the code.
            */
            #region "ProcessData"
            //process the xml data
            //Parameters are  xmlFilePath, xmlFileName, connection
            public void ProcessData(string xmlFilePath, string xmlFileName, SqlConnection connection)
            {
    
                EmployeeValidator empVal = new EmployeeValidator();
                List<Employee> lstemp = new List<Employee>();
                lstemp = empVal.ReadAllEmployeesFromXmlFile(xmlFilePath, xmlFileName);
    
                lstemp = empVal.PickValidEmployees(lstemp);
    
                empVal.SaveValidEmployeesToDB(lstemp, connection);
    
            }
    
            #endregion
    
            #region "ReadAllEmployeesFromXmlFile"
            //Read all the employees in XML File
            //xmlFilePath and xmlFileName are passed as parameter
            public List<Employee> ReadAllEmployeesFromXmlFile(string xmlFilePath, string xmlFileName)
            {
                List<Employee> empList = new List<Employee>();
                XElement xelement = XElement.Load(xmlFilePath + xmlFileName);
                IEnumerable<XElement> allEmployees = xelement.Elements();
    
                foreach (var allEmployee in allEmployees)
                {
                    empList.Add(new Employee()
                    {
                        EmployeeId = allEmployee.Element("EmployeeId").Value,
                        EmployeeName = allEmployee.Element("EmployeeName").Value,
                        EmailId = allEmployee.Element("EmailId").Value,
                        DateOfJoining = allEmployee.Element("DateOfJoining").Value
                    });
                }
                return empList;
            }
            #endregion
    
            #region "PickValidEmployees" 
            // Pick the vaild employees from given employee List
            //Employee List passed as Parameter 
            public List<Employee> PickValidEmployees(List<Employee> employees)
            {
                List<Employee> empList = new List<Employee>();
                Regex IsNumeric = new Regex(@"^\D+$");
                Regex IsAlphaNumeric = new Regex(@"\W+$");
                Regex IsvalidEmail = new Regex(@"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$");
    
                empList = employees.Select(x => employees.Find(
                    m => m.EmployeeName != null && m.EmployeeName != string.Empty
                        && !IsNumeric.IsMatch(m.EmployeeId.ToString()) && !IsAlphaNumeric.IsMatch(m.EmployeeName)
                         )).Distinct().ToList();
    
                return empList;
            }
            #endregion
    
            #region "SaveValidEmployeesToDB"
            // Save the valid employee in database
            //parameter List of employees and connection object
            public void SaveValidEmployeesToDB(List<Employee> employees, SqlConnection connection)
            {
                string query = @"Insert into SBA.Employees(EmployeeId,EmployeeName,DateOfJoining,EmailId) 
                                                Values(@EmployeeId,@EmployeeName,@DateOfJoining,@EmailId)";
                connection.Open();
                using (SqlCommand cmd = new SqlCommand(query, connection))
                {
                    foreach (Employee emp in employees)
                    {
                        cmd.Parameters.AddWithValue("@EmployeeId", emp.EmployeeId);
                        cmd.Parameters.AddWithValue("@EmployeeName", emp.EmployeeName);
                        cmd.Parameters.AddWithValue("@EmailId", emp.EmailId);
                        cmd.Parameters.AddWithValue("@DateOfJoining", emp.DateOfJoining);
                        int i = cmd.ExecuteNonQuery();
                    }
                }
                connection.Close();
            }
            #endregion
    
            #region "SortEmployeebyEmployeeName"
            // Sort the EmployeeName from Employee List
            public void SortEmployeebyEmployeeName()
            {
                Employee e1 = new Employee { EmployeeId = "101", EmployeeName = "John", EmailId = "John@hotmail.com", DateOfJoining ="8/2/2011"  };
                Employee e2 = new Employee { EmployeeId = "102", EmployeeName = "Preethi", EmailId = "Preethi@gmail.com", DateOfJoining = "10/6/2008" };
                Employee e3 = new Employee { EmployeeId = "103", EmployeeName = "Jenifer", EmailId = "Jenifer@yahoo.com", DateOfJoining = "6/10/2004" };
    
                List<Employee> employees = new List<Employee>();
                employees.Add(e1);
                employees.Add(e1);
                employees.Add(e1);
                List<Employee> sortedList = employees.OrderBy<Employee, string>(p => p.EmployeeName).ToList();
            }
            #endregion
        }
    }
    
    
    -------------------------------------------------------------------------------------------------------------------
    EmployeeValidatorException
    -------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace EmployeeValidation
    {
       
        public class EmployeeValidatorException : Exception
        {
            public EmployeeValidatorException()
                : base()
            {
    
            }
    
            public EmployeeValidatorException(string message)
                : base(message)
            {
    
            }
        }
    }
    
    
    -------------------------------------------------------------------------------------------------------------------
    Program
    -------------------------------------------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    namespace EmployeeValidation
    {
        public class Program
        {
            public static void Main()
            {
                  /*
                   * Pass the file path, file names and connection string if any in this method alone. 
                   * Do not hardcode in any other methods
                   */
                SqlConnection connection = new SqlConnection(@"Data Source=PC23638;Initial Catalog=DBEmployeeValidation;Integrated Security=True");
                EmployeeValidator empValidator = new EmployeeValidator();
                empValidator.ProcessData(@"C:\Users\15239\Desktop\Input File\", "Emp_12201.xml", connection);
                           
            }
        }
    }
    
    
    -------------------------------------------------------------------------------------------------------------------

    • Marked as answer by eralph Friday, December 22, 2017 9:37 PM
    Friday, December 22, 2017 9:36 PM