I have just made a logbook that lets the user add a title and a post to a book. And then they have the option to print it out, search for it, erase it or quit the program. The only problems I have is in case 4, how do I let the user erase his post in case 4? And my other problem is that I want the date and time to be added to every post, and I want it to be printed out everytime that post is shown. Would appreciate any answers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Loggbok
{
class MainClass
{
public static void Main(string[] args)
{
DateTime tiden = DateTime.UtcNow;
bool running = true;//Ger ett booleskt värde till variabeln running för att kunna skapa en loop
List<string[]> loggbok = new List<string[]>();
// int loggIndex = 0; // Används för att fylla Arrayen
while (running)//Här skapas loopen
{
Console.WriteLine("\nVälkommen till loggboken!");
Console.WriteLine("\n[1] Skriv ett inlägg");
Console.WriteLine("[2] Skriv ut alla inlägg");
Console.WriteLine("[3] Sök inlägg");
Console.WriteLine("[4] Radera innehåll");
Console.WriteLine("[5] Avsluta loggbok");
Console.Write("\nVälj: ");
int option;
try
{
option = Int32.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("Fel, du får bara skriva in nummer");
continue;
}
switch (option)
{
case 1:
string[] logg = new string[2];
Console.WriteLine(tiden);
Console.WriteLine("Ange en Titel");
logg[0] = Console.ReadLine();
Console.Clear();
Console.WriteLine("Ange text");
logg[1] = Console.ReadLine();
loggbok.Add(logg);
break;
case 2:
foreach (string[] item in loggbok)
{
Console.WriteLine(item[0]);
Console.WriteLine(item[1]);
}
Console.ReadLine();
break;
case 3:
Console.WriteLine("Skriv in ett ord du vill söka efter i loggboken");
var nyckelord = Console.ReadLine();
var entries = loggbok.Where(entry => entry.Any(item =>item.IndexOf(nyckelord, StringComparison.OrdinalIgnoreCase) > -1));
foreach (var entry in entries)
{
Console.WriteLine(string.Join(", ", entry));
}
if (entries.Count() == 0)
{
Console.Write("Din sökning misslyckades...");
}
break;
case 4:
break;
case 5:
running = false;
break;
}
}
}
}
}
This will add the timestamp to the title:
logg[0] = String.Format("[{0}] {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Console.ReadLine());
If you want to add the timestamp to the body instead, just replace logg[0]
with logg[1]
.
The above will for example result in:
[2017-04-17 23:48:34] This is my post
You can also put it after the body:
logg[1] = String.Format("{0}{1}{2}", Console.ReadLine(), Environment.NewLine, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
...which will result in:
This is the body.
2017-04-17 23:48:34
To remove a post by its title you'd have to iterate the entire list and check each title until you find one that matches the user's input.
This will remove the first post that it can find by the specified title:
Console.WriteLine("Skriv titeln på det inlägg du vill ta bort:")
string title = Console.ReadLine();
for(int x = 0; x < loggbok.Count; x++) //Loopa igenom varje inlägg.
{
if(String.Equals(loggbok[x][0], title, StringComparison.OrdinalIgnoreCase)) //Icke skiftlägeskänslig matchning av titeln.
{
loggbok.RemoveAt(x); //Matchning funnen.
break; //Avsluta loopen.
}
}
Note that for this particular code to work, you must not include the timestamp in the title.