Household accounts VB.Net Exercise - Visual Basic Programming Course


 Exercise

Household accounts

Objetive

Write a Visual Basic (VB.Net) program in Visual Basic (VB.Net) 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).

Code

Imports System
Public Class exercise96
    Structure accountData
        Public date As String
        Public description As String
        Public category As String
        Public amount As Double
    End Structure

    Public Shared Sub Main()
        Dim capacity As Integer = 10000
        Dim data As accountData() = New accountData(capacity - 1) {}
        Dim repeat As Boolean = True
        Dim [option] As String
        Dim amountOfData As Integer = 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()

            Select Case [option]
                Case "1"

                    If amountOfData > capacity - 1 Then
                        Console.WriteLine("Database full!")
                    Else

                        Do
                            Console.Write("Enter date (YYYYMMDD): ")
                            data(amountOfData).date = Console.ReadLine()
                        Loop While data(amountOfData).date.Length = 0

                        Do
                            Console.Write("Enter Description: ")
                            data(amountOfData).description = Console.ReadLine()
                            If data(amountOfData).description.Length = 0 Then Console.Write("Cannot be empty")
                        Loop 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 += 1
                    End If

                Case "2"

                    If amountOfData = 0 Then
                        Console.WriteLine("No data!")
                    Else
                        Console.Write("Enter the category: ")
                        Dim categ As String = Console.ReadLine()
                        Console.Write("Enter the start date (YYYYMMDD): ")
                        Dim startDate As String = Console.ReadLine()
                        Console.Write("Enter the end date (YYYYMMDD): ")
                        Dim endDate As String = Console.ReadLine()

                        For i As Integer = 0 To amountOfData - 1

                            If (data(i).category = categ) AndAlso (data(i).date.CompareTo(startDate) >= 0) AndAlso (data(i).date.CompareTo(endDate) <= 0) Then
                                Console.WriteLine("{0} - {1}/{2}/{3} - {4} -({5}) - {6}", i + 1, data(i).date.Substring(6, 2), data(i).date.Substring(4, 2), data(i).date.Substring(0, 4), data(i).description, data(i).category, data(i).amount.ToString("N2"))
                            End If
                        Next
                    End If

                Case "3"
                    Console.Write("Enter part of the description or category: ")
                    Dim search As String = Console.ReadLine().ToUpper()
                    Dim found As Boolean = False

                    For i As Integer = 0 To amountOfData - 1

                        If data(i).description.ToUpper().Contains(search) OrElse data(i).category.ToUpper().Contains(search) Then
                            Console.WriteLine("{0}: {1} - {2}", i + 1, data(i).date, data(i).description)
                            found = True
                        End If
                    Next

                    If Not found Then Console.WriteLine("Not found!")
                Case "4"
                    Console.Write("Enter the record number: ")
                    Dim recNumber As Integer = Convert.ToInt32(Console.ReadLine()) - 1

                    If (recNumber > amountOfData) OrElse (recNumber < 0) Then
                        Console.Write("Out of range!")
                    Else
                        Console.Write("Date (was {0}; hit ENTER to leave as is): ", data(recNumber).date)
                        Dim newText As String = Console.ReadLine()
                        If newText <> "" Then data(recNumber).date = newText
                        Console.Write("Description (was {0}; hit ENTER to leave as is): ", data(recNumber).description)
                        newText = Console.ReadLine()
                        If newText <> "" Then data(recNumber).description = newText
                        Console.Write("Category (was {0}; hit ENTER to leave as is): ", data(recNumber).category)
                        newText = Console.ReadLine()
                        If newText <> "" Then data(recNumber).category = newText
                        Console.Write("Amount (was {0}; hit ENTER to leave as is): ", data(recNumber).amount)
                        newText = Console.ReadLine()
                        If newText <> "" Then data(recNumber).amount = Convert.ToDouble(newText)
                    End If

                Case "5"
                    Dim position As Integer = 0
                    Console.Write("Enter the position number to delete: ")
                    position = Convert.ToInt32(Console.ReadLine()) - 1

                    If position > amountOfData Then
                        Console.WriteLine("Error: out of range")
                    Else

                        For i As Integer = position To amountOfData - 1
                            data(i) = data(i + 1)
                        Next

                        amountOfData -= 1
                    End If

                Case "6"
                    Dim aux As accountData

                    For i As Integer = 0 To amountOfData - 1 - 1

                        For j As Integer = i + 1 To amountOfData - 1
                            Dim data1 As String = data(i).date & data(i).description
                            Dim data2 As String = data(j).date & data(j).description

                            If data1.CompareTo(data2) > 0 Then
                                aux = data(i)
                                data(i) = data(j)
                                data(j) = aux
                            End If
                        Next
                    Next

                    Console.WriteLine("Sorted.")
                Case "7"

                    For i As Integer = 0 To amountOfData - 1
                        data(i).description = data(i).description.Trim()

                        While data(i).description.Contains("  ")
                            data(i).description = data(i).description.Replace("  ", " ")
                        End While

                        If data(i).description = data(i).description.ToUpper() Then data(i).description = data(i).description.Substring(0, 1).ToUpper() & data(i).description.Substring(1).ToLower()
                    Next

                Case "T", "t", "Q", "q"
                    repeat = False
                Case Else
                    Console.WriteLine("Wrong option!")
            End Select
        Loop While repeat <> False

        Console.WriteLine("Bye!")
    End Sub
End Class

More VB.Net Exercises of Arrays, Structures and Strings

 Reverse array
Write a Visual Basic (VB.Net) program to ask the user for 5 numbers, store them in an array and show them in reverse order....
 Search in array
Write a Visual Basic (VB.Net) program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how...
 Array of even numbers
Write a Visual Basic (VB.Net) program to ask the user for 10 integer numbers and display the even ones....
 Array of positive and negative numbers
Write a Visual Basic (VB.Net) program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative...
 Many numbers and sum
Write a Visual Basic (VB.Net) program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is go...
 Two dimensional array
Write a Visual Basic (VB.Net) program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average...
 Statistics V2
Write a Visual Basic (VB.Net) statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whe...
 Struct
Write a Visual Basic (VB.Net) Struct to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (...
 Array of struct
Write a Visual Basic (VB.Net) program that expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of s...
 Array of struct and menu
Write a Visual Basic (VB.Net) program that expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to...
 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 Visual Basic (VB.Net) program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has...
 Rectangle V3
Write a Visual Basic (VB.Net) program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Ent...
 Centered triangle
Write a Visual Basic (VB.Net) program that 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
Write a Visual Basic (VB.Net) program to imitate the basic Unix SysV "banner" utility, able to display big texts....
 Triangle right side
Write a Visual Basic (VB.Net) program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
 Strings manipulation
Write a Visual Basic (VB.Net) program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded wi...
 Nested structs
Write a Visual Basic (VB.Net) Struct to store two data for a person: name and date of birth. The date of birth must be another struct consisti...
 Sort data
Write a Visual Basic (VB.Net) 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
Write a Visual Basic (VB.Net) program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positio...
 Two dimensional array 2: circunference on screen
Write a Visual Basic (VB.Net) program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it...
 Computer programs
Write a Visual Basic (VB.Net) program that can store up to 1000 records of computer programs. For each program, you must keep the following data: *...
 Exercise tasks
Write a Visual Basic (VB.Net) program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 d...


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