Switch statement

Updated on October 30, 2017 in [C] Beginner
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on October 24, 2017

Hello everyone.

First off I want to thank Aviram for helping me solving this issue of mine.

I have a code that I’m trying to finish, Aviram helped me with almost everything. But I want to write the menu using a switch statement. So How do I make the same program but changing the menu to a Switch?

Here’s the code:

public class MainClass
 {
 static List<Log> logs;
 public static void Main(string[] args)
 {
 logs = new List<Log>();
 Dictionary<int, ActionNameMap> inputActionMapper = new Dictionary<int, ActionNameMap>()
 {
 [1] = new ActionNameMap("Make A New Log", MakeLog),
 [2] = new ActionNameMap("View All Existing Logs", ViewAllLogs),
 [3] = new ActionNameMap("Edit An Existing Log", EditLog),
 [4] = new ActionNameMap("Delete A Log", DeleteLog)
 };
 while (true)
 {
 Console.WriteLine("Please choose an action from one of the followings:");
 List<int> possibleInputs = new List<int>(inputActionMapper.Count);
 foreach (KeyValuePair<int, ActionNameMap> inputAction in inputActionMapper)
 {
 DrawInputAction(inputAction.Key, inputAction.Value.name);
 possibleInputs.Add(inputAction.Key);
 }
 int selectedAction;
 if (int.TryParse(Console.ReadLine(), out selectedAction))
 {
 if (possibleInputs.Contains(selectedAction))
 {
 Console.Clear();
 inputActionMapper[selectedAction].action();
 }
 else
 Console.WriteLine("Nope, such an action does not exist... Try again!");
 }
 else
 {
 Console.WriteLine("Um... You are supposed to put a number. You won't full meh. ;)");
 }
 Console.ReadKey(true);
 Console.Clear();
 }
 }
 static void DrawInputAction(int index, string name)
 {
 Console.WriteLine(index + ". " + name);
 }
 static void MakeLog()
 {
 Console.Write("Please insert the title: ");
 string title = Console.ReadLine();
 Console.Write("Please insert the log: ");
 string log = Console.ReadLine();
 logs.Add(new Log(title, log));
 }
 static void ViewAllLogs()
 {
 if (logs.Count < 1)
 {
 Console.WriteLine("No logs found!");
 return;
 }
 for (int i = 0; i < logs.Count; i++)
 {
 Console.WriteLine((i + 1) + ". " + logs[i].title + ": \"" + logs[i].log + "\" (Logged On " + logs[i].TimeLogged + ")");
 }
 }
 static void EditLog()
 {
 if (logs.Count < 1)
 {
 Console.WriteLine("No logs found!");
 return;
 }
 Console.WriteLine("Please select a log to edit:\n");
 for (int i = 0; i < logs.Count; i++)
 {
 Console.WriteLine((i + 1) + ". " + logs[i].title + ": \"" + logs[i].log + "\" (Logged On " + logs[i].TimeLogged + ")");
 }
 int selectedLog;
 if (int.TryParse(Console.ReadLine(), out selectedLog))
 {
 if (selectedLog > 0 && selectedLog <= logs.Count)
 {
 Console.Write("Please insert the new title: ");
 string title = Console.ReadLine();
 Console.Write("Please insert the new log: ");
 string log = Console.ReadLine();
 logs[selectedLog - 1] = new Log(title, log);
 }
 else
 Console.WriteLine("Such a log doesn't exist...");
 }
 else
 Console.WriteLine("Haha! You need a number... You are stupid if you thought you can edit a node with letters. xD");
 }
 static void DeleteLog()
 {
 if (logs.Count < 1)
 {
 Console.WriteLine("No logs found!");
 return;
 }
 Console.WriteLine("Please select a log to delete:\n");
 for (int i = 0; i < logs.Count; i++)
 {
 Console.WriteLine((i + 1) + ". " + logs[i].title + ": \"" + logs[i].log + "\" (Logged On " + logs[i].TimeLogged + ")");
 }
 int selectedLog;
 if (int.TryParse(Console.ReadLine(), out selectedLog))
 {
 if (selectedLog > 0 && selectedLog <= logs.Count)
 {
 logs.RemoveAt(selectedLog - 1);
 }
 else
 Console.WriteLine("Such a log doesn't exist...");
 }
 else
 Console.WriteLine("Haha! You need a number... You are stupid if you thought you can delete a node with letters. xD");
 }
 struct ActionNameMap
 {
 public string name;
 public Action action;
 public ActionNameMap(string name, Action action)
 {
 this.name = name;
 this.action = action;
 }
 }
 }
 class Log
 {
 public string title;
 public string log;
 private DateTime timeLogged;
 public DateTime TimeLogged
 {
 get { return timeLogged; }
 }
 public Log(string title, string log)
 {
 this.title = title;
 this.log = log;
 timeLogged = DateTime.Now;
 }
 }

 

  • Liked by
Reply
0 on October 24, 2017

I forgot to add in the title that this is a C# Console program 

  • Liked by
Reply
Cancel
0 on October 29, 2017

The concerning thing for me, especially if you want to learn is that it was all done for you, if you understand it all, that is of course fine.

It has been five days, so i want to check if the issue has been fixed before looking into it.

  • Liked by
Reply
Cancel
0 on October 30, 2017

Can you use
If (Bool= true) { add the log}
Else {remove it}????

  • Liked by
Reply
Cancel