I’m new to programing and could use some help . C#.

Updated on October 24, 2017 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
10 on February 7, 2017

Alright so I’m currently in a class for programing and we’re using C#.This is pretty difficult since I’m reading this course on distance (studying from home). I’m not looking for anyone who can write the code for me since that will most likely end up with me learning nothing or to little. I really want to learn how to program but it’s diffuclt. I was hoping that someone could maybe point me in the right dirrection and give some tips/tricks on what I can do in order to make this assignement. I’ve been watching videos on youtube (including your C# tutorials) and also i’ve been searching on google and MSDN. But I must have missed something because I can’t figure this out… Any help is be appriciated.

I’ve been assigned to make a program that works like a logbook. Here are some functions that are required:

    • A menu where you can navigate through the program.
    • Fix runtime errors.
    • The option to post something in the log.
    • The option to save new posts in the logbook, with titel and a message. The logbook should be a List while the seperate logs should be array.
    • The option to search for posts inside the logbook, for example the posts title.
    • Improve function on the handling of strings for the logs.

Extend program functionality with any of the following:

    • Ability to save the date of signing.
    • Ability to edit logs.
    • Ability to remove logs.
    • Write your program with basic object orientation. 

I am already familiar with how to make a menu and I can also prevent a program from crashing when the user is typing a letter instead of a number. What I find most difficult is how to make an option to save,search and/or delete posts.

Thank you.

Edit: Please note that english is not my mother language and therefor there might be alot of misspellings. If there’s something that you do not understand in my post please let me know so that I can clearify.

  • Liked by
Reply
0 on February 7, 2017

What type of application is it? (Like, game (if it is, which engine do you use?), UWP, Windows Forms, Console, etc..)

  • Liked by
Reply
Cancel
0 on February 7, 2017

I’m not sure what your question is. Do you mean what program I use to write the code? I’m using Visual Studio Express.

  • Liked by
Reply
Cancel
0 on February 7, 2017

I’m using Console ( I think :P)

  • Liked by
Reply
Cancel
1 on February 7, 2017

No, I am not asking what IDE. What type of application are you making?

on February 7, 2017

It’s a console application 🙂

Show more replies
  • Liked by
Reply
Cancel
1 on February 7, 2017
on February 7, 2017

I will let you know what kind of application it is as soon as I get an answer from him.

Show more replies
  • Liked by
Reply
Cancel
2 on February 8, 2017

Phew, that was a good practice… It good to sometimes try to do those things in console applications, where you don’t have many tools. I will walk you through what I did.

I just want to say that I am not 100% sure I understood you, but I still tried.

So first thing you listed is making a menu to navigate the program.
I did it using a Dictionary (look it up if you don’t know what it is) with int as the key and Action as the value. I declared it at the start of main:

Dictionary<int, Action> inputActionMapper = new Dictionary<int, Action>()
 {
 [1] = MakeLog,
 [2] = ViewAllLogs
 };

The key (int) is the index the user inserts, while the Action is the method to call. MakeLog and ViewAllLogs are methods I created, I will show you what I did in them later.

Now I started an infinite loop for the program using while(true), so imagine everything is there:
I just asked the user to select an action and drew the actions. I also made an int List, which will store the possible action numbers (you will see why later). This is how I did it:

Console.WriteLine("Please choose an action from one of the followings:");
 List<int> possibleInputs = new List<int>(inputActionMapper.Count);
 foreach (KeyValuePair<int, Action> inputAction in inputActionMapper)
 {
 DrawInputAction(inputAction.Key, inputAction.Value);
 possibleInputs.Add(inputAction.Key);
 }

You probably noticed I called a method to draw the action, so here it is:
static void DrawInputAction(int index, Action action)
 {
 Console.WriteLine(index + ". " + action.Method.Name);
 }

But there is a problem with this… action.Method.Name will give the name of the method, which is not very user friendly (mainly the fact that there are no spaces). Instead, it’s a good idea to make a struct…
struct ActionNameMap
 {
 public string name;
 public Action action;
 public ActionNameMap(string name, Action action)
 {
 this.name = name;
 this.action = action;
 }
 }

…to store the name and the item. Then I could just access the name variable when logging. Something like that:
Dictionary<int, ActionNameMap> inputActionMapper = new Dictionary<int, ActionNameMap>()
 {
  [1] = new ActionNameMap("Make A New Log", MakeLog),
  [2] = new ActionNameMap("View All Existing Logs", ViewAllLogs)
 };
 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);
 }

Now it’s time for the user to choose stuff. I will put the code here and explain with comments:
// Make an integer to store the index of the selected action.
int selectedAction;
 if (int.TryParse(Console.ReadLine(), out selectedAction))
 {
  // If the user put a number, check if there is an action with this index.
  if (possibleInputs.Contains(selectedAction))
  {
    // Clearing the console and, invoking the action at the index.
    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();

Now that was the menu. Now…option to post stuff in the log? I don’t think I got it, but what I did was letting the user save logs.

Making Logs:
I made a class for the log:

class Log
 {
 public string title;
 public string log;
 public Log(string title, string log)
 {
 this.title = title;
 this.log = log;
 }
 }

So then I made a list of logs and initialized it in Main:
static List<Log> logs;
 public static void Main(string[] args)
 {
   logs = new List<Log>();
}

Time to declare the two methods you saw at the start:
static void MakeLog()
 {
  // Ask for the title and store it in a variable.
  Console.Write("Please insert the title: ");
  string title = Console.ReadLine();
  // Ask for the log and store it in a variable.
  Console.Write("Please insert the log: ");
  string log = Console.ReadLine();
  // Add a new log to the list with the title and the log.
  logs.Add(new Log(title, log));
 }
 static void ViewAllLogs()
 {
  // If there are no logs, tell the user there are no logs.
  if (logs.Count < 1)
  {
  Console.WriteLine("No logs found!");
  return;
  }
  // If there are, Loop through them and draw them.
  for (int i = 0; i < logs.Count; i++)
  {
    Console.WriteLine((i + 1) + ". " + logs[i].title + ": \"" + logs[i].log + "\"");
  }
 }

I see there was something about storing the logs in an array, but I didn’t really get that, so I just ignored it…

I am actually too lazy to do the searching thing… Sorry! But it will stay a challenge for you, I guess. 😛
I didn’t get the thing with the better way of handling things with strings, so I will leave that to you as well…

Now for the bonuses!

Saving the log dates:
So let’s add a new field to the Log class:

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;
 }
 }

The reason I initialized it to DateTime.Now is because the time it was logged will never be different. The reason I didn’t make a setter for it is because the time it was logged will never change.

Now, just when viewing the 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 + ")");
 }
 }

That was a pretty simple one.

Edit and remove nodes:
So this is the good thing about the system with the dictionary!
All I need to do is add some more items in the dictionary:

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)
 };

And make the new functions:
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");
 }

That’s all I will do. To sum this up, here is the full 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;
 }
 }

Here are a few challenges I am giving:
Do all of the things in the original thing that I didn’t do.
Make it more efficient using things like ToString.
Add another date for the last time the post was edited.

on February 10, 2017

Hey Aviram.
Your code is verry usefull for me and I will experiment with it alot and also do your challanges! I appriciate your help alot, thank you! 🙂

I’ll update you once I’ve completed your challange.

on October 24, 2017

Hello Aviram!

I was wondering if you could perhaps help me figure out how to write the same program, but with the use of an switch statement for the menu 

 

Show more replies
  • Liked by
Reply
Cancel