Household accounts Learn programming Visual Basic (VB.net)

Lesson:

Arrays, Structures and Strings


Exercise:

Household accounts


Objetive:

Create a 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

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