Exemplo n.º 1
0
        public static string InitializeDatabase()
        {
            string message = "Database initatialization done";
                string path = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
                var testingDatabase = new SQLiteConnection (System.IO.Path.Combine (path, "Database.db"));
                testingDatabase.CreateTable<Account> ();
                testingDatabase.CreateTable<Profile> ();
                testingDatabase.CreateTable<Accommodation> ();
            var stuList = testingDatabase.Query<Account> ("SELECT * FROM Account");
            if (stuList.Count == 0) {
                for (int i = 0; i < studentIDs.Length; i++) {
                    Account account = new Account ();
                    Profile profile = new Profile ();
                    Accommodation accom = new Accommodation ();
                    account.StudentID = studentIDs [i];
                    account.Password = passwords [i];

                    profile.StudentID = studentIDs [i];
                    profile.StudentName = names [i];
                    profile.Nationality = nationalities [i];
                    profile.ContactNumber = contactNums [i];
                    profile.Degree = degrees [i];
                    profile.Interest = interests[i];
                    profile.Year = years [i];
                    profile.AccommodationID = i.ToString ();

                    accom.ID = i.ToString ();
                    accom.Address = addresses [i];
                    accom.Suburb = suburbs [i];
                    accom.RentAWeek = rents [i];
                    accom.PreferredContact = contacts [i];
                    accom.Description = descriptions [i];
                    testingDatabase.Insert (account);
                    testingDatabase.Insert (accom);
                    testingDatabase.Insert (profile);
                }
            } else {
                message = "Database already set up";
            }
            return message;
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.RegisterScreen);

            TextView title = FindViewById<TextView> (Resource.Id.registerHeading);
            EditText studentIDInput = FindViewById<EditText> (Resource.Id.registerStudentIDInput);
            EditText passwordInput = FindViewById<EditText> (Resource.Id.registerPasswordInput);
            EditText rePasswordInput = FindViewById<EditText> (Resource.Id.registerRePasswordInput);
            EditText nameInput = FindViewById<EditText> (Resource.Id.registerStudentNameInput);
            EditText nationalityInput = FindViewById<EditText> (Resource.Id.registerNationalityInput);
            Spinner basicInterest = FindViewById<Spinner> (Resource.Id.registerInterest);
            CheckBox agreeTac = FindViewById<CheckBox> (Resource.Id.registerAgreeTac);
            Button registerAccountButton = FindViewById<Button> (Resource.Id.registerAccountButton);
            Button cancelButton = FindViewById<Button> (Resource.Id.cancelButton);

            // Set up fonts.
            Typeface din = Typeface.CreateFromAsset (this.Assets, "fonts/din-regular.ttf");
            Typeface dinBold = Typeface.CreateFromAsset (this.Assets, "fonts/din-bold.ttf");

            // Set font to "Din".
            agreeTac.SetTypeface (din, TypefaceStyle.Normal);

            // Set font to "Din Bold".
            title.SetTypeface (dinBold, TypefaceStyle.Normal);
            registerAccountButton.SetTypeface (dinBold, TypefaceStyle.Normal);
            cancelButton.SetTypeface (dinBold, TypefaceStyle.Normal);

            string studentID = String.Empty;
            string password = String.Empty;
            string rePassword = String.Empty;
            string name = String.Empty;
            string nationality = String.Empty;
            bool tac = false;

            string path = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
            var accountDB = new SQLiteConnection (System.IO.Path.Combine(path, "Database.db"));

            registerAccountButton.Click += (object sender, EventArgs e) => {
                studentID = studentIDInput.Text;
                password = passwordInput.Text;
                rePassword = rePasswordInput.Text;
                name = nameInput.Text;
                nationality = nationalityInput.Text;
                tac = agreeTac.Checked;

                string[] input = {studentID, password, rePassword, name, nationality};

                if(InputValidation.isFilled(input))
                {
                    string message = "";
                    var result = accountDB.Query<Account>("SELECT * FROM Account WHERE StudentID = '" + studentID + "'");

                    if (result.Count != 0)
                    {
                        message = GetString(Resource.String.account_exists);
                        DisplayUnsuccessfulAlert(message);
                    }

                    else if (!password.Equals(rePassword))
                    {
                        message = GetString(Resource.String.mismatched_passwords);
                        DisplayUnsuccessfulAlert(message);
                    }

                    else if (basicInterest.SelectedItem.ToString()  == "Please select an interest...")
                    {
                        message = GetString(Resource.String.select_interest);
                        DisplayUnsuccessfulAlert(message);
                    }

                    else if (!tac)
                    {
                        message = GetString(Resource.String.must_agree);
                        DisplayUnsuccessfulAlert(message);
                    }

                    else
                    {
                        Account acc = new Account();
                        Profile prof = new Profile();
                        Accommodation accom = new Accommodation();
                        var accomList = accountDB.Query<Account> ("SELECT * FROM Accommodation");

                        acc.StudentID = studentID;
                        acc.Password = password;
                        accountDB.Insert(acc);

                        prof.StudentID = studentID;
                        prof.StudentName = name;
                        prof.Nationality = nationality;
                        prof.ContactNumber = String.Empty;
                        prof.Degree = String.Empty;
                        prof.Interest = basicInterest.SelectedItem.ToString();
                        prof.Year = String.Empty;
                        prof.AccommodationID = accomList.Count.ToString();
                        accountDB.Insert(prof);

                        accom.ID = accomList.Count.ToString();
                        accom.Address = String.Empty;
                        accom.Suburb = String.Empty;
                        accom.RentAWeek = String.Empty;
                        accom.PreferredContact = String.Empty;
                        accom.Description = String.Empty;
                        accountDB.Insert(accom);

                        var successfulAlert = new AlertDialog.Builder(this);

                        successfulAlert.SetMessage(GetString(Resource.String.account_created));
                        successfulAlert.SetNeutralButton("OK", delegate{
                            var intent = new Intent(this, typeof(MainActivity));
                            StartActivity(intent);
                            // Stops user from pressing back button to return.
                            Finish();
                        });
                        successfulAlert.Show();
                    }
                }
                else
                {
                    var notFilledAlert = new AlertDialog.Builder(this);

                    notFilledAlert.SetMessage(GetString(Resource.String.required_fields));
                    notFilledAlert.SetNegativeButton("OK", delegate{});
                    notFilledAlert.Show();
                }

            };

            cancelButton.Click += (object sender, EventArgs e) => {
                var intent = new Intent (this, typeof(MainActivity));
                StartActivity (intent);
            };
        }