test
public
Feb 04, 2025
Never
21
1 using System; 2 3 using System.Collections.Generic; 4 5 class TaskManager 6 7 { 8 9 private List<string> tasks = new List<string>(); 10 11 public void AddTask(string task) 12 13 { 14 15 tasks.Add(task); 16 17 Console.WriteLine($"Task '{task}' added."); 18 19 } 20 21 public void RemoveTask(int index) 22 23 { 24 25 if (index < 0 || index >= tasks.Count) 26 27 { 28 29 Console.WriteLine("Invalid task index."); 30 31 return; 32 33 } 34 35 Console.WriteLine($"Task '{tasks[index]}' removed."); 36 37 tasks.RemoveAt(index); 38 39 } 40 41 public void DisplayTasks() 42 43 { 44 45 if (tasks.Count == 0) 46 47 { 48 49 Console.WriteLine("No tasks available."); 50 51 return; 52 53 } 54 55 Console.WriteLine("Tasks:"); 56 57 for (int i = 0; i < tasks.Count; i++) 58 59 { 60 61 Console.WriteLine($"{i + 1}. {tasks[i]}"); 62 63 } 64 65 } 66 67 } 68 69 class Program 70 71 { 72 73 static void Main() 74 75 { 76 77 TaskManager taskManager = new TaskManager(); 78 79 string input; 80 81 Console.WriteLine("Task Manager"); 82 83 Console.WriteLine("Commands: add <task>, remove <task number>, list, exit"); 84 85 do 86 87 { 88 89 Console.Write("> "); 90 91 input = Console.ReadLine(); 92 93 string[] commandParts = input.Split(' ', 2); 94 95 string command = commandParts[0].ToLower(); 96 97 switch (command) 98 99 { 100 101 case "add": 102 103 if (commandParts.Length > 1) 104 105 { 106 107 taskManager.AddTask(commandParts[1]); 108 109 } 110 111 else 112 113 { 114 115 Console.WriteLine("Please specify a task to add."); 116 117 } 118 119 break; 120 121 case "remove": 122 123 if (commandParts.Length > 1 && int.TryParse(commandParts[1], out int index)) 124 125 { 126 127 taskManager.RemoveTask(index - 1); 128 129 } 130 131 else 132 133 { 134 135 Console.WriteLine("Please specify a valid task number to remove."); 136 137 } 138 139 break; 140 141 case "list": 142 143 taskManager.DisplayTasks(); 144 145 break; 146 147 case "exit": 148 149 Console.WriteLine("Exiting Task Manager."); 150 151 break; 152 153 default: 154 155 Console.WriteLine("Unknown command. Try 'add', 'remove', 'list', or 'exit'."); 156 157 break; 158 159 } 160 161 } while (input.ToLower() != "exit"); 162 163 } 164 165 } 166