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