Example #1
0
        // AddTask method take  data from user and create new TaskModel
        private static TaskModel CreateTask()
        {
            Console.Clear();
            MainMenu.Menu();

            string   description;
            DateTime startTime;
            DateTime?endTime;
            bool?    allDayTask;
            bool?    importantTask;

            ConsoleEx.WriteLine(":: NEW TASK ::", ConsoleColor.DarkCyan);
            ConsoleEx.WriteLine("".PadLeft(115, '^'), ConsoleColor.DarkCyan);
            Console.WriteLine(">>> Insert the data according to the scheme below (use comma [,] as a separator): ");
            ConsoleEx.WriteLine("".PadLeft(115, '-'), ConsoleColor.DarkCyan);
            ConsoleEx.WriteLine(">>> TASK SCHEME:\ndescription,start_time,end_time,[all-day task (optional)],[important task (optional)]", ConsoleColor.Green);
            Console.WriteLine();
            ConsoleEx.WriteLine("Description [required]:\ncontent of the task", ConsoleColor.Gray);
            Console.WriteLine();
            ConsoleEx.WriteLine("Start_time [required]:\nthe start date of the task in format: [YYYY-MM-DD]", ConsoleColor.Gray);
            Console.WriteLine();
            ConsoleEx.WriteLine("End_time [optional]:\nthe end date of the task in format: [YYYY-MM-DD] --> leave empty if it is all-day task", ConsoleColor.Gray);
            Console.WriteLine();
            ConsoleEx.WriteLine("All-day task [optional]:\nenter [true] or [false]", ConsoleColor.Gray);
            Console.WriteLine();
            ConsoleEx.WriteLine("Important task [optional]:\nenter [true] or [false]", ConsoleColor.Gray);
            ConsoleEx.WriteLine("".PadLeft(115, '='), ConsoleColor.DarkCyan);
            Console.WriteLine();
            Console.Write("Enter new task [in scheme]: ");

            string taskData = Console.ReadLine().Trim();

            string[] checkData = taskData.Split(",");

            try
            {
                if (checkData[0] == null)
                {
                    throw new NullReferenceException();
                }
                else
                {
                    description = checkData[0]; // set Description
                }
            }
            catch (NullReferenceException)
            {
                ConsoleEx.Write("Missing task description.", ConsoleColor.Red);
                return(null);
            }

            try
            {
                char[] chars = checkData[1].ToCharArray();
                if (checkData[1].Trim().Length == 10 && chars[4] == '-' && chars[7] == '-')
                {
                    startTime = DateTime.Parse(checkData[1].Trim()); // set StartTime
                }
                else
                {
                    throw new FormatException();
                }
            }
            catch (FormatException)
            {
                ConsoleEx.Write("Incorrect date format.", ConsoleColor.Red);
                return(null);
            }

            if (string.IsNullOrWhiteSpace(checkData[2])) // EndTime is optional - set null if it is empty
            {
                endTime = null;
            }
            else
            {
                try
                {
                    char[] chars = checkData[1].ToCharArray();
                    if (checkData[2].Trim().Length == 10 && chars[4] == '-' && chars[7] == '-')
                    {
                        endTime = DateTime.Parse(checkData[2].Trim()); // set EndTime
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                catch (Exception)
                {
                    ConsoleEx.Write("Incorrect date format.", ConsoleColor.Red);
                    return(null);
                }
            }

            if (checkData[3].Trim() == "true") // set AllDayTask flag
            {
                allDayTask = true;
            }
            else if (checkData[3].Trim() == "false")
            {
                allDayTask = false;
            }
            else
            {
                allDayTask = null;
            }

            if (checkData[4].Trim() == "true") // set ImportantTask flag
            {
                importantTask = true;
            }
            else if (checkData[4].Trim() == "false")
            {
                importantTask = false;
            }
            else
            {
                importantTask = null;
            }
            return(new TaskModel(description, startTime, endTime, allDayTask, importantTask));
        }
        static void AddTask(List <TaskModel> tasks)
        {
            ConsoleEx.Write("\nOpis zadania: ", ConsoleColor.Green);
            string descr = Console.ReadLine();

            ConsoleEx.Write("Start zadania: ", ConsoleColor.Green);
            string   start = Console.ReadLine();
            DateTime date;
            bool     isDateOk = DateTime.TryParse(start, out date);

            while (!isDateOk)
            {
                ConsoleEx.Write("Start zadania (rrrr-mm-dd [hh:mm]): ", ConsoleColor.Green);
                start    = Console.ReadLine();
                isDateOk = DateTime.TryParse(start, out date);
            }


            ConsoleEx.Write("Ważne? Y/N: ", ConsoleColor.Green);
            string important = Console.ReadLine().ToLower();

            while (!(important == "n" || important == "y"))
            {
                ConsoleEx.Write("Ważne? Y/N: ", ConsoleColor.Green);
                important = Console.ReadLine().ToLower();
            }


            ConsoleEx.Write("Całodzienne? Y/N: ", ConsoleColor.Green);
            string allday = Console.ReadLine().ToLower();

            while (!(allday == "n" || allday == "y"))
            {
                ConsoleEx.Write("Całodzienne? Y/N: ", ConsoleColor.Green);
                allday = Console.ReadLine().ToLower();
            }



            var tm = new TaskModel(descr, date);

            if (important == "y")
            {
                tm.IsImportant = true;
            }

            if (allday == "y")
            {
                tm.IsAllday = true;
                tm.EndDate  = null;
            }
            else
            {
                ConsoleEx.Write("Data zakończena zadania: ", ConsoleColor.Green);
                string   end = Console.ReadLine();
                DateTime edate;
                bool     iseDateOk = DateTime.TryParse(end, out edate);

                while (!iseDateOk)
                {
                    ConsoleEx.Write("Data zakończena zadania (rrrr-mm-dd [hh:mm]): ", ConsoleColor.Green);
                    end       = Console.ReadLine();
                    iseDateOk = DateTime.TryParse(end, out edate);
                }

                tm.EndDate = edate;
            }

            tasks.Add(tm);
            ConsoleEx.WriteLine("Zadanie dodane.", ConsoleColor.Green);
            SaveTasks(tasks);
        }
Example #3
0
        // LoadTasks method loads all tasks form a file
        private static void LoadTasks(List <TaskModel> taskList)
        {
            string path = "";

            string[] listUpload = null;

            Console.Clear();
            MainMenu.Menu();
            ConsoleEx.WriteLine(" :: LOADING TASKS FROM A FILE :: ", ConsoleColor.DarkCyan);
            ConsoleEx.WriteLine("".PadLeft(115, '^'), ConsoleColor.DarkCyan);
            Console.WriteLine();
            Console.Write("Enter the file name: ");
            path = Console.ReadLine().Trim();

            if (File.Exists(path) == false)
            {
                ConsoleEx.Write("File was not found.", ConsoleColor.Red);
                Console.ReadLine();
                return;
            }

            string[]        linesFromLoadedFile = File.ReadAllLines(path);
            List <string[]> tasksFromLoadedFile = new List <string[]>();

            char[] separators = { ',', ';' };

            foreach (string item in linesFromLoadedFile)
            {
                tasksFromLoadedFile.Add(item.Split(separators));
                try
                {
                    foreach (string[] loadedTask in tasksFromLoadedFile)
                    {
                        DateTime?endTime;
                        if (string.IsNullOrWhiteSpace(loadedTask[2]))
                        {
                            endTime = null;
                        }
                        else
                        {
                            endTime = DateTime.Parse(loadedTask[2]);
                        }

                        bool?allDayTask;
                        if (string.IsNullOrWhiteSpace(loadedTask[3]))
                        {
                            allDayTask = null;
                        }
                        else
                        {
                            allDayTask = bool.Parse(loadedTask[3]);
                        }

                        bool?importantTask;
                        if (string.IsNullOrWhiteSpace(loadedTask[4]))
                        {
                            importantTask = null;
                        }
                        else
                        {
                            importantTask = bool.Parse(loadedTask[4]);
                        }

                        taskList.Add(new TaskModel(loadedTask[0], DateTime.Parse(loadedTask[1]), endTime, allDayTask, importantTask));
                    }
                    tasksFromLoadedFile.Clear();
                }
                catch (FormatException)
                {
                    ConsoleEx.Write("Wrong file format.", ConsoleColor.Red);
                    Console.ReadLine();
                    return;
                }
                catch (Exception)
                {
                    ConsoleEx.Write("An unknown error occurred.", ConsoleColor.Red);
                    Console.ReadLine();
                    return;
                }
            }
            Console.WriteLine();
            ConsoleEx.WriteLine(" >>> Tasks from the file was successfully loaded.\n", ConsoleColor.Green);
            Console.Write("Press ENTER to continue... ");
            Console.ReadLine();
        }