We need to maintain the list of RTO districts. e.g. MH01 – Mumbai, MH 04 – Thane etc. Write a program which uses a Hashtable to maintain this list.
Task 1: Create a console application to maintain the list. It should display a menu to perform the following tasks:
• Add Record in Hashtable
• Search record
• Display All Records
• To display Total count of Records at any point
• Remove any particular record
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace C_SHARP
{
class Program
{
static void Main(string[] args)
{
Hashtable districts = new Hashtable();
int choice = 0;
while (choice != 6)
{
Console.WriteLine("1. Add a new record in hashtable");
Console.WriteLine("2. Search a record");
Console.WriteLine("3. Display all records");
Console.WriteLine("4. Display total count of records");
Console.WriteLine("5. Remove particular record");
Console.WriteLine("6. Exit");
Console.Write("Your choice: ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
{
Console.Write("Enter the district number (Example MH01): ");
string districtNumber = Console.ReadLine();
Console.Write("Enter the district name: ");
string districtName = Console.ReadLine();
districts.Add(districtNumber, districtName);
Console.WriteLine("\nThe district has been added!\n");
}
break;
case 2:
{
Console.Write("Enter the district number to search: ");
string districtNumber = Console.ReadLine();
bool exist = false;
foreach (string key in districts.Keys)
{
if (key == districtNumber)
{
Console.WriteLine(string.Format("{0,-20}{1,-20}", "District Number", "Name"));
Console.WriteLine(string.Format("{0,-20}{1,-20}", key, districts[key]));
exist = true;
break;
}
}
if (!exist)
{
Console.WriteLine("\nThe district does not exist!\n");
}
}
break;
case 3:
{
Console.WriteLine("All districts");
Console.WriteLine(string.Format("{0,-20}{1,-20}", "District Number", "Name"));
foreach (DictionaryEntry d in districts)
{
Console.WriteLine(string.Format("{0,-20}{1,-20}", d.Key, d.Value));
}
}
break;
case 4:
Console.WriteLine("The total number of records is {0}", districts.Count);
break;
case 5:
{
Console.Write("Enter the district number to delete: ");
string districtNumber = Console.ReadLine();
if (districts.ContainsKey(districtNumber) || districts.ContainsValue(districtNumber))
{
districts.Remove(districtNumber);
Console.WriteLine("\nThe district has been deleted!\n");
}
else {
Console.WriteLine("\nThe district does not exist!\n");
}
}
break;
case 6:
break;
default:
Console.WriteLine("Wrong menu item. Try again.");
break;
}
}
Console.ReadKey();
}
}
}
Comments
Leave a comment