Beispiel #1
0
        // This class manages the persistant object by reading from and writing to a file

        // Write the Person List to file as a serialized binary object
        public static bool writeToFile(ref PersonListClass plist, string fn)
        {
            Stream          thisFileStream;
            BinaryFormatter serializer = new BinaryFormatter();

            if (plist.getPersonList().Count() > 0)
            {
                try
                {
                    thisFileStream = File.Create(fn);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File open error: Person List not written", "POManager File Open");
                    MessageBox.Show(ex.ToString());
                    return(false);
                }  // end Try

                try
                {
                    serializer.Serialize(thisFileStream, plist);
                    //MsgBox("File write: Person List was written")
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File write error: Person List not written", "POManager File Write");
                    MessageBox.Show(ex.ToString());
                    return(false);
                }
                finally
                {
                    thisFileStream.Close();
                }  // end Try
            }
            else
            {
                MessageBox.Show("No Person in List");
            }
            // end if

            return(true);  // The file write succeeded
        }  // end WriteToFile
Beispiel #2
0
        // Button event handler for the OK button
        // Flags used for the Client, Manager, and Worker
        // Depending on which button was pressed, the form changes to what is needed
        // Validation and Insert to SFile and DB done as well
        private void btnOk_Click(object sender, EventArgs e)
        {
            // After the client button is clicked
            // Adds data related to the client to the serializable file and database
            if (clientClick == true)
            {
                //Validating ID
                if (newValidator.IsValidID(txtControlPersonID.Text) == false)
                {
                    MessageBox.Show("Invalid ID");
                    return;
                }

                //Validating name
                if (newValidator.IsValidEnglishCharacters(txtControlPersonName.Text) == false)
                {
                    MessageBox.Show("Invalid name.");
                    return;
                }

                //Validating date
                if (newValidator.IsValidDate(txtControlPersonBirthDate.Text) == false)
                {
                    MessageBox.Show("Invalid date.");
                    return;
                }

                //Validating client
                if (newValidator.IsValidEnglishCharacters(txtControlClientType.Text) == false)
                {
                    MessageBox.Show("Invalid Client Type");
                    return;
                }

                bool foundID = false;
                foreach (PersonClass personObject in personList.getPersonList())
                {
                    if (txtControlPersonID.Text == personObject.personID)
                    {
                        foundID = true;
                    }
                }
                if (foundID == true)
                {
                    MessageBox.Show("Person already inside serializable file.");
                }

                foundID = false;
                db.findDuplicateID(Convert.ToInt32(txtControlPersonID.Text), ref foundID);

                if (foundID == true)
                {
                    MessageBox.Show("Person is already inside Database.");
                    return;
                }

                ClientClass newClient = new ClientClass(txtControlPersonName.Text, txtControlPersonBirthDate.Text, txtControlPersonID.Text, txtControlClientType.Text);
                personList.getPersonList().Add(newClient);
                db.InsertPerson(Convert.ToInt32(txtControlPersonID.Text), txtControlPersonName.Text, txtControlPersonBirthDate.Text);
                db.InsertClient(Convert.ToInt32(txtControlPersonID.Text), txtControlClientType.Text);
                MessageBox.Show("Client has been added.");
            }
            // After the manger button is clicked
            // Adds manager related data to the serializable file and database
            else if (managerClick == true)
            {
                //Validating ID
                if (newValidator.IsValidID(txtControlPersonID.Text) == false)
                {
                    MessageBox.Show("Invalid ID");
                    return;
                }

                //Validating name
                if (newValidator.IsValidEnglishCharacters(txtControlPersonName.Text) == false)
                {
                    MessageBox.Show("Invalid name.");
                    return;
                }

                //Validating date
                if (newValidator.IsValidDate(txtControlPersonBirthDate.Text) == false)
                {
                    MessageBox.Show("Invalid date.");
                    return;
                }

                //Job Title
                if (newValidator.IsValidEnglishCharacters(txtControlEmployeeJobTitle.Text) == false)
                {
                    MessageBox.Show("Invalid Job Title.");
                    return;
                }


                //Salary
                if (newValidator.IsValidCurrency(txtControlManagerSalary.Text) == false)
                {
                    MessageBox.Show("Invalid Manager Currency.");
                    return;
                }

                //Bonus
                if (newValidator.IsValidCurrency(txtControlManagerBonus.Text) == false)
                {
                    MessageBox.Show("Invalid Manager Bonus.");
                    return;
                }

                //End of Validations

                bool foundID = false;
                foreach (PersonClass personObject in personList.getPersonList())
                {
                    if (txtControlPersonID.Text == personObject.personID)
                    {
                        foundID = true;
                    }
                }
                if (foundID == true)
                {
                    MessageBox.Show("Person already inside serializable file.");
                }

                foundID = false;
                db.findDuplicateID(Convert.ToInt32(txtControlPersonID.Text), ref foundID);

                if (foundID == true)
                {
                    MessageBox.Show("Person is already inside Database.");
                    return;
                }
                ManagerClass newManager = new ManagerClass(txtControlPersonName.Text, txtControlPersonBirthDate.Text, txtControlPersonID.Text, txtControlEmployeeJobTitle.Text, Convert.ToDecimal(txtControlManagerSalary.Text), Convert.ToDecimal(txtControlManagerBonus.Text));
                personList.getPersonList().Add(newManager);
                db.InsertPerson(Convert.ToInt32(txtControlPersonID.Text), txtControlPersonName.Text, txtControlPersonBirthDate.Text);
                db.InsertEmployee(Convert.ToInt32(txtControlPersonID.Text), txtControlEmployeeJobTitle.Text);
                db.InsertManager(Convert.ToInt32(txtControlPersonID.Text), Convert.ToDecimal(txtControlManagerSalary.Text), Convert.ToDecimal(txtControlManagerBonus.Text));
                MessageBox.Show("Manager has been added.");
            }
            // After the worker button has been clicked
            // Data related to the worker is added to the serializable file and database
            else if (workerClick == true)
            {
                //Validating ID
                if (newValidator.IsValidID(txtControlPersonID.Text) == false)
                {
                    MessageBox.Show("Invalid ID");
                    return;
                }

                //Validating name
                if (newValidator.IsValidEnglishCharacters(txtControlPersonName.Text) == false)
                {
                    MessageBox.Show("Invalid name.");
                    return;
                }

                //Validating date
                if (newValidator.IsValidDate(txtControlPersonBirthDate.Text) == false)
                {
                    MessageBox.Show("Invalid date.");
                    return;
                }

                //Job Title
                if (newValidator.IsValidEnglishCharacters(txtControlEmployeeJobTitle.Text) == false)
                {
                    MessageBox.Show("Invalid Job Title.");
                    return;
                }

                //Hourly Pay
                if (newValidator.IsValidCurrency(txtControlWorkerHourlyPay.Text) == false)
                {
                    MessageBox.Show("Invalid Hourly Pay.");
                    return;
                }

                bool foundID = false;
                foreach (PersonClass personObject in personList.getPersonList())
                {
                    if (txtControlPersonID.Text == personObject.personID)
                    {
                        foundID = true;
                    }
                }
                if (foundID == true)
                {
                    MessageBox.Show("Person already inside serializable file.");
                }

                foundID = false;
                db.findDuplicateID(Convert.ToInt32(txtControlPersonID.Text), ref foundID);

                if (foundID == true)
                {
                    MessageBox.Show("Person is already inside Database.");
                    return;
                }


                WorkerClass newWorker = new WorkerClass(txtControlPersonName.Text, txtControlPersonBirthDate.Text, txtControlPersonID.Text, txtControlEmployeeJobTitle.Text, Convert.ToDecimal(txtControlWorkerHourlyPay.Text));
                personList.getPersonList().Add(newWorker);
                db.InsertPerson(Convert.ToInt32(txtControlPersonID.Text), txtControlPersonName.Text, txtControlPersonBirthDate.Text);
                db.InsertEmployee(Convert.ToInt32(txtControlPersonID.Text), txtControlEmployeeJobTitle.Text);
                db.InsertWorker(Convert.ToInt32(txtControlPersonID.Text), Convert.ToDecimal(txtControlWorkerHourlyPay.Text));
                MessageBox.Show("Worker has been added.");
            }
            //After the user clicks on the update button
            //Adds data from the text fields to the serializable file and the database
            else if (editUpdateClick == true)
            {
                //Validating ID
                if (newValidator.IsValidID(txtControlSearchID.Text) == false)
                {
                    MessageBox.Show("Invalid ID");
                    return;
                }

                foreach (PersonClass personObject in personList.getPersonList())
                {
                    if (txtControlSearchID.Text == personObject.personID)
                    {
                        if (personObject.GetType() == typeof(ClientClass))
                        {
                            //Validating client
                            if (newValidator.IsValidEnglishCharacters(txtControlClientType.Text) == false)
                            {
                                MessageBox.Show("Invalid Client Type");
                                return;
                            }

                            DialogResult dialogResult = MessageBox.Show("Are you sure you want to edit the following item?", "Edit Confirmation", MessageBoxButtons.YesNo);
                            if (dialogResult == DialogResult.Yes)
                            {
                                personObject.Save(Globals.newForm);
                                db.UpdatePerson(txtControlPersonID.Text, txtControlPersonName.Text, txtControlPersonBirthDate.Text);
                                db.UpdateClient(Convert.ToInt32(txtControlPersonID.Text), txtControlClientType.Text);
                                MessageBox.Show("Updating Client.");
                            }
                            else if (dialogResult == DialogResult.No)
                            {
                                MessageBox.Show("Edit canceled.");
                            }
                        }
                        if (personObject.GetType() == typeof(ManagerClass))
                        {
                            //Job Title
                            if (newValidator.IsValidEnglishCharacters(txtControlEmployeeJobTitle.Text) == false)
                            {
                                MessageBox.Show("Invalid Job Title.");
                                return;
                            }

                            //Salary
                            if (newValidator.IsValidCurrency(txtControlManagerSalary.Text) == false)
                            {
                                MessageBox.Show("Invalid Manager Currency.");
                                return;
                            }

                            //Bonus
                            if (newValidator.IsValidCurrency(txtControlManagerBonus.Text) == false)
                            {
                                MessageBox.Show("Invalid Manager Bonus.");
                                return;
                            }


                            DialogResult dialogResult = MessageBox.Show("Are you sure you want to edit the following item?", "Edit Confirmation", MessageBoxButtons.YesNo);
                            if (dialogResult == DialogResult.Yes)
                            {
                                personObject.Save(Globals.newForm);
                                db.UpdatePerson(txtControlPersonID.Text, txtControlPersonName.Text, txtControlPersonBirthDate.Text);
                                db.UpdateEmployee(Convert.ToInt32(txtControlPersonID.Text), txtControlEmployeeJobTitle.Text);
                                db.UpdateManager(Convert.ToInt32(txtControlPersonID.Text), Convert.ToDecimal(txtControlManagerSalary.Text), Convert.ToDecimal(txtControlManagerBonus.Text));
                                MessageBox.Show("Updating Manager.");
                            }
                            else if (dialogResult == DialogResult.No)
                            {
                                MessageBox.Show("Edit canceled.");
                            }
                        }
                        if (personObject.GetType() == typeof(WorkerClass))
                        {
                            //Job Title
                            if (newValidator.IsValidEnglishCharacters(txtControlEmployeeJobTitle.Text) == false)
                            {
                                MessageBox.Show("Invalid Job Title.");
                                return;
                            }

                            //Hourly Pay
                            if (newValidator.IsValidCurrency(txtControlWorkerHourlyPay.Text) == false)
                            {
                                MessageBox.Show("Invalid Hourly Pay.");
                                return;
                            }

                            DialogResult dialogResult = MessageBox.Show("Are you sure you want to edit the following item?", "Edit Confirmation", MessageBoxButtons.YesNo);
                            if (dialogResult == DialogResult.Yes)
                            {
                                personObject.Save(Globals.newForm);
                                db.UpdatePerson(txtControlPersonID.Text, txtControlPersonName.Text, txtControlPersonBirthDate.Text);
                                db.UpdateEmployee(Convert.ToInt32(txtControlPersonID.Text), txtControlEmployeeJobTitle.Text);
                                db.UpdateWorker(Convert.ToInt32(txtControlPersonID.Text), Convert.ToDecimal(txtControlWorkerHourlyPay.Text));
                                MessageBox.Show("Updating Worker.");
                            }
                            else if (dialogResult == DialogResult.No)
                            {
                                MessageBox.Show("Edit canceled.");
                            }
                        }
                    }
                }
            }
            // After the delete button is clicked
            // Person data is removed from the serializable file and the database
            else if (deleteClick == true)
            {
                DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete the following item?", "Delete Confirmation", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    for (int i = 0; i < personList.getPersonList().Count; i++)
                    {
                        if (personList.getPersonList().ElementAt(i).personID == txtControlPersonID.Text)
                        {
                            personList.getPersonList().RemoveAt(i);
                        }
                    }

                    MessageBox.Show("Deleting person with ID: " + txtControlPersonID.Text);
                    db.Delete(Convert.ToInt32(txtControlPersonID.Text));
                    FormControllerClass.clear(Globals.newForm);
                }
                else if (dialogResult == DialogResult.No)
                {
                    MessageBox.Show("Deletion canceled.");
                }
            }
        }