Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace TodoApp
- {
- class Program
- {
- static void ColoredText(string text,ConsoleColor color)
- {
- Console.ForegroundColor = color;
- Console.WriteLine(text);
- Console.ResetColor();
- }
- static void ViewTODO(string[] lines)
- {
- Console.WriteLine("Things you have to do:");
- int i = 1;
- while (i < lines.Length) {
- Console.WriteLine($"\t{i}-{lines[i]}");
- i++;
- }
- }
- static void RemoveAt<T>(ref T[] arr, int index)
- {
- for (int a = index; a < arr.Length - 1; a++)
- {
- // moving elements downwards, to fill the gap at [index]
- arr[a] = arr[a + 1];
- }
- // finally, let's decrement Array's size by one
- Array.Resize(ref arr, arr.Length - 1);
- }
- static void Main()
- {
- // Path for the data.txt file.
- string path = @"data.txt";
- string firstName,input;
- Console.SetWindowSize(Console.LargestWindowWidth/2, Console.LargestWindowHeight/2);
- // Checks if the user is using this program for the first time.
- if (!File.Exists(path))
- {
- // Greetings.
- ColoredText("Welcome to TODO-listu.", ConsoleColor.Cyan);
- // Creates the data.txt file.
- ColoredText("Apparently this is your first time using the software." +
- "\nCan you please provide us with your name ?", ConsoleColor.Cyan);
- firstName = Console.ReadLine();
- using (StreamWriter sw = File.CreateText(path))
- {
- sw.WriteLine($"{firstName}");
- }
- string[] lines = System.IO.File.ReadAllLines(path);
- }
- // The user did use the program before.
- else
- {
- string[] lines = System.IO.File.ReadAllLines(path);
- firstName = lines[0];
- ColoredText($"Hey {firstName}, We're happy to have you back!", ConsoleColor.Cyan);
- }
- while(true)
- {
- if (File.ReadAllLines(path).Length == 1)
- {
- Console.WriteLine("Your TODO list is empty.");
- }
- else
- {
- ViewTODO(File.ReadAllLines(path));
- }
- ColoredText("Please pick a command:", ConsoleColor.Cyan);
- ColoredText("add: Add mode, to add multiple elements to the list.\ndel: Delete an element from the list." +
- "\nclear: This command deletes the whole list.\nesc: To exit the program.", ConsoleColor.Magenta);
- input = Console.ReadLine();
- // Add mode.
- if (input == "add")
- {
- Console.Clear();
- while (true)
- {
- ColoredText("Add mode activated, Type 'esc' to go back to main menu:", ConsoleColor.Cyan);
- input = Console.ReadLine();
- if (input == "esc")
- {
- Console.Clear();
- break;
- }
- else if (input == "")
- {
- Console.Clear();
- ColoredText("You can't add an empty element.", ConsoleColor.Red);
- }
- else {
- using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true))
- {
- file.WriteLine($"{input}");
- }
- Console.Clear();
- ColoredText($"The element '{input}' has been added to the list succsefully.", ConsoleColor.Green);
- }
- }
- }
- // Delete mode.
- else if (input == "del")
- {
- string[] lines = File.ReadAllLines(path);
- ColoredText("Please tell us the index of the element you want to delete ?:", ConsoleColor.Cyan);
- input = Console.ReadLine();
- if ((Int32.Parse(input) == 0) || (Int32.Parse(input) > lines.Length-1))
- {
- Console.Clear();
- ColoredText($"There is no element with the index {input}.", ConsoleColor.Red);
- continue;
- }
- else
- {
- Console.Clear();
- ColoredText($"The element '{lines[Int32.Parse(input)]}' has been deleted succsefully.", ConsoleColor.Green);
- RemoveAt(ref lines, Int32.Parse(input));
- System.IO.File.WriteAllLines(@"data.txt", lines);
- }
- }
- // Clear mode.
- else if (input == "clear")
- {
- Console.Clear();
- ColoredText($"The TODO list has been cleared succsefully.", ConsoleColor.Green);
- using (StreamWriter sw = File.CreateText(path))
- {
- sw.WriteLine(firstName);
- }
- }
- // Exit the program
- else if (input == "esc")
- {
- Console.Clear();
- break;
- }
- // Do nothing if unvalid input.
- else
- {
- Console.Clear();
- ColoredText($"The command '{input}' doesn't exist.", ConsoleColor.Red);
- continue;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement