///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 } } }
// Change the user's preferred temp unit then close this form private void btnApply_Click(object sender, EventArgs e) { JSONconnection js = new JSONconnection(); //Instance of the JSONconnection class to user JSON methods if (radCelsius.Checked == true) //if the celsius button is checked change the user's preference to celsius { fUser.IsCelsius = true; } else //if the fahrenheit button is checked change the user's preferrence to fahrenheit { fUser.IsCelsius = false; } if (js.changeUserPreference(fUser)) //Save this changes to the file { MessageBox.Show("User preference successfully changed"); ForecasterMenu fm = new ForecasterMenu(fUser); this.Hide(); fm.Show(); } else { MessageBox.Show("User preference could not be changed, please try again", "Change error"); } }
//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); }
//Method to handle the user login private void btnSignIn_Click_1(object sender, EventArgs e) { JSONconnection js = new JSONconnection(); //Onstance of the JSONConnection class to use the JSON methods UserModel user = new UserModel(); //User model that will be used to store the found user user = js.login(txtUsername.Text, txtPassword.Text); //Capture the username and password from the textboxes and pass them into the JSON login method if (user.FName != null) //If there there was a user found on the system { if (user.FName.Equals("no users available")) //This will determine if there are no users registered on the system { DialogResult dr = MessageBox.Show("There are no users registered on the system. would you like to sign up?", "No Users", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dr == DialogResult.Yes) //Send the user to the sign up page { signup sg = new signup(); sg.Show(); this.Hide(); } } else if (user.Type.Equals("general user")) //If the user found is a general user open the general user side of the application { Dashboard ds = new Dashboard(user); this.Hide(); ds.Show(); } else //If the user found is a forecaster open the Forecaster side of the application { ForecasterMenu fm = new ForecasterMenu(user); this.Hide(); fm.Show(); } } else // if the user credentials are incorrect display an error message and clear all fields { MessageBox.Show("User could not be found... Please check your credentials"); txtUsername.Clear(); txtPassword.Clear(); } }
//Helper methods end //--------------------------------- //Start events //--------------------------------- //Event that change the user's password private void btnChangeLogin_Click(object sender, EventArgs e) { string password = txtPassword.Text; //String to store the password entered in the textbox string username = gUser.UserName; //String to store the username of the user logged in //Perform error check on the text box if (string.IsNullOrEmpty(txtPassword.Text)) //check if the field is empty { MessageBox.Show("Please enter a password first", "Password empty", MessageBoxButtons.OK, MessageBoxIcon.Error); } else //If there are no error { JSONconnection js = new JSONconnection(); // create an instance of JSON class to use JSON methods if (js.changePassword(username, password)) // call the change username password, if trure is returned the password has been successfuly changed { MessageBox.Show("Password successfully changed"); } else //The password could not be changed { MessageBox.Show("Password change unsuccessful. Please try again", "Password change error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
//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); } } } } }
}; //This list will feed the combo box and be altered as the users enters more cities //end of globals //-------------------------- //Begin events //------------------------------- //Begin sign up event private void btnSignUp_Click(object sender, EventArgs e) { //Error check begin if (errorCheck(txtFname)) { if (errorCheck(txtSname)) { if (errorCheck(txtUsername)) { if (cboCity.SelectedIndex != -1) { if (cboUserType.SelectedIndex != -1) //Error check end { JSONconnection js = new JSONconnection(); //Create instance of the JSON class to use the JSON methods //Begin collecting the user info from the textboxes,combo boxes and radio buttons string username = txtUsername.Text; string fName = txtFname.Text; string sName = txtSname.Text; string pass = txtPassword.Text; string homeCity = cboCity.Text; int userType = cboUserType.SelectedIndex; bool isCelsius; if (radCelsius.Checked == true) { isCelsius = true; } else { isCelsius = false; } //End of collection of user information if (js.signUp(userType, username, fName, sName, pass, isCelsius, homeCity, cities)) //Send all the collected information to the sign up method, return true if the user was successfully signed up { MessageBox.Show(fName + " " + sName + " has been added as " + username); //Tell the user that the sign up was successful login lg = new login(); //Open the login page so that they can now login this.Hide(); lg.Show(); } else { MessageBox.Show("User Could not be added... Please try again"); //Let the uwser that there was a problem with the registration, The error has already been displayed to them //Clear all fields txtFname.Clear(); txtSname.Clear(); txtPassword.Clear(); txtUsername.Clear(); lstboxCities.Items.Clear(); cboCity.SelectedIndex = -1; cboUserType.SelectedIndex = -1; cboExtraCity.SelectedIndex = -1; } } else { captureError.SetError(cboCity, "Please select a user type"); //Display error message if the user type was not selected } } else { captureError.SetError(cboCity, "Please select a city"); //Display error message if the home town was not selected } } } } }