Exercise Tasks C# Sharp Exercise

Exercise

Exercise Tasks

Objetive

Create a C# program that can store up to 2000 "to-do tasks". For each task, it must keep the following data:

• Date (a set of 3 data: day, month and year)
• Description of task
• Level of importance (1 to 10)
• Category

The program should allow the user the following operations:

1 - Add a new task (the date must "seem correct": day 1 to 31, month 1 to 12, year between 1000 and 3000).

2 - Show the tasks between two certain dates (day, month and year). If the user presses Enter without specifying date, it will be taken as "today". It must display the number of each record, the date (DD/ MM/YYYY), description, category and importance, all in the same line, separated with hyphens.

3 - Find tasks that contain a certain text (in description or category, not case sensitive). It will display number, date and description (only 50 letters, in case it was be longer). The user should be notified if none is found.

4 - Update a record (it will ask for the number, will display the previous value of each field and the user can press Enter not to modify any of the data). The user should be warned (but not asked again) if he enters an incorrect record number. It is not necessary to validate any of the fields.

5 - Delete some data, between two positions indicated by the user. The user should be warned (but not asked again) if he enters any incorrect record number. Each record to be deleted must be displayed, and the user must be asked for confirmation.

6 - Sort the data alphabetically by date and (if two dates are the same) by description.

7 - Find Duplicates: If two records have the same description, both will be displayed on-screen.

Q - Quit (end the application; as we do not store the information, it will be lost).

(Hint: you can know the current date using DateTime.Now.Day, DateTime.Now.Month and DateTime.Now.Year).

Example Code

using System;
public class exercis95
{
    struct DateType
    {
        public ushort year;
        public byte month;
        public byte day;
    }

    struct TaskType
    {
        public DateType date;
        public string description;
        public byte level;
        public string category;
    }

    public static void Main()
    {
        int capacity = 2000;
        TaskType[] tasks = new TaskType[capacity];
        char option;
        int counter = 0;
        string search, newValue;
        bool found;

        do
        {
            Console.WriteLine();
            Console.WriteLine("Tasks database");
            Console.WriteLine();
            Console.WriteLine("1- Add a new task.");
            Console.WriteLine("2- Show the tasks between two certain dates.");
            Console.WriteLine("3- Find tasks that contain a certain text.");
            Console.WriteLine("4- Update a record.");
            Console.WriteLine("5- Delete some data, between two positions indicated.");
            Console.WriteLine("6- Sort the data alphabetically by date.");
            Console.WriteLine("7- Find Duplicates.");
            Console.WriteLine("Q- Quit.");
            Console.WriteLine("Enter an option:");
            option = Convert.ToChar(Console.ReadLine().ToUpper());

            switch (option)
            {
                case '1': //Add a new task
                    if (counter < capacity)
                    {
                        Console.Write("Enter the Description of the task: ");
                        tasks[counter].description = Console.ReadLine();

                        Console.Write("Enter the Level of the task (1-10): ");
                        tasks[counter].level = Convert.ToByte(Console.ReadLine());

                        Console.Write("Enter the Category of the task: ");
                        tasks[counter].category = Console.ReadLine();

                        do
                        {
                            Console.Write("Enter the Day of the task (1 to 31): ");
                            tasks[counter].date.day = Convert.ToByte(Console.ReadLine());
                            if (tasks[counter].date.day < 1 || tasks[counter].date.day > 31)
                                Console.WriteLine("Not a valid day!");
                        }
                        while (tasks[counter].date.day < 1 || tasks[counter].date.day > 31);

                        do
                        {
                            Console.Write("Enter the Month of the task (1 to 12): ");
                            tasks[counter].date.month = Convert.ToByte(Console.ReadLine());
                            if (tasks[counter].date.month < 1 || tasks[counter].date.month > 12)
                                Console.WriteLine("Not a valid month!");
                        }
                        while (tasks[counter].date.month < 1 || tasks[counter].date.month > 12);

                        do
                        {
                            Console.Write("Enter the Year of the task: ");
                            tasks[counter].date.year = Convert.ToUInt16(Console.ReadLine());
                            if (tasks[counter].date.year < 1000 || tasks[counter].date.year > 3000)
                                Console.WriteLine("Not a valid year!");
                        }
                        while (tasks[counter].date.year < 1000 || tasks[counter].date.year > 3000);
                        counter++;
                    }
                    else
                        Console.WriteLine("Database full.");
                    break;

                case '2': //Show the tasks between two certain dates
                    if (counter >= 1)
                    {
                        byte startDay, startMonth;
                        ushort startYear;
                        byte endDay, endMonth;
                        ushort endYear;

                        Console.WriteLine("Starting day: ");
                        string number = Console.ReadLine();

                        if (number == "")
                            startDay = Convert.ToByte(DateTime.Now.Day);
                        else
                            startDay = Convert.ToByte(number);

                        Console.WriteLine("Starting month: ");
                        number = Console.ReadLine();
                        if (number == "")
                            startMonth = Convert.ToByte(DateTime.Now.Month);
                        else
                            startMonth = Convert.ToByte(number);

                        Console.WriteLine("Starting year: ");
                        number = Console.ReadLine();
                        if (number == "")
                            startYear = Convert.ToUInt16(DateTime.Now.Year);
                        else
                            startYear = Convert.ToUInt16(number);

                        Console.WriteLine("Final day: ");
                        number = Console.ReadLine();
                        if (number == "")
                            endDay = Convert.ToByte(DateTime.Now.Day);
                        else
                            endDay = Convert.ToByte(number);

                        Console.WriteLine("Final month: ");
                        number = Console.ReadLine();
                        if (number == "")
                            endMonth = Convert.ToByte(DateTime.Now.Month);
                        else
                            endMonth = Convert.ToByte(number);

                        Console.WriteLine("Final year: ");
                        number = Console.ReadLine();
                        if (number == "")
                            endYear = Convert.ToUInt16(DateTime.Now.Year);
                        else
                            endYear = Convert.ToUInt16(number);

                        string startDate = "" + startYear +
                            startMonth.ToString("00") + startDay.ToString("00");
                        string endDate = "" + endYear +
                            endMonth.ToString("00") + endDay.ToString("00");

                        for (int i = 0; i < counter; i++)
                        {
                            string currentDate = "" + tasks[i].date.year +
                                tasks[i].date.month.ToString("00") + tasks[i].date.day.ToString("00");
                            if (currentDate.CompareTo(startDate) >= 0 && currentDate.CompareTo(endDate) <= 0)
                            {
                                Console.WriteLine("The number is {0}: {1}/{2}/" +
                                    "{3} - {4} - {5} - {6}.", i + 1,
                                    tasks[i].date.day, tasks[i].date.month,
                                    tasks[i].date.year, tasks[i].description,
                                    tasks[i].category, tasks[i].level);
                            }
                        }
                    }
                    else
                        Console.WriteLine("Database empty.");
                    break;

                case '3': //Find tasks that contain a certain text
                    if (counter >= 1)
                    {
                        Console.Write("Enter the text to search: ");
                        search = Console.ReadLine();
                        found = false;
                        string newValue5;

                        for (int i = 0; i < counter; i++)
                        {
                            if (tasks[i].description.IndexOf(search) >= 0
                                || tasks[i].category.IndexOf(search) >= 0)
                            {
                                if (tasks[i].description.Length > 50)
                                    newValue5 = tasks[i].description.Substring(0, 50);
                                else
                                    newValue5 = tasks[i].description;

                                found = true;
                                Console.WriteLine("{0}: {1}/{2}/{3} - {4}",
                                    i + 1, tasks[i].date.day, tasks[i].date.month,
                                    tasks[i].date.year, newValue5);
                            }
                        }
                        if (!found)
                            Console.WriteLine("Not found.");
                    }
                    else
                        Console.WriteLine("Database empty.");
                    break;

                case '4': //Update a record
                    if (counter >= 1)
                    {
                        Console.Write("Enter the number of the task to update: ");
                        int update = Convert.ToInt32(Console.ReadLine()) - 1;
                        if ((update >= 0) && (update < counter))
                        {
                            Console.Write("Description ({0}): ",
                                tasks[update].description);
                            newValue = Console.ReadLine();
                            if (newValue != "")
                                tasks[update].description = newValue;

                            Console.WriteLine("Level ({0}): ",
                                tasks[update].level);
                            newValue = Console.ReadLine();
                            if (newValue != "")
                                tasks[update].level = Convert.ToByte(newValue);

                            Console.WriteLine("Category ({0}): ",
                                tasks[update].category);
                            newValue = Console.ReadLine();
                            if (newValue != "")
                                tasks[update].category = newValue;

                            Console.WriteLine("Year ({0}): ",
                                tasks[update].date.year);
                            newValue = Console.ReadLine();
                            if (newValue != "")
                                tasks[update].date.year = Convert.ToUInt16(newValue);

                            Console.WriteLine("Month ({0}): ",
                                tasks[update].date.month);
                            newValue = Console.ReadLine();
                            if (newValue != "")
                                tasks[update].date.month = Convert.ToByte(newValue);

                            Console.WriteLine("Day ({0}): ",
                                tasks[update].date.day);
                            newValue = Console.ReadLine();
                            if (newValue != "")
                                tasks[update].date.day = Convert.ToByte(newValue);
                        }
                        else
                            Console.WriteLine("Wrong number entered.");
                    }
                    else
                        Console.WriteLine("Database empty.");
                    break;

                case '5': //Delete some data, between two positions indicated by the user
                    if (counter >= 1)
                    {
                        Console.Write("Enter the first number of data to delete: ");
                        int delete = Convert.ToInt32(Console.ReadLine()) - 1;
                        Console.Write("Enter the second number of data to delete: ");
                        int delete2 = Convert.ToInt32(Console.ReadLine()) - 1;

                        for (int pos = delete; pos <= delete2; pos++)
                        {
                            for (int i = delete; i < counter; i++)
                                tasks[i] = tasks[i + 1];
                            counter--;
                        }
                    }
                    else
                        Console.WriteLine("Database empty.");
                    break;

                case '6': //Sort the data alphabetically on date + description
                    for (int i = 0; i < counter - 1; i++)
                    {
                        string firstDate = "" + tasks[i].date.year +
                            tasks[i].date.month.ToString("00") +
                            tasks[i].date.day.ToString("00") +
                            tasks[i].description;
                        for (int j = i + 1; j < counter; j++)
                        {
                            string secondDate = "" + tasks[j].date.year +
                                tasks[j].date.month.ToString("00") +
                                tasks[j].date.day.ToString("00") +
                                tasks[j].description;

                            if (firstDate.CompareTo(secondDate) > 0)
                            {
                                TaskType aux = tasks[i];
                                tasks[i] = tasks[j];
                                tasks[j] = aux;
                            }
                        }
                    }
                    break;

                case '7': //Find Duplicates
                    for (int i = 0; i < counter - 1; i++)
                    {
                        for (int j = i + 1; j < counter; j++)
                        {
                            if (tasks[i].description == tasks[j].description)
                            {
                                Console.WriteLine("{0} - {1}/{2}/{3}",
                                    tasks[i].description, tasks[i].date.day,
                                    tasks[i].date.month, tasks[i].date.year);
                                Console.WriteLine("{0} - {1}/{2}/{3}",
                                    tasks[j].description, tasks[j].date.day,
                                    tasks[j].date.month, tasks[j].date.year);
                            }
                        }
                    }
                    break;

                case 'Q': //Quit
                    Console.WriteLine("Quitting...");
                    break;

                default:
                    Console.WriteLine("You entered a wrong option. Please re-enter it.");
                    break;
            }
        }
        while (option != 'Q');
    }
}

More C# Sharp Exercises of Arrays, Structures And Strings

Reverse Array
Create a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order....
Search In Array
Create a C# program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how many data will he...
Array Of Even Numbers
Write a C# program to ask the user for 10 integer numbers and display the even ones....
Array Of Positive And Negative Numbers
Create a C# program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones....
Many Numbers And Sum
Create a program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must d...
Two Dimensional Array
Write a C# program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group....
Statistics V2
reate a statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been entere...
Struct
Create a "struct" to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (red colour, byte) ...
Array Of Struct
Expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the user for data for the first...
Array Of Struct And Menu
Expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data for one point - Display al...
Books Database
Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information: Title Author...
Triangle V2
Write a C# program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length: ...
Rectangle V3
Write a C# program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Enter size: 4 YoYoYoY...
Centered Triangle
Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
Cities Database
Create a database to store information about cities. In a first approach, we will store only the name of each city and the number of inhabitants, a...
Banner
Create a C# program to imitate the basic Unix SysV "banner" utility, able to display big texts....
Triangle Right Side
Create a C# program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
Strings Manipulation
Create a C# program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded with a space - Disp...
Nested Structs
Create a struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month and ...
Sort Data
Create a C# program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
Two Dimensional Array As Buffer For Screen
Create a C# program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays th...
Two Dimensional Array 2: Circunference On Screen
Create a C# program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it ...
Computer Programs
Create a C# program that can store up to 1000 records of computer programs. For each program, you must keep the following data: * Name * Category ...
Household Accounts
Create a program in C# that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income), shou...