Exercise
Cities database
Objetive
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, and allocate space for up to 500 cities.
The menu should include the following options:
1 .- Add a new city (at the end of the existing data)
2 .- View all cities (name and inhabitants)
3 .- Modify a record (rename and / or change number of inhabitants)
4 .- Insert a new record (in a specified position, moving the following ones to the right)
5 .- Delete a record (moving the following ones to the left so that no empty spaces are left)
6 .- Search in the records (display the ones which contain a certain text in their name, whether in upper or lower case, using partial search)
7 .- Correct the capitalization of the names (turn into uppercase the first letter and the ones after a space, and make the rest lowercase).
0 .- Exit
Code
Imports System
Public Class exercise86
Structure city
Public name As String
Public inhabitants As UInteger
End Structure
Public Shared Sub Main()
Dim maxCities As Integer = 500
Dim cities As city() = New city(maxCities - 1) {}
Dim amount As Integer = 0
Dim currentCityNumber As Integer
Dim [option] As String
Dim textToSearch As String
Dim found As Boolean
Dim textToModify As String
Dim finished As Boolean = False
Do
Console.WriteLine()
Console.WriteLine("Cities database")
Console.WriteLine()
Console.WriteLine("1.- Add a new city")
Console.WriteLine("2.- View all cities")
Console.WriteLine("3.- Modify a record")
Console.WriteLine("4.- Insert a new record")
Console.WriteLine("5.- Delete a record")
Console.WriteLine("6.- Search in the records")
Console.WriteLine("7.- Correct the capitalization of the names")
Console.WriteLine("0.- Exit")
Console.WriteLine()
Console.Write("Choose an option: ")
[option] = Console.ReadLine()
Select Case [option]
Case "0"
finished = True
Case "1"
If amount > maxCities - 1 Then
Console.WriteLine("the database is full")
Else
Console.WriteLine("Entering data for city number {0}", amount + 1)
Console.Write("Enter the city name: ")
cities(amount).name = Console.ReadLine()
Console.Write("Enter the inhabitants numbers: ")
cities(amount).inhabitants = Convert.ToUInt32(Console.ReadLine())
Console.WriteLine("The data was entered correctly")
amount += 1
End If
Case "2"
For i As Integer = 0 To amount - 1
Console.WriteLine("{0}: {1}, {2} inhabitants", i + 1, cities(i).name, cities(i).inhabitants)
Next
Console.WriteLine()
Case "3"
Console.Write("Enter the city number: ")
currentCityNumber = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Enter a new data for a city number: {0}", currentCityNumber)
Console.Write("City name (was {0}; hit ENTER to leave as is): ", cities(currentCityNumber - 1).name)
textToModify = Console.ReadLine()
If textToModify <> "" Then cities(currentCityNumber - 1).name = textToModify
Console.Write("Inhabitants (was {0}; hit ENTER to leave as is): ", cities(currentCityNumber - 1).inhabitants)
textToModify = Console.ReadLine()
If textToModify <> "" Then cities(currentCityNumber - 1).inhabitants = Convert.ToUInt32(textToModify)
Console.WriteLine()
Case "4"
If amount > maxCities - 1 Then
Console.WriteLine("The database is full")
Else
Console.Write("Enter the number of the city to modify: ")
currentCityNumber = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Insert a new data at {0} position: ", currentCityNumber)
amount += 1
For i As Integer = CInt(amount) To currentCityNumber - 1 + 1
cities(i) = cities(i - 1)
Next
Console.Write("City name: ")
cities(currentCityNumber - 1).name = Console.ReadLine()
Console.Write("Inhabitants: ")
cities(currentCityNumber - 1).inhabitants = Convert.ToUInt32(Console.ReadLine())
End If
Case "5"
Console.Write("Enter the city number for delete: ")
currentCityNumber = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Deleting the number {0}", currentCityNumber)
For i As Integer = currentCityNumber - 1 To amount - 1
cities(i) = cities(i + 1)
Next
amount -= 1
Case "6"
Console.Write("Enter the text to search: ")
textToSearch = Console.ReadLine()
found = False
For i As Integer = 0 To amount - 1
If cities(i).name.ToUpper().IndexOf(textToSearch.ToUpper()) >= 0 Then
Console.WriteLine("{0} found in {1}", textToSearch, cities(i).name)
found = True
End If
Next
If Not found Then Console.WriteLine("Not found.")
Case "7"
For i As Integer = 0 To amount - 1
Dim lowerCaseName As String = cities(i).name.ToLower()
Dim correctedName As String = lowerCaseName.Substring(0, 1).ToUpper() & lowerCaseName.Substring(1)
For j As Integer = 1 To correctedName.Length - 2 - 1
If correctedName(j) = " "c Then correctedName = correctedName.Substring(0, j) & " " & correctedName.Substring(j + 1, 1).ToUpper() & correctedName.Substring(j + 2)
Next
cities(i).name = correctedName
Next
Case Else
Console.WriteLine("Wrong option ")
End Select
Loop While Not finished
End Sub
End Class