In 0.3, the First Day application used information while it was running to make a clothing decision. In this assignment, you will explore what happens when information needs to remain available after the application closes.
You will add a small school shopping list, observe its text while the application is running, save the same text to a file, close the application, and then reopen the application so the saved list can be restored. Most of the file code is provided. Your job is to place it correctly, trace the information, and explain where the information exists at each point.
User Story
As a student preparing for the first day of class, I want my school shopping list saved outside the running application so that the information remains available after I close and reopen the program.
Acceptance Criteria
Begin from a copy of your completed 0.3 First Day solution
Use a UML class diagram to recognize classes, attributes, and basic data types already present in the project
Add the supplied shopping-list controls without removing the existing 0.3 controls
Observe the shopping-list text while it exists in the running application
Save the shopping-list text to ShoppingList.txt using the supplied file-writing code
Close the application and verify the file still contains the saved information
Use the supplied startup code to read the saved file back when it exists
Reopen the application and verify the saved shopping list is restored
Submit the zipped solution through the Feedback System and submit the Feedback System URL to Canvas
You are not expected to design a file-storage system. The file-reading and file-writing statements are supplied so you can focus on the difference between information in a running application and information stored persistently.
1. Back Up and Open Your Existing First Day Solution
Your First Day solution was renamed to your last name in 0.2. Keep that working name for the rest of the sequence. At the beginning of each module, make a backup of the completed previous checkpoint, then continue working in the original student-named solution.
Step 1 — Make a checkpoint backup
One Working Name
The backup identifies the completed checkpoint. The working solution remains named for you, so later First Day assignments continue in the same project without repeatedly renaming the solution.
Step 2 — Verify the previous checkpoint
Build or run the solution before making the new 0.4 changes
Verify the major behavior you completed in 0.3 is still present
If the previous checkpoint is not working, correct that starting state before adding the new module work
Module 0.2 used object diagrams to represent particular objects and their values. Module 0.3 used activity diagrams to represent what happens next. A class diagram answers a different question: What information can this kind of object hold, and how are the types related?
Step 1 — Compare classes and data types
The class diagram below summarizes several classes already present in your First Day project. Scalar information appears inside the class with its data type. Relationships to other classes are shown as labeled arrows.
Existing First Day classes provide examples of several basic data types and class relationships.
Step 2 — Find examples of different data types
Find one string attribute
Find one int attribute
Find one double attribute
Find one bool attribute
Find one decimal attribute
Class Diagram vs. Object Diagram
A class diagram describes reusable structure. It does not say that one particular Student currently has a particular name or ID. Those specific runtime values belong to an object diagram or debugger observation.
Check Your Work
You should be able to explain why IsHungry : bool represents a true/false kind of value while FirstName : string represents text. You do not need to change these classes in this assignment.
The next part of the application needs a place where you can enter several lines of text and a button that saves that text. The new controls are supplied so the focus stays on the information, not on designing the interface.
Step 1 — Open MainWindow.xaml
Open FirstDayScenario/MainWindow.xaml
Keep the existing 0.3 StackPanel and controls unchanged
Locate the space between the first StackPanel and the existing textOutput TextBox
Add the shopping-list section below the existing 0.3 controls.
Verify the new School Shopping List text area and Save Shopping List button appear
Type several simple items such as Notebook, Pens, and USB cable, one per line
The new text area gives the running application a value you can observe and later save.Go to top
4. Observe Information While the Program Is Running
Before saving anything, notice where the shopping-list information exists. You typed the text into a control in a running application. At this moment, the program can use the value, but you have not yet demonstrated that it will survive after the program stops.
Step 1 — Add the file namespace
Open MainWindow.xaml.cs. Add the following line with the other using statements near the top of the file.
MainWindow.xaml.cs — add with the using statements
using System.IO;
Step 2 — Add the supplied Save handler
Scroll to the bottom of the MainWindow class. Add the following event handler after the existing event handlers and before the closing brace of MainWindow.
MainWindow.xaml.cs — add this event handler
/// <summary>
/// Saves the school shopping list to a text file.
/// </summary>
/// <param name="sender">The object that initiated the event.</param>
/// <param name="e">The event arguments for the event.</param>
private void SaveShoppingListButton_Click(object sender, RoutedEventArgs e)
{
File.WriteAllText("ShoppingList.txt", this.shoppingListTextBox.Text);
this.textOutput.Text = "Shopping list saved.";
}
Step 3 — Pause before the file is written
Set a breakpoint on the File.WriteAllText line
Run the application
Enter several shopping-list items
Click Save Shopping List
When Visual Studio pauses, inspect this.shoppingListTextBox.Text
Before the write occurs, the shopping-list text is information being used by the running application.Trace the same information from the running application into persistent storage.
Where Is the Information Now?
At the breakpoint, the shopping-list text is available to the running application. The next statement will copy that information into a text file. The purpose of the breakpoint is to observe the state immediately before that change in location.
The supplied File.WriteAllText statement takes the current shopping-list text and writes it to a file named ShoppingList.txt. You are using the operation, not designing a file API.
Step 1 — Complete the save
At the breakpoint, verify the shopping-list text contains the items you entered
Press F10 to execute the File.WriteAllText statement
Continue the application
Verify the output area says Shopping list saved.
Step 2 — Find and inspect ShoppingList.txt
Use File Explorer to search within your 0.4 solution folder for ShoppingList.txt
Open the text file
Compare the file contents with the shopping list in the running application
The text file is a persistent representation of the information that was in the running application.
Step 3 — Prove the information persists
Stop debugging and close the First Day application
Open ShoppingList.txt again
Verify the saved items are still present even though the application is no longer running
Persistence
The application stopped, but the file remained. That is the important difference between information that exists only while a program is running and information that has been written to persistent storage.
6. Read the Shopping List Back When the Program Opens
Saving proves the file survives. Reading the file when the application opens shows why persistent storage is useful: information from an earlier run can become part of a later run.
Step 1 — Add the supplied startup read-back code
Open MainWindow.xaml.cs and locate the MainWindow constructor. Place the following block immediately after this.InitializeComponent();.
The read-back check belongs after the window controls have been initialized.
MainWindow constructor — add after InitializeComponent
if (File.Exists("ShoppingList.txt"))
{
this.shoppingListTextBox.Text = File.ReadAllText("ShoppingList.txt");
}
Step 2 — Read the flow before running it
The activity diagram shows both possible startup paths. If the file exists, the application reads it. If the file does not exist, the shopping list simply begins empty.
Startup read-back and save flow for the 0.4 First Day application.
Step 3 — Trace the read-back path
Make sure the ShoppingList.txt file you created earlier still exists
Set a breakpoint on if (File.Exists("ShoppingList.txt"))
Start the application
When the constructor pauses, predict whether the if block should run
Use F10 to step through the file check and the File.ReadAllText statement
Continue running the application
Check Your Work
The shopping-list items saved during the earlier run should appear again
You should not need to retype those items
The restored text should match the contents of ShoppingList.txt
The new run of the application restores information that was saved by the earlier run.
Step 4 — Observe the no-file path
Close the application
Temporarily rename ShoppingList.txt to ShoppingList-old.txt
Start the application again
Verify the shopping-list area starts empty because ShoppingList.txt does not exist
Close the application and rename the file back to ShoppingList.txt
What Changed Between Runs?
The program code stayed the same. The external file state changed. The File.Exists decision lets the application respond appropriately to either condition.
Review the Feedback System results and correct any in-scope issues if required
Resubmit a corrected ZIP when needed
Step 4 — Submit the Feedback System URL to Canvas
After the required Feedback System submission is ready, copy its URL
Open the correct 0.4 First Day Assignment in Canvas
Submit the Feedback System URL as your Canvas submission
Checkpoint Summary
You observed text while it existed in a running application, wrote the text to persistent storage, proved the file remained after the application closed, and restored the saved information during a later run. You also used a UML class diagram to recognize the data types and structural relationships already represented in the First Day project.