gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
create c# console menu with choices AddItem, DeleteById, PrintAll and Exit
- User
create c# console menu with choices AddItem, DeleteById, PrintAll and Exit
- Assistant
Here's an example of a C# console menu with the specified choices:
using System; class Program { static void Main(string[] args) { bool exit = false; ItemList itemList = new ItemList(); while (!exit) { Console.Clear(); Console.WriteLine("Menu:"); Console.WriteLine("1. Add Item"); Console.WriteLine("2. Delete by ID"); Console.WriteLine("3. Print All"); Console.WriteLine("4. Exit"); Console.Write("Enter your choice: "); string choice = Console.ReadLine(); switch (choice) { case "1": Console.Write("Enter item ID: "); int id = int.Parse(Console.ReadLine()); Console.Write("Enter item name: "); string name = Console.ReadLine(); Item item = new Item(id, name); itemList.AddItem(item); break; case "2": Console.Write("Enter item ID to delete: "); int deleteId = int.Parse(Console.ReadLine()); itemList.DeleteById(deleteId); break; case "3": itemList.PrintAll(); break; case "4": exit = true; break; default: Console.WriteLine("Invalid choice. Please try again."); break; } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } Console.WriteLine("Exiting the program..."); } } class Item { public int ID { get; set; } public string Name { get; set; } public Item(int id, string name) { ID = id; Name = name; } } class ItemList { private List<Item> items; public ItemList() { items = new List<Item>(); } public void AddItem(Item item) { items.Add(item); Console.WriteLine("Item added successfully."); } public void DeleteById(int id) { Item item = items.FirstOrDefault(i => i.ID == id); if (item != null) { items.Remove(item); Console.WriteLine("Item deleted successfully."); } else { Console.WriteLine("No item found with the provided ID."); } } public void PrintAll() { Console.WriteLine("Items:"); foreach (Item item in items) { Console.WriteLine($"ID: {item.ID}, Name: {item.Name}"); } } }This example uses a
ItemListclass to store and manage the items. TheItemclass represents an individual item with an ID and a name. The main program presents a menu with options to add an item, delete an item by ID, print all the items, or exit the program. Based on the user's choice, the program interacts with theItemListto perform the specified action.