Esempio n. 1
0
        public static void AddClient(string idnumber, string name, string surname, string email, string phonenumber, string housenumber, string streetname, string suburb, string city, string province, string postalcode, string newsletter)
        {
            bool          errors          = false;
            string        newClientNumber = ClientNumberRandomLetter() + EightDigitNumber();
            List <Person> personlist      = Person.GetPerson();
            List <Client> clientList      = GetClient();

            //validation starts here
            try
            {
                if (String.IsNullOrEmpty(idnumber) || String.IsNullOrEmpty(name) || String.IsNullOrEmpty(surname) || String.IsNullOrEmpty(email) || String.IsNullOrEmpty(phonenumber) || String.IsNullOrEmpty(housenumber.ToString()) || String.IsNullOrEmpty(streetname) || String.IsNullOrEmpty(suburb) || String.IsNullOrEmpty(city) || String.IsNullOrEmpty(province) || String.IsNullOrEmpty(postalcode) || String.IsNullOrEmpty(newsletter))
                {
                    //if fields are empty a message box informs the user and sets errors to true.
                    errors = true;
                    throw new EmptyFieldsException();
                }
            }
            catch (EmptyFieldsException e)
            {
                MessageBox.Show(e.Message);
            }

            foreach (Client item in clientList)
            {
                if (item.clientNumber == newClientNumber)
                {
                    newClientNumber = newClientNumber + 1;
                }
            }

            foreach (Person item in personlist)
            {
                if (item.IDNumber == idnumber)
                {
                    MessageBox.Show("ID already in system");
                    errors = true;
                }
                if (item.Email == email)
                {
                    MessageBox.Show("Email Address already in system");
                    errors = true;
                }
                if (item.PhoneNumber == phonenumber)
                {
                    MessageBox.Show("Phone number already in system");
                    errors = true;
                }
            }
            if (phonenumber.All(Char.IsNumber) == false || phonenumber.Length > 10)
            {
                errors = true;
                MessageBox.Show("The phone number may only contain numbers, and be no longer than 10 characters!", "Client Name", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (name.All(Char.IsLetter) == false)
            {
                errors = true;
                MessageBox.Show("The client name may only contain letters!", "Client Name", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (surname.All(Char.IsLetter) == false)
            {
                errors = true;
                MessageBox.Show("The client surname may only contain letters!", "Client Surname", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (streetname.All(Char.IsLetter) == false)
            {
                errors = true;
                MessageBox.Show("The street name may only contain letters!", "Street Name", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (suburb.All(Char.IsLetter) == false)
            {
                errors = true;
                MessageBox.Show("The suburb name may only contain letters!", "Suburb Name", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (city.All(Char.IsLetter) == false)
            {
                errors = true;
                MessageBox.Show("The city name may only contain letters!", "City Name", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (postalcode.All(Char.IsNumber) == false)
            {
                errors = true;
                MessageBox.Show("The postal code may only contain numbers!", "Postal Code", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (housenumber.All(Char.IsLetterOrDigit) == false)
            {
                errors = true;
                MessageBox.Show("The housenumber may only contain numbers and letters!", "House Number", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            //if errors are true then a message box notifies the user to enter all necessary information correctly.
            if (errors == true)
            {
                MessageBox.Show("Please enter correct information");
            }
            //if errors are false then the new client gets added to the database.
            else
            {
                Data.AddClientSP(newClientNumber, idnumber, name, surname, email, phonenumber, housenumber, streetname, suburb, city, province, postalcode, newsletter);
                MessageBox.Show("The client has been added.", "Client Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }