Exemple #1
0
        /* Save button */
        private void Save_Click(object sender, EventArgs e)
        {
            // Get field's info
            string   username    = usernameTextBox.Text;
            string   password    = passwordTextBox.Text;
            string   firstName   = firstNameTextBox.Text;
            string   lastName    = lastNameTextBox.Text;
            string   email       = emailTextBox.Text;
            string   address     = addressTextBox.Text;
            string   postCode    = postCodeTextBox.Text;
            DateTime dateOfBirth = dobDateTimeBox.Value;
            bool     deleted     = deletedCheckBox.Checked;


            int id = 0;

            if (currentUser.IsAdmin) // If current user is an admin
            {
                id = person.ID;      // ID of select user from combobox
            }
            else                     // If current user is not an admin
            {
                id = currentUser.ID; // ID from current user
            }
            // If all textboxes have data and dateOfBirth is before today and after 1900
            if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password) && string.IsNullOrEmpty(firstName) && string.IsNullOrEmpty(lastName) && string.IsNullOrEmpty(email) && string.IsNullOrEmpty(address) && string.IsNullOrEmpty(postCode) && dateOfBirth < today && dateOfBirth > new DateTime(1900, 1, 1))
            {
                bool usernameTaken = db.UsernameTaken(username); // Checks if username is taken


                if (!usernameTaken)                                                                                       // If username is free to use
                {
                    if (currentUser.IsAdmin)                                                                              // If current user is an admin
                    {
                        db.DeleteAccount(id, deleted);                                                                    // Deletes/Restores account
                    }
                    db.UpdateAccount(id, username, password, firstName, lastName, email, address, postCode, dateOfBirth); // Updates account in the database
                    MessageBox.Show("Your details have been changed");                                                    // Conformation message box appears

                    // Updates current user
                    if (!currentUser.IsAdmin)                                                                                                                                             // If user is not an admin - admin account's are not displayed so only non admins can edit their own account
                    {
                        UserFactory factory = new CustomerFactory(currentUser.ID, currentUser.AccountNo, username, password, firstName, lastName, email, address, postCode, dateOfBirth); // Sets factory for customer
                        Main.CurrentUser = factory.CreateUser();                                                                                                                          // Creates customer and update CurrentUser
                    }
                }
                else // If username is not free to use
                {
                    MessageBox.Show("Username Taken"); // Error message
                }
            }
            else // If one of the fields does not have the correct data
            {
                MessageBox.Show("You haven't filled in all the fields.  Please try again!"); // Error message
            }
        }