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 }
//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); } } } } }