//This event will capture the weather forecast
        private void btnCapture_Click(object sender, EventArgs e)
        {
            JSONconnection js = new JSONconnection(); //create an object of the JSONconnection class in order to manipulate the JSON file

            WeatherModel w = new WeatherModel();      // create a weather object that will capture all the weather information

            weath = js.readWeatherFile();             // read all the values in the weather file into a list

            bool valid = false;

            if (cboCity1.SelectedIndex == -1)                            // only enter if an item has been selected
            {
                captureError.SetError(cboCity1, "Please select a city"); // if an item has not been selected display an error message
            }
            else
            {
                //check for errors first

                if (errorCheck(txtMax) == false) // check if the control is a numeric value and check if it is not empty
                {
                    valid = false;               //if there is an error end the event here and return till the error is cleared
                    return;
                }


                if (errorCheck(txtMin) == false)
                {
                    valid = false;
                    return;
                }

                if (Convert.ToDouble(txtMin) > Convert.ToDouble(txtMax)) // ensure that the minimum temperature is not greater than the maximum
                {
                    captureError.SetError(txtMin, "The minimum temperature cannot be greater than the maximum temperature");
                    valid = false;
                    return;
                }

                if (errorCheck(txtHumid) == false)
                {
                    valid = false;
                    return;
                }

                if (errorCheck(txtMin) == false)
                {
                    valid = false;
                    return;
                }

                if (errorCheck(txtWind) == false)
                {
                    valid = false;
                    return;
                }

                //check the combobox controls for errors
                if (cboCloud.SelectedIndex == -1)
                {
                    captureError.SetError(cboCloud, "Please select the cloud cover");
                    valid = false;
                    return;
                }
                else
                {
                    captureError.Clear(); //clear any errors that were created
                }
                if (cboUvIndex.SelectedIndex == -1)
                {
                    captureError.SetError(cboUvIndex, "Please select the UV index");
                    valid = false;
                    return;
                }
                else
                {
                    captureError.Clear(); //clear any errors that were created
                    valid = true;
                }


                if (valid)
                {
                    captureError.Clear(); //clear any previous errors
                    foreach (WeatherModel v in weath)
                    {
                        //check if user wants to overwrite or continue with the operation

                        if (v.City.Equals(cboCity1.Text) && v.Date == dtpDate.Value.Date) //check if there is already a value with the same date
                        {
                            //if a value already exits for the this city and date, ask the user if they would like to overide
                            DialogResult dr = MessageBox.Show("Values already exits for this city on this day. Would you like to overwrite?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                            if (dr == DialogResult.Yes)
                            {
                                //Add all the values to the weather object
                                w.City          = cboCity1.Text;
                                w.Date          = dtpDate.Value.Date;
                                w.Max           = Convert.ToDouble(txtMax.Text);
                                w.Min           = Convert.ToDouble(txtMin.Text);
                                w.Humidity      = Convert.ToDouble(txtHumid.Text);
                                w.Precipitation = Convert.ToDouble(txtPrecip.Text);
                                w.Wind          = Convert.ToDouble(txtWind.Text);
                                w.CloudCover    = cboCloud.SelectedIndex;
                                w.UvIndex       = cboUvIndex.Text;
                                if (radCelsius.Checked == true)
                                {
                                    w.IsCelsius = true;
                                }
                                else
                                {
                                    w.IsCelsius = false;
                                }

                                if (js.overwriteFile(w)) //overwrite the existing value if the user has chosen to update
                                {
                                    MessageBox.Show("Data overwritten successfully");
                                    cboCity1.SelectedIndex = -1;
                                    txtMax.Clear();
                                    txtMin.Clear();
                                    txtHumid.Clear();
                                    txtPrecip.Clear();
                                    txtWind.Clear();
                                    cboCloud.SelectedIndex   = -1;
                                    cboUvIndex.SelectedIndex = -1;
                                }
                                else
                                {
                                    MessageBox.Show("There was an error with the request", "Overwrite error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }

                                valid = false;
                                break; //break the foreach
                            }
                            else
                            {
                                //if the user doesnt want to overwrite, clear all the fields
                                cboCity1.SelectedIndex = -1;
                                txtMax.Clear();
                                txtMin.Clear();
                                txtHumid.Clear();
                                txtPrecip.Clear();
                                txtWind.Clear();
                                cboCloud.SelectedIndex   = -1;
                                cboUvIndex.SelectedIndex = -1;
                                valid = false; // set  valid to false to end this event
                                break;         //break the foreach
                            }
                        }
                    }
                    //continue if there were no errors and no overwrite request
                    if (valid)
                    {
                        captureError.Clear(); //clear any previous errors

                        w.City          = cboCity1.Text;
                        w.Date          = Convert.ToDateTime(dtpDate.Value.ToShortDateString()); //input the date
                        w.Max           = Convert.ToDouble(txtMax.Text);                         //convert and input the max value
                        w.Min           = Convert.ToDouble(txtMin.Text);                         //convert and input
                        w.Humidity      = Convert.ToDouble(txtHumid.Text);                       //convert and input the data
                        w.Precipitation = Convert.ToDouble(txtPrecip.Text);                      //convert and input
                        w.Wind          = Convert.ToDouble(txtWind.Text);                        //convert the value and input it
                        w.CloudCover    = cboCloud.SelectedIndex;                                //convert the value and input it
                        w.UvIndex       = cboUvIndex.Text;

                        if (radCelsius.Checked == true)
                        {
                            w.IsCelsius = true;
                        }
                        else
                        {
                            w.IsCelsius = false;
                        }

                        weath.Add(w); // add the object to the list

                        DialogResult dg = MessageBox.Show("Would you like to add more values", "Enter More", MessageBoxButtons.YesNo);
                        if (dg == DialogResult.Yes)
                        {
                            //reset all the controls and return
                            cboCity1.SelectedIndex = -1;
                            txtMax.Clear();
                            txtMin.Clear();
                            txtHumid.Clear();
                            txtPrecip.Clear();
                            txtWind.Clear();
                            cboCloud.SelectedIndex   = -1;
                            cboUvIndex.SelectedIndex = -1;
                            return;
                        }

                        else
                        {
                            js.writeWeatherFile(weath);

                            cboCity1.SelectedIndex = -1;
                            txtMax.Clear();
                            txtMin.Clear();
                            txtHumid.Clear();
                            txtPrecip.Clear();
                            cboCloud.SelectedIndex   = -1;
                            cboUvIndex.SelectedIndex = -1;
                            SetAcivePanel(pnlHome);
                        }
                    }
                }
            }
        }
예제 #2
0
        //end of helper methods
        //--------------------------------

        //start of events
        //-----------------------------

        //this method will overwrite the data stored in the file with new values
        private void btnCapture_Click(object sender, EventArgs e)
        {
            WeatherModel weather = new WeatherModel();
            bool         valid   = true;

            weath = js.readWeatherFile();

            //check for errors
            #region
            if (errorCheck(txtMax) == false) // check if the control is a numeric value and check if it is not empty
            {
                valid = false;
                return;
            }


            if (errorCheck(txtMin) == false)
            {
                valid = false;
                return;
            }


            if (errorCheck(txtHumid) == false)
            {
                valid = false;
                return;
            }

            if (errorCheck(txtMin) == false)
            {
                valid = false;
                return;
            }

            if (errorCheck(txtWind) == false)
            {
                valid = false;
                return;
            }

            if (cboCloud.SelectedIndex == -1)
            {
                captureError.SetError(cboCloud, "Please select the cloud cover");
                valid = false;
            }

            if (cboUvIndex.SelectedIndex == -1)
            {
                captureError.SetError(cboUvIndex, "Please select the UV index");
                valid = false;
            }
            #endregion

            if (valid)
            {
                captureError.Clear();
                foreach (WeatherModel item in weath)
                {
                    if (item.City.Equals(cboCity.Text) && item.Date == dtpDate.Value.Date) //check if there is already a value with the same date
                    {
                        //if a value already exits for the this city and date, ask the user if they would like to overide
                        DialogResult dr = MessageBox.Show("are you sure you would like to overwrite the values?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                        if (dr == DialogResult.Yes)
                        {
                            weather.City          = cboCity.Text;
                            weather.Date          = dtpDate.Value.Date;
                            weather.Max           = Convert.ToDouble(txtMax.Text);
                            weather.Min           = Convert.ToDouble(txtMin.Text);
                            weather.Humidity      = Convert.ToDouble(txtHumid.Text);
                            weather.Precipitation = Convert.ToDouble(txtPrecip.Text);
                            weather.Wind          = Convert.ToDouble(txtWind.Text);
                            weather.CloudCover    = cboCloud.SelectedIndex;
                            weather.UvIndex       = cboUvIndex.Text;


                            if (js.overwriteFile(weather))
                            {
                                MessageBox.Show("Data overwritten successfully");
                                ForecasterMenu fm = new ForecasterMenu(fUser);
                                this.Hide();
                                fm.Show();
                            }
                            else
                            {
                                MessageBox.Show("There was an error with the request", "Overwrite error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                break;
                            }
                        }
                        else
                        {
                            MessageBox.Show("Operation cancelled", "Overwrite cancelled", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            cboCity.SelectedIndex = -1;
                            txtMax.Clear();
                            txtMin.Clear();
                            txtHumid.Clear();
                            txtPrecip.Clear();
                            txtWind.Clear();
                            cboCloud.SelectedIndex   = -1;
                            cboUvIndex.SelectedIndex = -1;
                        }
                    }
                }
            }
        }