///load cities into the single city range combo box private void loadCities() { string city = ""; //variable to hold the current city JSONconnection js = new JSONconnection(); //Instance of the JSONconnection class to use the JSON methods List <WeatherModel> weath = new List <WeatherModel>(); //List to store all the weather data ArrayList cities = new ArrayList(); //Arraylist to hold the cities weath = js.readWeatherFile(); //use the json method to get all the weather data and store it in the list foreach (WeatherModel vals in weath) //cycle through list to find all the cities { string store = vals.City; cities.Add(store); } cities.Sort(); // sort the list in alphabetical order so that you can extract each city and not repeat a city foreach (string item in cities) { if (!city.Equals(item)) { city = item; cboCity2.Items.Add(city); //add the items to the combo box } } }
//Start of helper methods //-------------------------- //This method will get all the weather data from the file private List <WeatherModel> getWeatherData() { JSONconnection js = new JSONconnection(); //create an instance of the weather class List <WeatherModel> list = new List <WeatherModel>(); // to store the weather data list = js.readWeatherFile(); //retrieve all the weather data and store inside the list foreach (WeatherModel item in list) { item.convertUnit(fUser.IsCelsius); //Convert the data to the user's preferred temperature unit } return(list); //return the weather data }
//start of hellper methods //------------------------- private List <WeatherModel> getWeatherData() { JSONconnection js = new JSONconnection(); List <WeatherModel> list = new List <WeatherModel>(); list = js.readWeatherFile(); foreach (WeatherModel item in list) { item.convertUnit(fUser.IsCelsius); } return(list); }
//start of helper methods //--------------------------- private void loadComboBox() { List <string> cities = new List <string>(); string city = ""; weath = js.readWeatherFile(); foreach (WeatherModel item in weath) { cities.Add(item.City); } cities.Sort(); foreach (string ci in cities) { if (!city.Equals(ci)) { cboCity.Items.Add(ci); cboDeleteCity.Items.Add(ci); city = ci; } } }
//method to set the display according to the users preffered city private void PreferedData(UserModel gUser) { weath = js.readWeatherFile(); //Get all the forecasts and store them in a list of weather models string city; double min; double max; double precip; double humidity; int cloud; string uv; double wind; string userCity = ""; userCity = gUser._homeCity; //Store the user's city foreach (WeatherModel w in weath) { if (w.City.Equals(userCity) && w.Date == DateTime.Today) // enter if the user's city corresponds with weather model { w.convertUnit(gUser.IsCelsius); // Convert the temperature unit to user's preferrence //store the corresponding data city = w.City; max = w.Max; min = w.Min; precip = w.Precipitation; humidity = w.Humidity; cloud = w.CloudCover; wind = w.Wind; uv = w.UvIndex; //display the data on form lblCity.Text = "Weather in " + city; lblMax.Text = Convert.ToString(max) + "°" + unit; lblMin.Text = Convert.ToString(min) + "°" + unit; lblPrecipitaion.Text = Convert.ToString(precip) + "%"; lblHumidity.Text = Convert.ToString(humidity) + "%"; lblWindSpeed.Text = Convert.ToString(wind) + "km/h"; lblUvIndex.Text = uv; //determine which cloud icon to display in the picture boxes if (cloud == 1) { pBoxSunny.Visible = true; pBoxPartlyCloudy.Visible = false; pBoxCloudy.Visible = false; pBoxStormy.Visible = false; lblCloudCover.Text = "Sunny"; } else if (cloud == 2) { pBoxSunny.Visible = false; pBoxPartlyCloudy.Visible = true; pBoxCloudy.Visible = false; pBoxStormy.Visible = false; lblCloudCover.Text = "Partly Cloud"; } else if (cloud == 3) { pBoxSunny.Visible = false; pBoxPartlyCloudy.Visible = false; pBoxCloudy.Visible = true; pBoxStormy.Visible = false; lblCloudCover.Text = "Full CLoud Cover"; } else if (cloud == 4) { pBoxSunny.Visible = false; pBoxPartlyCloudy.Visible = false; pBoxCloudy.Visible = false; pBoxStormy.Visible = true; lblCloudCover.Text = "Thunder Showers"; } } } }
//Begin helper methods //--------------------------------- //method to set information according to the users preffered city public void PreferedData(UserModel gUser, bool nextCity = false) { JSONconnection js = new JSONconnection(); //Create a connection to the JSON class weath = js.readWeatherFile(); //Get all the forecasts and store them in a list of weather models if (weath != null) //if the weather file is not empty attempt to populate the user's preffered information { if (gUser != null) //If the user is not found is not a null value attempt to populate the display with correct info { // Variable to store the weatehr values string city; double min; double max; double precip; double humidity; int cloud; string userCity = ""; if (nextCity == false) //If the user wants to see thy're home city { userCity = gUser._homeCity; } else //If the user has not specified that they want to see the next stored city { foreach (string item in gUser.prefferedCities) { if (!currentCity.Equals(item) && !previousCity.Equals(item)) { userCity = item;//store the user's city as the next extra city previousCity = currentCity; currentCity = item; break; } } } //Check the user's preferred temperature unit if (gUser.IsCelsius) { unit = "C"; } else { unit = "F"; } foreach (WeatherModel w in weath) { if (w.City.Equals(userCity) && w.Date == DateTime.Today) { w.convertUnit(gUser.IsCelsius); //Convert the unit of emasure if they user's and the weather file do not correspond city = w.City; max = w.Max; min = w.Min; precip = w.Precipitation; humidity = w.Humidity; cloud = w.CloudCover; //set the display based on the corresponding values found in the weather file lblCity.Text = "Weather in " + city + " today"; lblMax.Text = Convert.ToString(max) + "°" + unit; lblMin.Text = Convert.ToString(min) + "°" + unit; lblPrecip.Text = Convert.ToString(precip) + "%"; lblHumidity.Text = Convert.ToString(humidity) + "%"; //set the picture box based on the value that i9s found in the weather file if (cloud == 1) { pBoxSunny.Visible = true; pBoxPartlyCloudy.Visible = false; pBoxCloudy.Visible = false; pBoxStormy.Visible = false; lblCloudCover.Text = "Sunny"; } else if (cloud == 2) { pBoxSunny.Visible = false; pBoxPartlyCloudy.Visible = true; pBoxCloudy.Visible = false; pBoxStormy.Visible = false; lblCloudCover.Text = "Partly Cloud"; } else if (cloud == 3) { pBoxSunny.Visible = false; pBoxPartlyCloudy.Visible = false; pBoxCloudy.Visible = true; pBoxStormy.Visible = false; lblCloudCover.Text = "Full Cloud Cover"; } else if (cloud == 4) { pBoxSunny.Visible = false; pBoxPartlyCloudy.Visible = false; pBoxCloudy.Visible = false; pBoxStormy.Visible = true; lblCloudCover.Text = "Thunder Showers"; } } } } } else //If there is no weather data send the user back to the landing page { MessageBox.Show("There is no weather data to display. Please create weather data if you are a forecaster. If you are not please contact/" + "send an email to the developer", "Weather Data error", MessageBoxButtons.OK, MessageBoxIcon.Error); Landing ld = new Landing(); ld.Show(); this.Close(); } }
//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); } } } } }