Exemple #1
0
        /// <summary>
        /// This method deletes the selected weathe object from the file
        /// </summary>
        /// <param name="weather">Accepts the weather object to delete</param>
        /// <returns>True if object if the object was removed</returns>
        public bool deleteWeather(WeatherModel weather)
        {
            string conv  = File.ReadAllText(weatherPath);
            string write = string.Empty;

            List <WeatherModel> oldList = new List <WeatherModel>();

            oldList = JsonConvert.DeserializeObject <List <WeatherModel> >(conv);

            foreach (WeatherModel item in oldList)
            {
                if (item.City.Equals(weather.City) && item.Date == weather.Date)
                {
                    oldList.Remove(item);
                    break;
                }
            }

            try
            {
                write = JsonConvert.SerializeObject(oldList);
                File.WriteAllText(weatherPath, write);
                MessageBox.Show("Data deleted");
                return(true);
            }
            catch
            {
                MessageBox.Show("Could not delete the data");
                return(false);
            }
        }
Exemple #2
0
        JSONconnection js         = new JSONconnection();      //Create an instance f the JSON class to use the JSON methods
        //----------------------------

        //Begin helper methods
        //-------------------------------

        //Method to create the test data
        private void SetTestValues()
        {
            //This is only to recreate the test data
            //--------------------
            File.Delete("weatherdata.json");
            js.createFiles();
            //-------------------


            DateTime d1 = DateTime.Today;
            DateTime d2 = DateTime.Today.AddDays(1);
            DateTime d3 = DateTime.Today.AddDays(2);
            DateTime d4 = DateTime.Today.AddDays(3);
            DateTime d5 = DateTime.Today.AddDays(4);


            string[]   cit        = { "Johannesburg", "Pretoria", "Durban", "Polokwane", "Cape Town", "Johannesburg", "Pretoria", "Durban", "Polokwane", "Cape Town", "Johannesburg", "Pretoria", "Durban", "Polokwane", "Cape Town", "Johannesburg", "Pretoria", "Durban", "Polokwane", "Cape Town", "Johannesburg", "Pretoria", "Durban", "Polokwane" };
            DateTime[] dates      = { d1, d1, d1, d1, d1, d2, d2, d2, d2, d2, d3, d3, d3, d3, d3, d4, d4, d4, d4, d4, d5, d5, d5, d5, d5 };
            double[]   maxes      = { 25, 27, 30, 32, 24, 21, 23, 33, 20, 20, 28, 29, 31, 19, 17, 34, 22, 37, 29, 26, 23, 19, 10, 32, 24 };
            double[]   mins       = { 18, 17, 20, 12, 14, 12, 11, 10, 17, 19, 10, 9, 20, 21, 13, 14, 19, 22, 23, 18, 19, 7, 2, 1, 4 };
            double[]   humids     = { 10, 7, 3, 32, 4, 1, 27, 33, 12, 44, 18, 17, 33, 92, 24, 30, 27, 13, 52, 84, 40, 8, 33, 22, 64 };
            double[]   precips    = { 23, 38, 2, 4, 10, 6, 76, 43, 52, 44, 20, 34, 24, 55, 24, 14, 56, 25, 32, 46, 67, 35, 35, 32, 4 };
            double[]   winds      = { 32, 47, 11, 51, 13, 30, 7, 3, 35, 23, 10, 17, 11, 45, 43, 60, 37, 41, 23, 32, 33, 77, 91, 65, 3 };
            int[]      CloudCover = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2 };
            string[]   uvIndex    = { "Low", "High", "Medium", "High", "Medium", "Low", "High", "Medium", "High", "Medium", "Low", "High", "Medium", "High", "Medium", "Low", "High", "Medium", "High", "Medium", "Low", "High", "Medium", "High", "Medium" };

            for (int index = 0; index < cit.Length; index++)
            {
                WeatherModel hu = new WeatherModel();
                hu.City          = cit[index];
                hu.Date          = dates[index];
                hu.Max           = maxes[index];
                hu.Min           = mins[index];
                hu.Precipitation = precips[index];
                hu.Humidity      = humids[index];
                hu.Wind          = winds[index];
                hu.CloudCover    = CloudCover[index];
                hu.UvIndex       = uvIndex[index];
                hu.IsCelsius     = true;
                weath.Add(hu);
            }
            js.writeWeatherFile(weath); //write the test values to the file
        }
Exemple #3
0
        /// <summary>
        /// //This method will overwrite the existing weather data
        /// </summary>
        /// <param name="obj"></param>
        public bool overwriteFile(WeatherModel obj)
        {
            string conv  = File.ReadAllText(weatherPath);//read all data from the file
            string write = string.Empty;

            List <WeatherModel> List = new List <WeatherModel>();

            List = JsonConvert.DeserializeObject <List <WeatherModel> >(conv);

            foreach (WeatherModel item in List)
            {
                if (item.City.Equals(obj.City) && item.Date == obj.Date)
                {
                    item.Max           = obj.Max;
                    item.Min           = obj.Min;
                    item.Humidity      = obj.Humidity;
                    item.Precipitation = obj.Precipitation;
                    item.UvIndex       = obj.UvIndex;
                    item.Wind          = obj.Wind;
                    item.CloudCover    = obj.CloudCover;
                    item.IsCelsius     = obj.IsCelsius;
                }
            }

            try
            {
                write = JsonConvert.SerializeObject(List);
                File.WriteAllText(weatherPath, write);
                return(true);
            }
            catch
            {
                MessageBox.Show("Could not overwrite the data");
                return(false);
            }
        }
        //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);
                        }
                    }
                }
            }
        }
        //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;
                        }
                    }
                }
            }
        }