Exemplo n.º 1
0
        /// <summary>
        /// Method Name: LoadDatabase.
        /// The purpose of this method is to parse an input string from a file into objects to be added to the database. This method
        /// ensures that all of the database items it is creating are valid, else it will not create them. The format
        /// in which a database item is read in is as such:
        /// (Employee Type)|(Last Name)|(First Name)|(SIN)|(SubField1)|(SubField2)|(SubField3)|
        /// </summary>
        /// <returns>A boolean value of true upon completion.</returns>
        public bool LoadDatabase()
        {
            String fileInput = "";
            FullTimeEmployee ft = new FullTimeEmployee();
            PartTimeEmployee pt = new PartTimeEmployee();
            ContractEmployee c = new ContractEmployee();
            SeasonalEmployee s = new SeasonalEmployee();

            String currentType = "";
            String tempSin = "";
            Boolean sinValid = true;

            String[] objects;

            //Read in the data from the database file.
            FileStream db = databaseFile.Openfile("dbase.dtb", 'R');
            fileInput = databaseFile.ReadFromFile(db);
            databaseFile.CloseFile(db);

            //Remove null terminations and carrige returns.
            fileInput = fileInput.Replace("\0", "");
            fileInput = fileInput.Replace("\r", "");

            //Split up the fields for each new line.
            objects = fileInput.Split('\n');

            //Initialize an multi-array for each attribute.
            String[][] attributes = new String[objects.Length][];
            for (int k = 0; k < objects.Length; k++)
            {
                attributes[k] = new String[10];
            }

            //For each object to be entered into the database.
            for (int i = 0; i < objects.Length; i++)
            {
                //Reset the valid SIN flag to true.
                sinValid = true;

                //Split up the object into attributes.
                attributes[i] = objects[i].Split('|');

                //For each attribute in the current object.
                for (int j = 0; j < attributes[i].Length; j++)
                {
                    //If the current attribute is the employee identifier.
                    if (j == 0)
                    {
                        //Check what type of employee the object is.
                        switch (attributes[i][0])
                        {
                            //If the employee is a Full Time employee.
                            case "FT":
                                //If the SIN number is valid.
                                if (ft.CheckSinNumber(attributes[i][3]))
                                {
                                    //Reset the full Time employee object to house the attributes.
                                    ft = new FullTimeEmployee();
                                    //Insert the employee type into the current employee object.
                                    currentType = attributes[i][0];
                                }
                                else
                                {
                                    //Set the valid SIN flag to false.
                                    sinValid = false;
                                    //Break the current object's cycle of the for loop.
                                    j = attributes.Length;
                                }
                                break;
                            //If the employee is a Part Time employee.
                            case "PT":
                                //If the SIN number is valid.
                                if (ft.CheckSinNumber(attributes[i][3]))
                                {
                                    //Reset the Part Time employee object to house the attributes.
                                    pt = new PartTimeEmployee();
                                    //Insert the employee type into the current employee object.
                                    currentType = attributes[i][0];
                                }
                                else
                                {
                                    //Set the valid SIN flag to false.
                                    sinValid = false;
                                    //Break the current object's cycle of the for loop.
                                    j = attributes.Length;
                                }
                                break;
                            //If the employee is a Contract Employee.
                            case "CT":
                                //If the SIN number is valid.
                                if (ft.CheckSinNumber(attributes[i][3]))
                                {
                                    //Reset the Contract employee object to house the attributes.
                                    c = new ContractEmployee();
                                    //Insert the employee type into the current employee object.
                                    currentType = attributes[i][0];
                                }
                                else
                                {
                                    //Set the valid SIN flag to false.
                                    sinValid = false;
                                    //Break the current object's cycle of the for loop.
                                    j = attributes.Length;
                                }
                                break;
                            //If the employee is a Seasonal Employee.
                            case "SN":
                                //If the SIN number is valid.
                                if (ft.CheckSinNumber(attributes[i][3]))
                                {
                                    //Reset the Seasonal Employee object to house the attributes.
                                    s = new SeasonalEmployee();
                                    //Insert the employee type into the current employee object.
                                    currentType = attributes[i][0];
                                }
                                else
                                {
                                    //Set the valid SIN flag to false.
                                    sinValid = false;
                                    //Break the current object's cycle of the for loop.
                                    j = attributes.Length;
                                }
                                break;
                            //Not a valid employee type.
                            default:
                                //Nullify the current employee type.
                                currentType = "";
                                //Break the current object's cycle of the for loop.
                                j = attributes.Length;
                                break;
                        }
                    }
                    //If the current attribute is not the employee identifier.
                    else
                    {
                        //Check the current employee type.
                        switch (currentType)
                        {
                            //If the current employee is a Full Time employee
                            case "FT":
                                //Switch based the the current attribute being read in.
                                switch (j)
                                {
                                    //Set the first name.
                                    case 1:
                                        //Make sure there are no spaces.
                                        attributes[i][j] = attributes[i][j].Replace(" ", "");
                                        ft.SetLastName(attributes[i][j]);
                                        break;
                                    //Set the last name.
                                    case 2:
                                        //Make sure there are no spaces.
                                        attributes[i][j] = attributes[i][j].Replace(" ", "");
                                        ft.SetFirstName(attributes[i][j]);
                                        break;
                                    //Set the SIN number.
                                    case 3:
                                        if (ft.CheckSinNumber(attributes[i][j]))
                                        {
                                            //Make sure there are no spaces in the SIN number.
                                            attributes[i][j] = attributes[i][j].Replace(" ", "");
                                            ft.SetSocialNumber(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Date of Birth.
                                    case 4:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            ft.SetDateOfBirth(new DateTime(0));
                                        }
                                        else
                                        {
                                            ft.SetDateOfBirth(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Date of Hire.
                                    case 5:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            ft.SetDateOfHire(new DateTime(0));
                                        }
                                        else
                                        {
                                            ft.SetDateOfHire(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Date of Termination.
                                    case 6:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            ft.SetDateOfTermination(new DateTime(0));
                                        }
                                        else
                                        {
                                            ft.SetDateOfTermination(attributes[i][j]);
                                        }
                                        break;
                                    //Set the current Salary.
                                    case 7:
                                        try
                                        {
                                            ft.SetSalary(float.Parse(attributes[i][j]));
                                        }
                                        catch
                                        {
                                        }
                                        break;
                                }
                                break;
                            //If the current employee is a Part Time employee.
                            case "PT":
                                //Switch based on the current attribute.
                                switch (j)
                                {
                                    //Set the first name.
                                    case 1:
                                        attributes[i][j] = attributes[i][j].Replace(" ", "");
                                        pt.SetLastName(attributes[i][j]);
                                        break;
                                    //Set the last name.
                                    case 2:
                                        attributes[i][j] = attributes[i][j].Replace(" ", "");
                                        pt.SetFirstName(attributes[i][j]);
                                        break;
                                    //Set the sin number.
                                    case 3:
                                        if (pt.CheckSinNumber(attributes[i][j]))
                                        {
                                            attributes[i][j] = attributes[i][j].Replace(" ", "");
                                            pt.SetSocialNumber(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Date of Birth.
                                    case 4:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            pt.SetDateOfBirth(new DateTime(0));
                                        }
                                        else
                                        {
                                            pt.SetDateOfBirth(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Date of Hire.
                                    case 5:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            pt.SetDateOfHire(new DateTime(0));
                                        }
                                        else
                                        {
                                            pt.SetDateOfHire(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Date of Termination.
                                    case 6:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            pt.SetDateOfTermination(new DateTime(0));
                                        }
                                        else
                                        {
                                            pt.SetDateOfTermination(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Hourly Rate.
                                    case 7:
                                        try
                                        {
                                            pt.SetHourlyRate(float.Parse(attributes[i][j]));
                                        }
                                        catch
                                        {
                                        }
                                        break;
                                }
                                break;
                            //If the current employee is a Contract Employee.
                            case "CT":
                                switch (j)
                                {
                                    //Set the last name.
                                    case 1:
                                        attributes[i][j] = attributes[i][j].Replace(" ", "");
                                        c.SetLastName(attributes[i][j]);
                                        break;
                                    //Set the first name.
                                    case 2:
                                        attributes[i][j] = attributes[i][j].Replace(" ", "");
                                        c.SetFirstName(attributes[i][j]);
                                        break;
                                    //Set the SIN number.
                                    case 3:
                                        tempSin = attributes[i][j];
                                        break;
                                    //Set the Date of Birth.
                                    case 4:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            c.SetDateOfBirth(new DateTime(0));
                                        }
                                        else
                                        {
                                            c.SetDateOfBirth(attributes[i][j]);
                                        }
                                        if (c.CheckSinNumber(tempSin))
                                        {
                                            tempSin = tempSin.Replace(" ", "");
                                            c.SetSocialNumber(tempSin);
                                        }
                                        break;
                                    //Set the Contract Start Date.
                                    case 5:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            c.SetContractStartDate(new DateTime(0));
                                        }
                                        else
                                        {
                                            c.SetContractStartDate(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Contract End Date.
                                    case 6:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            c.SetContractStartDate(new DateTime(0));
                                        }
                                        else
                                        {
                                            c.SetContractStopDate(attributes[i][j]);
                                        }
                                        break;
                                    case 7:
                                        try
                                        {
                                            c.SetFixedContractAmount(float.Parse(attributes[i][j]));
                                        }
                                        catch
                                        {
                                        }
                                        break;
                                }
                                break;
                            //If the current employee is a Seasonal Employee.
                            case "SN":
                                //Switch based on the current attribute.
                                switch (j)
                                {
                                    //Set the last name.
                                    case 1:
                                        attributes[i][j] = attributes[i][j].Replace(" ", "");
                                        s.SetLastName(attributes[i][j]);
                                        break;
                                    //Set the first name.
                                    case 2:
                                        attributes[i][j] = attributes[i][j].Replace(" ", "");
                                        s.SetFirstName(attributes[i][j]);
                                        break;
                                    //Set the SIN number.
                                    case 3:
                                        if (s.CheckSinNumber(attributes[i][j]))
                                        {
                                            attributes[i][j] = attributes[i][j].Replace(" ", "");
                                            s.SetSocialNumber(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Date of Birth.
                                    case 4:
                                        if (attributes[i][j] == "N/A")
                                        {
                                            s.SetDateOfBirth(new DateTime(0));
                                        }
                                        else
                                        {
                                            s.SetDateOfBirth(attributes[i][j]);
                                        }
                                        break;
                                    //Set the Season.
                                    case 5:
                                        s.SetSeason(attributes[i][j]);
                                        break;
                                    //Set the piece pay.
                                    case 6:
                                        try
                                        {
                                            s.SetPiecePay(float.Parse(attributes[i][j]));
                                        }
                                        catch
                                        {
                                        }
                                        break;
                                }
                                break;
                        }
                    }
                }
                //If the valid SIN flag was not invalidated.
                if (sinValid)
                {
                    //Check the current employee type.
                    switch (currentType)
                    {
                        //If the current employee is a Full Time Employee.
                        case "FT":
                            //Validate the full time employee.
                            if (ft.Validate())
                            {
                                //Add the employee to the database.
                                databaseContainer.Add(ft);
                            }
                            break;
                        //If the current employee is a Part Time employee.
                        case "PT":
                            //Validate the part tiem employee.
                            if (pt.Validate())
                            {
                                //Add the employee to the database.
                                databaseContainer.Add(pt);
                            }
                            break;
                        //If the current employee is a Contract employee.
                        case "CT":
                            //Validate the COntract employee.
                            if (c.Validate())
                            {
                                //Add the employee to the database.
                                databaseContainer.Add(c);
                            }
                            break;
                        //If the current employee is a Seasonal Employee.
                        case "SN":
                            //Validate the seasonal Employee.
                            if (s.Validate())
                            {
                                //Add the employee to the database.
                                databaseContainer.Add(s);
                            }
                            break;
                    }
                }
            }
            return true;
        }