Manage a list of items for when you go shopping.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
try, catch, finallyThe block of commands will be attempted, but if they cause an error/exception the program will not crash.
It is possible to nest more than one try block.
catch (ExceptionType)
Block of commands will be executed if an error occurred. It is good practice to specify the error being trapped. E.g.
catch (FileNotFoundException)
finally
Block of commands executes regardless of whether an error occurred or not.
Questions to think about with this program to check your understanding:
Why is a try/catch (FileNotFoundException) block a good idea to include in all programs using files?
try
{
StreamReader file = new StreamReader("shopping.txt");
…
}
catch (FileNotFoundException)
{
File.WriteAllText("shopping.txt", "");
}
The file can be accessed outside of the program and therefore it might have been moved, renamed or deleted by the user, or maybe it may never have been created. The program would crash if it cannot find a file it is expecting to read from. We should stop our program from crashing when exceptions happen. This is called “exception handling”.
Why can’t you sort data directly in a file without reading the contents into memory first?
A StreamReader can only move forward through a file, it cannot go back once a line of data has been read. That makes it impossible to move an item in a file using the approach taken in this program.
This is why text files are only used for simple data storage. More advanced techniques exist for handling large data sets.
Change the program so that it:
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice:
1
Enter the item to add to the list:
Bread
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice:
2
-----------------------------
Bread
-----------------------------
1. Add item
2. Output shopping list
3. Sort list
4. Clear list
5. Quit
Enter choice:
5
add_item
// Add an item to the shopping list
static void add_item()
{
---
string file_content = "";
try
{
StreamReader file_reader = new StreamReader("shopping.txt");
file_content = file_reader.ReadToEnd();
file_reader.Close();
}
---
catch (FileNotFoundException)
{
StreamWriter creator = new StreamWriter("shopping.txt", false);
creator.Close();
}
---
Console.WriteLine("Enter the item to add to the list:");
string new_item = Console.ReadLine();
bool item_exists = false;
string[] items = file_content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
---
foreach (string item in items)
{
if (item.Trim() == new_item)
{
item_exists = true;
}
}
---
StreamWriter file_writer = new StreamWriter("shopping.txt", true);
if (!item_exists)
{
file_writer.WriteLine(new_item);
}
file_writer.Close();
}
output_items
// Output the shopping list
static void output_items()
{
---
try
{
StreamReader file_reader = new StreamReader("shopping.txt");
Console.WriteLine("-----------------------------");
string file_content = file_reader.ReadToEnd();
file_reader.Close();
string[] items = file_content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
---
foreach (string item in items)
{
Console.WriteLine(item.Trim());
}
Console.WriteLine("-----------------------------");
}
---
catch (FileNotFoundException)
{
StreamWriter file_writer = new StreamWriter("shopping.txt", true);
file_writer.Close();
}
}
sort_items
// Sort the shopping list
static void sort_items()
{
---
try
{
StreamReader file_reader = new StreamReader("shopping.txt");
string file_content = file_reader.ReadToEnd();
file_reader.Close();
string[] items = file_content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
Array.Sort(items, StringComparer.Ordinal);
---
using (StreamWriter file = new StreamWriter("shopping.txt"))
{
foreach (string item in items)
{
file.WriteLine(item);
}
}
}
---
catch (FileNotFoundException)
{
StreamWriter file_writer = new StreamWriter("shopping.txt", true);
file_writer.Close();
}
}
clear_list
// Clear the shopping list
static void clear_list()
{
---
StreamWriter file_writer = new StreamWriter("shopping.txt", false);
file_writer.Close();
}
menu
// Menu
static void menu()
{
---
string choice = "";
while (choice != "5")
{
---
Console.WriteLine("1. Add item");
Console.WriteLine("2. Output shopping list");
Console.WriteLine("3. Sort list");
Console.WriteLine("4. Clear list");
Console.WriteLine("5. Quit");
---
while (choice != "1" && choice != "2" && choice != "3" && choice != "4" && choice != "5")
{
---
Console.WriteLine("Enter choice:");
choice = Console.ReadLine();
---
}
---
switch (choice)
{
---
case "1": add_item(); choice = ""; break;
case "2": output_items(); choice = ""; break;
case "3": sort_items(); choice = ""; break;
case "4": clear_list(); choice = ""; break;
---
}
---
}
---
}