Warning! It seems that you are using Dodona within another webpage, so not everything may work properly. Let your teacher know so that he can solve the problem by adjusting a setting in the learning environment. In the meantime, you can click
this link to open Dodona in a new window.
Warning! The page was not fully loaded, probably because of a network issue. It could be that not all functionalities work as expected. Please try reloading the page.
Level 8 - Files and exceptions
0/9 0 of 9 activities solved
Saving data
Not yet solved
School club
Not yet solved
Shopping list
Not yet solved
Attributes
Not yet solved
Cookies
Not yet solved
Morse code
Not yet solved
ini file
Not yet solved
Amino acids
Not yet solved
Gamertag
Not yet solved
Next series
Level 9 - Two-dimension arrays and lists
Previous activity
School club
Next activity
Attributes
Sign in to hand in your solution and get feedback.
// Shopping list program
using System;
using System.Collections.Generic;
using System.IO;
class Submission
{
// -------------------------
// Subprograms
// -------------------------
static void add_item()
{
Console.WriteLine("Enter the item to add to the list:");
string new_item = Console.ReadLine();
StreamWriter file_writer = new StreamWriter("shopping.txt", true);
file_writer.WriteLine(new_item);
file_writer.Close();
}
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' });
foreach (string item in items)
{
Console.WriteLine(item.Trim());
}
Console.WriteLine("-----------------------------");
}
catch (FileNotFoundException)
{
StreamWriter file_writer = new StreamWriter("shopping.txt", true);
file_writer.Close();
}
}
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' });
Array.Sort(items);
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();
}
}
static void menu()
{
string choice = "";
while (choice != "4")
{
Console.WriteLine("1. Add item");
Console.WriteLine("2. Output shopping list");
Console.WriteLine("3. Sort list");
Console.WriteLine("4. Quit");
while (choice != "1" && choice != "2" && choice != "3" && choice != "4")
{
Console.WriteLine("Enter choice:");
choice = Console.ReadLine();
}
// Perform function
switch (choice)
{
case "1":
add_item();
choice = "";
break;
case "2":
output_items();
choice = "";
break;
case "3":
sort_items();
choice = "";
break;
}
}
}
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
menu();
}
}
Feedback