Advertisement
Guest User

TodoConsoleApplication

a guest
Jul 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.08 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace TodoApp
  5. {
  6.     class Program
  7.     {
  8.         static void ColoredText(string text,ConsoleColor color)
  9.         {
  10.             Console.ForegroundColor  = color;
  11.             Console.WriteLine(text);
  12.             Console.ResetColor();
  13.         }
  14.         static void ViewTODO(string[] lines)
  15.         {
  16.             Console.WriteLine("Things you have to do:");
  17.             int i = 1;
  18.             while (i < lines.Length) {
  19.                 Console.WriteLine($"\t{i}-{lines[i]}");
  20.                 i++;
  21.             }
  22.         }
  23.         static void RemoveAt<T>(ref T[] arr, int index)
  24.         {
  25.             for (int a = index; a < arr.Length - 1; a++)
  26.             {
  27.                 // moving elements downwards, to fill the gap at [index]
  28.                 arr[a] = arr[a + 1];
  29.             }
  30.             // finally, let's decrement Array's size by one
  31.             Array.Resize(ref arr, arr.Length - 1);
  32.         }
  33.         static void Main()
  34.         {
  35.             // Path for the data.txt file.
  36.             string path = @"data.txt";
  37.             string firstName,input;
  38.             Console.SetWindowSize(Console.LargestWindowWidth/2, Console.LargestWindowHeight/2);
  39.             // Checks if the user is using this program for the first time.
  40.             if (!File.Exists(path))
  41.             {
  42.                 // Greetings.
  43.                 ColoredText("Welcome to TODO-listu.", ConsoleColor.Cyan);
  44.                 // Creates the data.txt file.
  45.                 ColoredText("Apparently this is your first time using the software." +
  46.                     "\nCan you please provide us with your name ?", ConsoleColor.Cyan);
  47.                 firstName = Console.ReadLine();
  48.                 using (StreamWriter sw = File.CreateText(path))
  49.                 {
  50.                     sw.WriteLine($"{firstName}");                  
  51.                 }
  52.                 string[] lines = System.IO.File.ReadAllLines(path);
  53.             }
  54.             // The user did use the program before.
  55.             else
  56.             {
  57.                 string[] lines = System.IO.File.ReadAllLines(path);
  58.                 firstName = lines[0];
  59.                 ColoredText($"Hey {firstName}, We're happy to have you back!", ConsoleColor.Cyan);
  60.             }
  61.             while(true)
  62.             {
  63.                 if (File.ReadAllLines(path).Length == 1)
  64.                 {
  65.                     Console.WriteLine("Your TODO list is empty.");
  66.                 }
  67.                 else
  68.                 {
  69.                     ViewTODO(File.ReadAllLines(path));
  70.                 }
  71.                 ColoredText("Please pick a command:", ConsoleColor.Cyan);
  72.                 ColoredText("add: Add mode, to add multiple elements to the list.\ndel: Delete an element from the list." +
  73.                     "\nclear: This command deletes the whole list.\nesc: To exit the program.", ConsoleColor.Magenta);
  74.                 input = Console.ReadLine();
  75.                 // Add mode.
  76.                 if (input == "add")
  77.                 {
  78.                     Console.Clear();
  79.                     while (true)
  80.                     {
  81.                         ColoredText("Add mode activated, Type 'esc' to go back to main menu:", ConsoleColor.Cyan);
  82.                         input = Console.ReadLine();
  83.                         if (input == "esc")
  84.                         {
  85.                             Console.Clear();
  86.                             break;
  87.                         }
  88.                         else if (input == "")
  89.                         {
  90.                             Console.Clear();
  91.                             ColoredText("You can't add an empty element.", ConsoleColor.Red);
  92.                         }
  93.                         else {
  94.                             using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true))
  95.                             {
  96.                                 file.WriteLine($"{input}");
  97.                             }
  98.                             Console.Clear();
  99.                             ColoredText($"The element '{input}' has been added to the list succsefully.", ConsoleColor.Green);
  100.                         }
  101.                     }
  102.                 }
  103.                 // Delete mode.
  104.                 else if (input == "del")
  105.                 {
  106.                     string[] lines = File.ReadAllLines(path);
  107.                     ColoredText("Please tell us the index of the element you want to delete ?:", ConsoleColor.Cyan);
  108.                     input = Console.ReadLine();
  109.                     if ((Int32.Parse(input) == 0) || (Int32.Parse(input) > lines.Length-1))
  110.                     {
  111.                         Console.Clear();
  112.                         ColoredText($"There is no element with the index {input}.", ConsoleColor.Red);
  113.                         continue;
  114.                     }
  115.                     else
  116.                     {
  117.                         Console.Clear();
  118.                         ColoredText($"The element '{lines[Int32.Parse(input)]}' has been deleted succsefully.", ConsoleColor.Green);
  119.                         RemoveAt(ref lines, Int32.Parse(input));
  120.                         System.IO.File.WriteAllLines(@"data.txt", lines);
  121.                     }
  122.  
  123.                 }
  124.                 // Clear mode.
  125.                 else if (input == "clear")
  126.                 {
  127.                     Console.Clear();
  128.                     ColoredText($"The TODO list has been cleared succsefully.", ConsoleColor.Green);
  129.                     using (StreamWriter sw = File.CreateText(path))
  130.                     {
  131.                         sw.WriteLine(firstName);
  132.                     }
  133.                 }
  134.                 // Exit the program
  135.                 else if (input == "esc")
  136.                 {
  137.                     Console.Clear();
  138.                     break;
  139.                 }
  140.                 // Do nothing if unvalid input.
  141.                 else
  142.                 {
  143.                     Console.Clear();
  144.                     ColoredText($"The command '{input}' doesn't exist.", ConsoleColor.Red);
  145.                     continue;
  146.                 }
  147.             }
  148.  
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement