コード例 #1
0
        private void FindUser_btn_Click(object sender, RoutedEventArgs e)
        {
            //Making a string with an error message, that would be returned from function findCsvLine if line doesnt exists
            string error = "Error 1! Record Not Found. Please try again.";

            //flow control with switch and goto. Was with ifs and else's but currently is more readable and less repeated code.
            switch (select_cmBox.SelectedIndex)
            {
            case 0:     //phone number selected
                if (phone_inp_txtBox != null && !string.IsNullOrWhiteSpace(phone_inp_txtBox.Text))
                {
                    string[] name_phone = CsvIO.findCsvLine(phone_inp_txtBox.Text, 2, "users.csv");
                    if (name_phone[0] == error)
                    {
                        goto case 4;
                    }
                    user3.User_name  = name_phone[1];
                    user3.User_phone = name_phone[2];
                    goto case 5;
                }
                else
                {
                    goto case 3;
                }

            case 1:     //name selected
                if (name_inp_txtBox != null && !string.IsNullOrWhiteSpace(name_inp_txtBox.Text))
                {
                    string[] name_phone = CsvIO.findCsvLine(name_inp_txtBox.Text, 1, "users.csv");
                    if (name_phone[0] == error)
                    {
                        goto case 4;
                    }
                    user3.User_name  = name_phone[1];
                    user3.User_phone = name_phone[2];
                    goto case 5;
                }
                else
                {
                    goto case 3;
                }

            case 3:     //input empty
                MessageBox.Show("Please input search term!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                break;

            case 4:     //user not found
                MessageBox.Show("User not found in the database! Please try again!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                user3.User_name  = String.Empty;
                user3.User_phone = String.Empty;
                break;

            case 5:     //final check
                MessageBoxResult result = MessageBox.Show("Is that the correct user? \n" + user3.User_name + " " + user3.User_phone, "User Found", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.No)
                {
                    MessageBox.Show("Try Again.", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                    user3.User_name       = String.Empty;
                    user3.User_phone      = String.Empty;
                    phone_inp_txtBox.Text = String.Empty;
                    name_inp_txtBox.Text  = String.Empty;
                }

                if (result == MessageBoxResult.Yes)
                {
                    findUser_btn.IsEnabled    = false;
                    select_cmBox.IsEnabled    = false;
                    DatePicker.IsEnabled      = true;
                    hour_text_box.IsEnabled   = true;
                    minute_text_box.IsEnabled = true;
                    generate_btn.IsEnabled    = true;
                }
                break;
            }
        }
コード例 #2
0
        //button for user 2, same code, different variables, considered functions, but could find how to make them with objects
        private void FindUser_btn_2_Click(object sender, RoutedEventArgs e)
        {
            string error = "Error 1! Record Not Found. Please try again."; //find csv function returns the same error

            switch (select_cmBox_2.SelectedIndex)
            {
            case 0:     ///phone number
                if (phone_inp_txtBox_2 != null && !string.IsNullOrWhiteSpace(phone_inp_txtBox_2.Text))
                {
                    string[] name_phone = CsvIO.findCsvLine(phone_inp_txtBox_2.Text, 2, "users.csv");
                    if (name_phone[0] == error)     //if error is returned
                    {
                        goto case 4;
                    }
                    user2.User_name  = name_phone[1];
                    user2.User_phone = name_phone[2];
                    goto case 5;
                }
                else
                {
                    goto case 3;
                }

            case 1:     //name
                if (name_inp_txtBox_2 != null && !string.IsNullOrWhiteSpace(name_inp_txtBox_2.Text))
                {
                    string[] name_phone = CsvIO.findCsvLine(name_inp_txtBox_2.Text, 1, "users.csv");
                    if (name_phone[0] == error)
                    {
                        goto case 4;
                    }
                    user2.User_name  = name_phone[1];
                    user2.User_phone = name_phone[2];
                    goto case 5;
                }
                else
                {
                    goto case 3;
                }

            case 3:     //input empty
                MessageBox.Show("Please input search term!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                break;

            case 4:     //user not found
                MessageBox.Show("User not found in the database! Please try again!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                user2.User_name  = String.Empty;
                user2.User_phone = String.Empty;
                break;

            case 5:     //final check
                MessageBoxResult result = MessageBox.Show("Is that the correct user? \n" + user2.User_name + " " + user2.User_phone, "User Found", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.No)
                {
                    MessageBox.Show("Try Again.", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                    user2.User_name         = String.Empty;
                    user2.User_phone        = String.Empty;
                    phone_inp_txtBox_2.Text = String.Empty;
                    name_inp_txtBox_2.Text  = String.Empty;
                }

                if (result == MessageBoxResult.Yes)
                {
                    findUser_btn_2.IsEnabled = false;
                    save_btn.IsEnabled       = true;
                    select_cmBox.IsEnabled   = false;
                    DatePicker.IsEnabled     = true;
                    Date_input_lbl.IsEnabled = true;
                    Time_pick_lbl.IsEnabled  = true;
                }
                break;
            }
        }
コード例 #3
0
        private void Save_btn_Click(object sender, RoutedEventArgs e)
        {
            int caseSwitch = 1;

            switch (caseSwitch)
            {
            case 1:     //check for null and existing
                if (string.IsNullOrWhiteSpace(loc_srch_TextBox.Text))
                {
                    MessageBox.Show("Error! Location not selected. Try again!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                }

                if (DatePicker.SelectedDate == null)
                {
                    MessageBox.Show("Error! Date not selected. Try again!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                }

                if (hour_text_box.Text == "Hour" || minute_text_box.Text == "Minute" || string.IsNullOrWhiteSpace(minute_text_box.Text) || string.IsNullOrWhiteSpace(hour_text_box.Text))
                {
                    MessageBox.Show("Error! Time not selected. Try Again!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                }



                else
                {
                    goto case 5;     //check time first
                }


            case 2:     //location input
                string[] found_line = CsvIO.findCsvLine(loc_srch_TextBox.Text, 1, "locations.csv");
                if (found_line[0] == "Error 1! Record Not Found. Please try again.")
                {
                    MessageBoxResult result_loc = MessageBox.Show(loc_srch_TextBox.Text + " location not found in the database! Do you want to add as new location?", "Instructions", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result_loc == MessageBoxResult.Yes)
                    {
                        CsvIO.importCSV("locations.csv", "id,locationName", loc_srch_TextBox.Text);
                        MessageBox.Show("Location: " + loc_srch_TextBox.Text + "\n Added successful!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                        string[] newloc = CsvIO.findCsvLine(loc_srch_TextBox.Text, 1, "locations.csv");
                        visit1.Event_id            = newloc[0];
                        visit1.Visit_location_name = newloc[1];
                        goto case 4;
                    }
                    else
                    {
                        MessageBox.Show("Input existing location.", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                        loc_srch_TextBox.Text = String.Empty;
                        break;
                    }
                }
                else
                {
                    visit1.Event_id            = found_line[0];
                    visit1.Visit_location_name = found_line[1];
                    goto case 4;
                }

            case 3:    //date check
                string           date   = DatePicker.SelectedDate.Value.ToString("MM/dd/yyyy");
                MessageBoxResult result = MessageBox.Show("Is that the correct date? \n" + date, "Date Validation", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    visit1.Event_date = date;
                    //Visit.addToCsvVisit(visit1.User_name, visit1.User_phone, visit1.Visit_location, visit1.Visit_date);
                    goto case 2;
                }
                else
                {
                    MessageBox.Show("Please try again!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                break;

            case 4:     //correct location check
                MessageBoxResult result2 = MessageBox.Show("Is that the correct location? \n" + visit1.Visit_location_name, "Location Found", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result2 == MessageBoxResult.Yes)
                {
                    string header = "VisitId,UserName,UserPhone,LocationID,LocationName,Date,Time";
                    string toCsv  = visit1.User_name + "," + visit1.User_phone + "," + visit1.Event_id + "," + visit1.Visit_location_name + "," + visit1.Event_date + "," + visit1.Event_time;
                    CsvIO.importCSV("visit.csv", header, toCsv);
                    MessageBox.Show("Visit added successful!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                }
                else
                {
                    MessageBox.Show("Try Again.", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                    loc_srch_TextBox.Text = String.Empty;
                    break;
                }

            case 5:     //Time validation
                bool success1 = Int32.TryParse(hour_text_box.Text, out int hours);
                bool success2 = Int32.TryParse(minute_text_box.Text, out int minutes);

                if (success1 && success2 && hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59)
                {
                    visit1.Event_time = hours + ":" + minutes;

                    if (minutes < 10)
                    {
                        visit1.Event_time = String.Empty;
                        string minutes_0 = string.Format("0{0}", minutes);
                        visit1.Event_time = hours + ":" + minutes_0;
                    }

                    MessageBoxResult result_time = MessageBox.Show("Is that the correct Time? \n" + visit1.Event_time, "Time Validation", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result_time == MessageBoxResult.Yes)
                    {
                        goto case 3;
                    }
                    else
                    {
                        visit1.Event_time = String.Empty;
                        MessageBox.Show("Please input time again!", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                        break;
                    }
                }
                else
                {
                    MessageBox.Show("Invalid Time Input! Please Try Again! ", "Instructions", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                }
            }
        }