Shopping list

Sign in to test your solution.
// 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(); } }
You can submit as many times as you like. Only your latest submission will be taken into account.
Sign in to test your solution.