示例#1
0
        /// <summary>
        /// Method Name: AddFullTimeEmployee.
        /// The purpose of thsi function is to get the data associated with a Full Time Employee from the user, and
        /// add it to the database.  The data will be validated before it is added to the database. Any errors
        /// will be indicated to the user.
        /// </summary>
        /// <returns></returns>
        private bool AddFullTimeEmployee()
        {
            // create  a Full Time employee object which will house the following information
            // for this full time employee
            FullTimeEmployee entry = new FullTimeEmployee();
            // declare local variables
            bool result = false;
            float salary = 0;

            this.SetBaseAttributes(entry);

            // get the date the employee was hired
            Console.WriteLine("Please enter the date the employee was hired <YYYY-MM-DD>:");
            result = entry.SetDateOfHire(Console.ReadLine());
            while (result == false)
            {
                Console.WriteLine("Please enter a valid date in which the employee was hired <YYYY-MM-DD>:");
                result = entry.SetDateOfHire(Console.ReadLine());
            }

            // get the employee's yearly salary
            Console.WriteLine("Please enter the employee's yearly salary (example 45000.54):");
            while (true)
            {

                try
                {
                    salary = float.Parse(Console.ReadLine().Replace("$", ""));
                    if (entry.SetSalary(salary) != false)
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Please re-enter a valid employee's salary (example 45000.54):");
                    }
                }
                catch
                {
                    Console.WriteLine("Please re-enter a valid employee's salary (example 45000.54):");
                }
            }

            // get the date of termination if the employee was fired
            Console.WriteLine("Please enter a date of termination or enter a '0' if the employee is still employed at your company:");
            result = entry.SetDateOfTermination(Console.ReadLine());
            while (result == false)
            {
                Console.WriteLine("Please re-enter a valid date of termination or enter a '0' if the employee is still employed at your company:");
                result = entry.SetDateOfTermination(Console.ReadLine());
            }

            // validate the employee information
            if (entry.Validate() == false)
            {
                logfile.Log(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, 'A', 'F', "Full-Time Employee");
                Console.WriteLine("There was an error with the employee entered. Please add the employee again.");
                return false;
            }
            // add the entry to the database
            try
            {
                databaseContainer.Add(entry);
            }
            catch
            {
                // if there was an error indicate to the user that the employee could not be added to the database
                Console.WriteLine("The employee could not be added to the database.");
                logfile.Log(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, 'A', 'F', "Full-Time Employee");
                return false;
            }
            Console.Clear();
            Console.WriteLine("\nThe employee added was:\n");
            logfile.Log(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, 'A', 'S', "Full-Time Employee");

            entry.Details();
            return true;
        }