Group
C# Arrays, Structures and Strings
Objective
1. Create a database to store information about up to 500 cities, where each record contains the city name and the number of inhabitants.
2. Implement a menu with options to:
- Add a new city.
- View all cities.
- Modify an existing city record.
- Insert a new record at a specific position.
- Delete a record and adjust the array accordingly.
- Search records by city name (case-insensitive).
- Correct capitalization in city names (capitalize the first letter and any following a space).
3. Allow the user to select options from the menu, and ensure proper handling of edge cases such as attempting to add a city when the database is full or deleting the last entry.
Create a database to store information about cities.
Example C# Exercise
Show C# Code
using System;
class CityDatabase
{
// Define a struct for a City to store name and population
struct City
{
public string Name;
public int Inhabitants;
}
// Define the maximum number of cities
static int MaxCities = 500;
static City[] cities = new City[MaxCities];
static int cityCount = 0;
static void Main()
{
// Main menu loop
while (true)
{
Console.Clear();
Console.WriteLine("City Database Menu:");
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 names");
Console.WriteLine("0 - Exit");
Console.Write("Choose an option: ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
AddCity();
break;
case "2":
ViewCities();
break;
case "3":
ModifyCity();
break;
case "4":
InsertCity();
break;
case "5":
DeleteCity();
break;
case "6":
SearchCity();
break;
case "7":
CorrectCapitalization();
break;
case "0":
return;
default:
Console.WriteLine("Invalid option, please try again.");
break;
}
}
}
static void AddCity()
{
if (cityCount < MaxCities)
{
Console.Write("Enter city name: ");
string name = Console.ReadLine();
Console.Write("Enter number of inhabitants: ");
int inhabitants = int.Parse(Console.ReadLine());
cities[cityCount].Name = name;
cities[cityCount].Inhabitants = inhabitants;
cityCount++;
Console.WriteLine("City added successfully!");
}
else
{
Console.WriteLine("Database is full, cannot add more cities.");
}
Console.ReadKey();
}
static void ViewCities()
{
Console.WriteLine("City List:");
for (int i = 0; i < cityCount; i++)
{
Console.WriteLine($"{cities[i].Name} - {cities[i].Inhabitants} inhabitants");
}
Console.ReadKey();
}
static void ModifyCity()
{
Console.Write("Enter the city number to modify (0 to {0}): ", cityCount - 1);
int index = int.Parse(Console.ReadLine());
if (index >= 0 && index < cityCount)
{
Console.Write("Enter new city name: ");
cities[index].Name = Console.ReadLine();
Console.Write("Enter new number of inhabitants: ");
cities[index].Inhabitants = int.Parse(Console.ReadLine());
Console.WriteLine("City record updated successfully!");
}
else
{
Console.WriteLine("Invalid city number.");
}
Console.ReadKey();
}
static void InsertCity()
{
Console.Write("Enter position to insert a new city (0 to {0}): ", cityCount);
int position = int.Parse(Console.ReadLine());
if (position >= 0 && position <= cityCount && cityCount < MaxCities)
{
for (int i = cityCount; i > position; i--)
{
cities[i] = cities[i - 1]; // Shift cities to the right
}
Console.Write("Enter city name: ");
cities[position].Name = Console.ReadLine();
Console.Write("Enter number of inhabitants: ");
cities[position].Inhabitants = int.Parse(Console.ReadLine());
cityCount++;
Console.WriteLine("City inserted successfully!");
}
else
{
Console.WriteLine("Invalid position or database is full.");
}
Console.ReadKey();
}
static void DeleteCity()
{
Console.Write("Enter city number to delete (0 to {0}): ", cityCount - 1);
int index = int.Parse(Console.ReadLine());
if (index >= 0 && index < cityCount)
{
for (int i = index; i < cityCount - 1; i++)
{
cities[i] = cities[i + 1]; // Shift cities to the left
}
cityCount--;
Console.WriteLine("City deleted successfully!");
}
else
{
Console.WriteLine("Invalid city number.");
}
Console.ReadKey();
}
static void SearchCity()
{
Console.Write("Enter a search term: ");
string searchTerm = Console.ReadLine().ToLower();
Console.WriteLine("Search Results:");
for (int i = 0; i < cityCount; i++)
{
if (cities[i].Name.ToLower().Contains(searchTerm))
{
Console.WriteLine($"{cities[i].Name} - {cities[i].Inhabitants} inhabitants");
}
}
Console.ReadKey();
}
static void CorrectCapitalization()
{
for (int i = 0; i < cityCount; i++)
{
cities[i].Name = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(cities[i].Name.ToLower());
}
Console.WriteLine("Capitalization corrected for all cities.");
Console.ReadKey();
}
}
Output
City Database Menu:
1 - Add a new city
2 - View all cities
3 - Modify a record
4 - Insert a new record
5 - Delete a record
6 - Search in the records
7 - Correct the capitalization of names
0 - Exit
Choose an option: 1
Enter city name: Madrid
Enter number of inhabitants: 3200000
City added successfully!
City Database Menu:
1 - Add a new city
2 - View all cities
3 - Modify a record
4 - Insert a new record
5 - Delete a record
6 - Search in the records
7 - Correct the capitalization of names
0 - Exit
Choose an option: 2
City List:
Madrid - 3200000 inhabitants