0.5 First Day Assignment [1 hour 15 minutes]

Overview

Acceptance Criteria

  • The existing student, breakfast, backpack, and clothing controls still work.
  • Save Shopping List and Load Shopping List still work as they did in 0.4.
  • The new weather panel appears between the existing Weather controls and school shopping list.
  • Clicking Load weather requests weather for the entered coordinates and displays the returned JSON forecast in the browser area.
  • A failed weather request displays an error message in the browser area.
  • The completed solution is submitted through the Feedback System, and its URL is submitted to Canvas.
Instructions
1.Open Your Existing Solution [10 minutes]

Continue your completed 0.4 First Day application. Keep the shopping-list save and load features, extend the window layout, and add a button that retrieves weather using longitude and latitude.

Goal: Request information from an online service and display the result in your application.

A Back up and open the solution
1 Close Visual Studio if the solution is open
2 Back up the completed 0.4 solution folder

Name the backup First Day 0.4 Backup LastName.

3 Open LastName.sln from your original working folder

Keep the working folder and solution names unchanged.

4 Run the application and verify the existing controls work

Include Save Shopping List and Load Shopping List. Fix any existing errors before continuing.

5 Close the application before editing
2.Explore the Weather Service [10 minutes]
A Explore the Open-Meteo documentation
1 Open the Open-Meteo website in your regular web browser
https://open-meteo.com/en/docs

Open-Meteo provides weather data through an API: an interface that lets an application request information from an online service. Its documentation lets you explore locations, weather variables, and the URL used to request data.

2 Find the latitude and longitude fields

Latitude describes a location north or south of the equator. Longitude describes a location east or west of the prime meridian. Negative values represent south or west. Our window places longitude first, so read each label carefully.

3 Explore the available weather variables

Explore the daily precipitation variables. This assignment requests one day of precipitation hours, maximum precipitation probability, and totals for precipitation, showers, rain, and snowfall. The URL also specifies unit preferences. Read the returned daily_units values to identify the units for each result. The service returns JSON: text containing named values and lists. Your application will display that response directly.

3.Add the Grid Row 2 Weather Panel [20 minutes]
A Add the weather panel
1 Open FirstDayScenario/MainWindow.xaml

Switch to the XAML source view.

2 Find the gap between the existing Grid row 1 and Grid row 3 panels

In MainWindow.xaml, locate the outer Weather StackPanel whose opening tag contains Grid.Row="1". Find its matching closing tag, after the rain checkbox and clothing button. The shopping-list StackPanel with Grid.Row="3" follows it.

3 Add the Grid row 2 panel between those existing panels

Insert the following entire block immediately after the outer Weather panel's closing tag and before the shopping-list panel's opening tag. The new StackPanel must be a direct child of the existing Grid, alongside those two panels. The Grid already has a row definition for row 2, so you do not need to add or change any row definitions.

MainWindow.xaml — add the Grid row 2 panel
<StackPanel Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Top">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Longitude:" Width="125" Margin="5,5,5,0"/>
        <TextBlock Text="Latitude:" Width="125" Margin="5,5,5,0"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBox x:Name="longitudeTextBox" Width="125" Margin="5"/>
        <TextBox x:Name="latitudeTextBox" Width="125" Margin="5"/>
        <Button x:Name="loadWeatherButton" HorizontalAlignment="Left" Content="Load weather" Width="125" Margin="5" Click="loadWeatherButton_Click" />
    </StackPanel>
    <Border Margin="5" Height="125" BorderThickness="1" BorderBrush="Black">
        <WebBrowser x:Name="BrowserControl"/>
    </Border>
</StackPanel>
Shows the Grid row 2 weather panel with longitude and latitude inputs, Load weather button, and browser area.
Add the Grid row 2 panel

The new panel occupies Grid row 2. Its first horizontal StackPanel holds the coordinate labels; its second holds the matching text boxes and Load weather button. The Border contains the browser that will display the response.

4 Save MainWindow.xaml

The new loadWeatherButton_Click method has not been created yet. Complete the next task before building or running the application.

B Check the existing event-handler connections
1 Check that the existing buttons remain connected to their 0.4 handlers

Keep the existing newStudentButton_Click, haveBreakfastButton_Click, fillBackpackButton_Click, chooseClothingButton_Click, saveShoppingListButton_Click, and loadShoppingListButton_Click methods and their implementations. Each XAML Click value must match its C# method name exactly, including capitalization.

4.Load Weather and Check the Application [25 minutes]
A Create the Load weather event handler
1 Double-click the Load weather button in the XAML designer

Open the Design view of MainWindow.xaml and double-click Load weather. Visual Studio creates the Click event handler and opens MainWindow.xaml.cs. The method must be named loadWeatherButton_Click, matching the supplied XAML exactly. If the method already exists, double-clicking opens it; do not add a duplicate.

The supplied code uses fully qualified type names. Keep your existing using statements, including System.IO from 0.4. No additional using statement or JSON parsing package is needed.

2 Add the HTTP client field inside the MainWindow class

Place this field immediately after the existing Student field and before the constructor. It belongs inside the MainWindow class, outside all methods. Keep the existing Breakfast and Student fields and Feedback attributes.

MainWindow.xaml.cs — add below the Student field
private static readonly System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient();

This client sends requests to the weather service and is reused each time you click Load weather.

3 Add GetForecastAsync after the weather handler

Paste this complete method after the weather event handler and before the closing brace of the MainWindow class. Do not place it inside the constructor or another method.

MainWindow.xaml.cs — add GetForecastAsync
private async System.Threading.Tasks.Task<string> GetForecastAsync()
{
    string url = $"https://api.open-meteo.com/v1/forecast?latitude={this.latitudeTextBox.Text}&longitude={this.longitudeTextBox.Text}&daily=precipitation_hours,precipitation_probability_max,precipitation_sum,showers_sum,rain_sum,snowfall_sum&forecast_days=1&wind_speed_unit=mph&temperature_unit=fahrenheit&precipitation_unit=inch";

    return await Client.GetStringAsync(url);
}

The URL uses the text from the two coordinate TextBoxes. The latitude parameter must read latitudeTextBox.Text, and the longitude parameter must read longitudeTextBox.Text. Keep the URL on one line. GetStringAsync retrieves the response as a string. The async and await keywords allow the application to wait for the response without blocking the window.

4 Replace only the generated loadWeatherButton_Click method with this method

Include the method declaration and braces below. Notice the async keyword in the declaration. Do not paste a method inside another method, and do not replace the entire MainWindow class. Keep the constructor and every event handler from 0.4.

MainWindow.xaml.cs — loadWeatherButton_Click
private async void loadWeatherButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
    try
    {
        string json = await this.GetForecastAsync();

        string html = "<html><head>"
            + "<meta charset=\"utf-8\">"
            + "</head><body>"
            + "<pre style=\"white-space:pre-wrap;word-wrap:break-word;\">"
            + System.Net.WebUtility.HtmlEncode(json)
            + "</pre></body></html>";

        this.BrowserControl.NavigateToString(html);
    }
    catch (System.Exception ex)
    {
        this.BrowserControl.NavigateToString(
            "<html><body><h1>Unable to retrieve the forecast</h1><p>"
            + System.Net.WebUtility.HtmlEncode(ex.Message)
            + "</p></body></html>");
    }
}

The event handler calls GetForecastAsync and stores the response in json. HtmlEncode makes the response safe to display as text inside an HTML page. NavigateToString displays that page in BrowserControl. If the request fails, the catch block displays an error in the same browser area. Weather loading does not change textOutput or the rain checkbox.

5 Save all files and build the solution

Correct errors before continuing. If a Click handler is missing, double-click its button in the designer to create or open the handler. Restore its implementation from your 0.4 backup when needed; an empty handler will not perform the previous behavior.

B Check Your Work: Weather and all existing buttons
1 Run the application
2 Click New student

Verify that Nou Xiong, the student ID, energy level, and hungry status appear. Click New student before Have breakfast or Fill backpack so that a Student object exists.

3 Click Have breakfast

Verify that the output describes the breakfast and updated energy level and hungry status.

4 Click Fill backpack

Verify that its previous behavior still works.

5 Check Choose clothing with the rain checkbox selected and cleared

Verify both clothing outcomes from the previous assignment. Loading weather does not automatically change this checkbox.

6 Check the Save Shopping List button from the previous assignment

Type Notebook and Pens, one per line, into the shopping-list TextBox. Click Save shopping list. Verify that the output says Shopping list saved..

7 Check the Load Shopping List button from the previous assignment

Close and restart the application. Verify that the shopping-list TextBox starts empty. Click Load shopping list. Notebook and Pens should reappear, and the output should say Shopping list loaded.. Clear the TextBox and click Load shopping list again without saving; the same items should return.

8 Enter the example coordinates

Type -87.6298 in Longitude and 41.8781 in Latitude. Use a period for the decimal separator. These coordinates identify a location in Chicago.

9 Click Load weather and inspect the result

With an internet connection, the browser area should display the JSON response. Look for daily_units, daily, and the requested precipitation fields. Scroll within the browser area if needed. The values change over time; no particular forecast value is required. The textOutput area keeps its previous message.

10 Enter an invalid coordinate and click Load weather

Keep the example longitude and change Latitude to 100. Click Load weather. The browser area should display Unable to retrieve the forecast with an error message because the request contains an invalid latitude. Restore Latitude to 41.8781 and verify that the JSON response loads again.

11 Close the application
Submission
5.Final Check and Submit [5 minutes]
A Complete the final technical check
1 Build the solution and correct any build errors
2 Verify that all checks in Step 4B pass

Include both Save Shopping List and Load Shopping List, as well as Load weather.

B Zip the solution
1 Close Visual Studio
2 Create a ZIP of the entire LastName solution folder
3 Open the ZIP and confirm the solution and project are present

Verify that LastName.sln and the FirstDayScenario project folder are included.

C Submit to the Feedback System
1 Open the Feedback System and select First Day 0.5
2 Upload the zipped Visual Studio solution
3 Review the results and correct any required issues

Resubmit an updated ZIP when needed.

D Submit the Feedback System URL to Canvas
1 Copy the Feedback System submission URL
2 Open the 0.5 First Day Assignment in Canvas
3 Submit the Feedback System URL as your Canvas submission