Household accounts C# Exercise - C# Programming Course

 Exercise

Household accounts

 Objetive

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), should be allowed to save the following information:

• Date (8 characters: YYYYMMDD format)
• Description of expenditure or revenue
• Category
• Amount (if positive income, negative if an expense)

The program should allow the user to perform the following operations:

1 - Add a new expense (the date should "look right": day 01 to 31 months from 01 to 12 years between 1000 and 3000). The description must not be empty. Needless to validate the other data.

2 - Show all expenses of a certain category (eg, "studies") between two certain dates (eg between "20110101" and "20111231"). Number is displayed, date (format DD / MM / YYYY), description, category in parentheses, and amount to two decimal places, all in the same line, separated by hyphens. At the end of all data show the total amount of data displayed.

3 - Search costs containing a certain text (in the description or category without distinguishing case sensitive). Number is displayed, the date and description (the description is displayed in the sixth truncated blank, if any spaces six or more).

4 - Modify a tab (tab number prompt the user, it will show the previous value of each field and press Enter to not be able to modify any of the data). Should be advised (but not re-order) if the user enters a wrong card number. Needless to validate any data.

5 - Delete some data, from the number that you enter. Should be advised (but not re-order) if you enter an incorrect number. It should show the card to be clear and prompt prior to deletion.

6 - Sort data alphabetically, by date and (if matched) description.

7 - Normalize descriptions: remove trailing spaces, spaces and mirror sites. If a description is all uppercase, will be converted to lowercase (except for the first letter, kept in uppercase).

T-End the use of the application (as we store the information, the data will be lost).

 Example Code

using System;
public class exercise96
{
    struct accountData
    {
        public string date;
        public string description;
        public string category;
        public double amount;
    }

    public static void Main()
    {
        int capacity = 10000;
        accountData[] data = new accountData[capacity];
        bool repeat = true;
        string option;
        int amountOfData = 0;

        do
        {
            Console.WriteLine();
            Console.WriteLine("Household accounts");
            Console.WriteLine();
            Console.WriteLine("1.- Add data.");
            Console.WriteLine("2.- View all data.");
            Console.WriteLine("3.- Search data.");
            Console.WriteLine("4.- Modify data.");
            Console.WriteLine("5.- Delete data.");
            Console.WriteLine("6.- Sort alphabetically");
            Console.WriteLine("7.- Fix spaces");
            Console.WriteLine("Q,T.-Quit.");
            Console.Write("Option: ");
            option = Console.ReadLine();

            switch (option)
            {
                case "1": //add
                    if (amountOfData > capacity - 1)
                        Console.WriteLine("Database full!");
                    else
                    {
                        do
                        {
                            Console.Write("Enter date (YYYYMMDD): ");
                            data[amountOfData].date = Console.ReadLine();
                            // TODO: Individual validation for the date
                        } while (data[amountOfData].date.Length == 0);

                        do
                        {
                            Console.Write("Enter Description: ");
                            data[amountOfData].description = Console.ReadLine();
                            if (data[amountOfData].description.Length == 0)
                                Console.Write("Cannot be empty");
                        } while (data[amountOfData].description.Length == 0);

                        Console.Write("Enter category: ");
                        data[amountOfData].category = Console.ReadLine();

                        Console.Write("Enter the amount: ");
                        data[amountOfData].amount = Convert.ToDouble(Console.ReadLine());

                        amountOfData++;
                    }
                    break;

                case "2": //view
                    if (amountOfData == 0)
                        Console.WriteLine("No data!");
                    else
                    {
                        Console.Write("Enter the category: ");
                        string categ = Console.ReadLine();
                        Console.Write("Enter the start date (YYYYMMDD): ");
                        string startDate = Console.ReadLine();
                        Console.Write("Enter the end date (YYYYMMDD): ");
                        string endDate = Console.ReadLine();

                        for (int i = 0; i < amountOfData; i++)
                        {
                            if ((data[i].category == categ) &&
                                (data[i].date.CompareTo(startDate) >= 0) &&
                                (data[i].date.CompareTo(endDate) <= 0))
                            {
                                Console.WriteLine("{0} - {1}/{2}/{3} - {4} -({5}) - {6}",
                                    i + 1,
                                    data[i].date.Substring(6, 2), // Day
                                    data[i].date.Substring(4, 2), // Month
                                    data[i].date.Substring(0, 4), // Year
                                    data[i].description,
                                    data[i].category,
                                    data[i].amount.ToString("N2"));
                            }
                        }
                    }
                    break;

                case "3": //search
                    Console.Write("Enter part of the description or category: ");
                    string search = Console.ReadLine().ToUpper();
                    bool found = false;
                    for (int i = 0; i < amountOfData; i++)
                    {
                        if (data[i].description.ToUpper().Contains(search)
                                || data[i].category.ToUpper().Contains(search))
                        {
                            Console.WriteLine("{0}: {1} - {2}",
                                i + 1,
                                data[i].date,
                                data[i].description);
                            // TODO: Split in sixth space
                            found = true;
                        }
                    }
                    if (!found)
                        Console.WriteLine("Not found!");
                    break;

                case "4":  // modify
                    Console.Write("Enter the record number: ");
                    int recNumber = Convert.ToInt32(Console.ReadLine()) - 1;

                    if ((recNumber > amountOfData) || (recNumber < 0))
                        Console.Write("Out of range!");
                    else
                    {
                        Console.Write("Date (was {0}; hit ENTER to leave as is): ",
                            data[recNumber].date);
                        string newText = Console.ReadLine();
                        if (newText != "")
                            data[recNumber].date = newText;

                        Console.Write("Description (was {0}; hit ENTER to leave as is): ",
                            data[recNumber].description);
                        newText = Console.ReadLine();
                        if (newText != "")
                            data[recNumber].description = newText;

                        Console.Write("Category (was {0}; hit ENTER to leave as is): ",
                            data[recNumber].category);
                        newText = Console.ReadLine();
                        if (newText != "")
                            data[recNumber].category = newText;

                        Console.Write("Amount (was {0}; hit ENTER to leave as is): ",
                            data[recNumber].amount);
                        newText = Console.ReadLine();
                        if (newText != "")
                            data[recNumber].amount = Convert.ToDouble(newText);
                    }
                    break;

                case "5": //delete
                    int position = 0;
                    Console.Write("Enter the position number to delete: ");
                    position = Convert.ToInt32(Console.ReadLine()) - 1;
                    if (position > amountOfData)
                        Console.WriteLine("Error: out of range");
                    else
                    {
                        // TODO: Ask for confirmation
                        for (int i = position; i < amountOfData; i++)
                            data[i] = data[i + 1];
                        amountOfData--;
                    }
                    break;

                case "6": // Sort
                    accountData aux;
                    for (int i = 0; i < amountOfData - 1; i++)
                    {
                        for (int j = i + 1; j < amountOfData; j++)
                        {
                            string data1 = data[i].date + data[i].description;
                            string data2 = data[j].date + data[j].description;
                            if (data1.CompareTo(data2) > 0)
                            {
                                aux = data[i];
                                data[i] = data[j];
                                data[j] = aux;
                            }
                        }
                    }
                    Console.WriteLine("Sorted.");
                    break;

                case "7": //replace "  " x " "
                    for (int i = 0; i < amountOfData; i++)
                    {
                        data[i].description = data[i].description.Trim();
                        while (data[i].description.Contains("  "))
                            data[i].description = data[i].description.Replace("  ", " ");
                        if (data[i].description == data[i].description.ToUpper())
                            data[i].description = data[i].description.Substring(0, 1).ToUpper()
                                + data[i].description.Substring(1).ToLower();
                    }
                    break;

                case "T":
                case "t":
                case "Q":
                case "q":
                    repeat = false;
                    break;

                default:
                    Console.WriteLine("Wrong option!");
                    break;
            }
        } while (repeat != false);
        Console.WriteLine("Bye!");
    }
}

More C# 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 ...
 Exercise tasks
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 an...

Juan A. Ripoll - Programming Tutorials and Courses © 2024 All rights reserved.  Legal Conditions.