static void Main(string [] args) { todoSvc = new ToDoService(new ListRepository()); Boolean exit = false; string [] menus = { "New Todo", "List Todo", "Update Todo", "Delete Todo", "End Application" }; // Action -> type data C# untuk refer ke fuction tanpa parameter dan return type // Action<int> -> terima 1 parameter integer return void // Func<int, string> -> terima 1 parameter int, return string Dictionary <string, Action> commands = new Dictionary <string, Action> { { "new", NewTodoForm }, { "list", ListTodo }, { "update", () => Console.WriteLine("update") }, { "delete", DeleteTodo }, { "end", () => exit = true } }; Action otherCommand = () => Console.WriteLine("Command salah"); do { displayMenu(menus); string command = getUserChoice(); Action action = otherCommand; if (commands.ContainsKey(command)) { action = commands[command]; } action(); } while (!exit); }
static void NewTodoForm() { ToDo todo = ToDoService.BlankTodo(); Console.WriteLine("New Todo"); Console.Write("Input tanggal dan waktu (yyyy-mm-dd hh:mm), kosongkan untuk waktu sekarang: "); string waktu = Console.ReadLine().Trim(); if (waktu != "") { todo.waktu = DateTime.Parse(waktu); } Console.Write("Keterangan: "); todo.keterangan = Console.ReadLine(); Maybe <ToDo> newTodo = todoSvc.Create( todo.waktu, todo.keterangan ); string hasil = "berhasil"; if (newTodo is Nothing <ToDo> ) { hasil = "gagal"; } Console.WriteLine($"Simpan: {hasil}"); }