using System; // Importing the System namespace for accessing Console, Math, and other functionalities
class Program
{
// Define the Program struct to store the program details
struct ProgramRecord
{
public string Name;
public string Category;
public string Description;
public (string VersionNumber, byte LaunchMonth, ushort LaunchYear) Version;
}
static void Main()
{
// Create an array to hold up to 1000 program records
ProgramRecord[] programs = new ProgramRecord[1000];
int programCount = 0; // Keeps track of how many programs have been added
while (true)
{
Console.Clear();
Console.WriteLine("Program Management System");
Console.WriteLine("1 - Add a new program");
Console.WriteLine("2 - Show all program names");
Console.WriteLine("3 - View details of a program");
Console.WriteLine("4 - Update a record");
Console.WriteLine("5 - Delete a record");
Console.WriteLine("6 - Sort by program name");
Console.WriteLine("7 - Fix redundant spaces in names");
Console.WriteLine("X - Exit");
Console.Write("Enter an option: ");
string choice = Console.ReadLine();
switch (choice.ToUpper())
{
case "1":
AddProgram(ref programs, ref programCount);
break;
case "2":
ShowProgramNames(programs, programCount);
break;
case "3":
ViewProgramDetails(programs, programCount);
break;
case "4":
UpdateRecord(programs, programCount);
break;
case "5":
DeleteRecord(ref programs, ref programCount);
break;
case "6":
SortProgramsByName(ref programs, programCount);
break;
case "7":
FixRedundantSpaces(ref programs, programCount);
break;
case "X":
return;
default:
Console.WriteLine("Invalid option. Try again.");
break;
}
}
}
static void AddProgram(ref ProgramRecord[] programs, ref int programCount)
{
if (programCount >= 1000)
{
Console.WriteLine("Error: Program limit reached.");
return;
}
// Get program details from the user
string name;
do
{
Console.Write("Enter program name: ");
name = Console.ReadLine();
} while (string.IsNullOrWhiteSpace(name));
string category;
do
{
Console.Write("Enter category (max 30 characters): ");
category = Console.ReadLine();
} while (category.Length > 30);
Console.Write("Enter description (max 100 characters): ");
string description = Console.ReadLine();
if (description.Length > 100) description = description.Substring(0, 100);
Console.Write("Enter version number: ");
string versionNumber = Console.ReadLine();
Console.Write("Enter launch month (1-12): ");
byte launchMonth = byte.Parse(Console.ReadLine());
Console.Write("Enter launch year: ");
ushort launchYear = ushort.Parse(Console.ReadLine());
// Store the program
programs[programCount] = new ProgramRecord
{
Name = name,
Category = category,
Description = description,
Version = (versionNumber, launchMonth, launchYear)
};
programCount++;
Console.WriteLine("Program added successfully!");
}
static void ShowProgramNames(ProgramRecord[] programs, int programCount)
{
if (programCount == 0)
{
Console.WriteLine("No programs stored.");
return;
}
int index = 0;
while (index < programCount)
{
for (int i = index; i < Math.Min(index + 20, programCount); i++)
{
Console.WriteLine(programs[i].Name);
}
if (index + 20 < programCount)
{
Console.WriteLine("Press Enter to see the next 20 programs...");
Console.ReadLine();
}
index += 20;
}
}
static void ViewProgramDetails(ProgramRecord[] programs, int programCount)
{
Console.Write("Enter part of the name, category, or description to search: ");
string searchQuery = Console.ReadLine().ToLower();
bool found = false;
for (int i = 0; i < programCount; i++)
{
if (programs[i].Name.ToLower().Contains(searchQuery) ||
programs[i].Category.ToLower().Contains(searchQuery) ||
programs[i].Description.ToLower().Contains(searchQuery))
{
Console.WriteLine($"Program {i + 1}:");
Console.WriteLine($" Name: {programs[i].Name}");
Console.WriteLine($" Category: {programs[i].Category}");
Console.WriteLine($" Description: {programs[i].Description}");
Console.WriteLine($" Version: {programs[i].Version.VersionNumber}, {programs[i].Version.LaunchMonth}/{programs[i].Version.LaunchYear}");
Console.WriteLine();
found = true;
}
}
if (!found)
{
Console.WriteLine("No programs found matching the search query.");
}
}
static void UpdateRecord(ProgramRecord[] programs, int programCount)
{
Console.Write("Enter the program number to update: ");
int programNumber = int.Parse(Console.ReadLine()) - 1;
if (programNumber < 0 || programNumber >= programCount)
{
Console.WriteLine("Invalid program number.");
return;
}
Console.WriteLine("Current data:");
Console.WriteLine($"Name: {programs[programNumber].Name}");
Console.WriteLine($"Category: {programs[programNumber].Category}");
Console.WriteLine($"Description: {programs[programNumber].Description}");
Console.WriteLine($"Version: {programs[programNumber].Version.VersionNumber}, {programs[programNumber].Version.LaunchMonth}/{programs[programNumber].Version.LaunchYear}");
Console.Write("Enter new name (or press Enter to keep current): ");
string newName = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newName)) programs[programNumber].Name = newName;
Console.Write("Enter new category (or press Enter to keep current): ");
string newCategory = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newCategory)) programs[programNumber].Category = newCategory;
Console.Write("Enter new description (or press Enter to keep current): ");
string newDescription = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newDescription)) programs[programNumber].Description = newDescription;
Console.Write("Enter new version number (or press Enter to keep current): ");
string newVersionNumber = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newVersionNumber)) programs[programNumber].Version.VersionNumber = newVersionNumber;
Console.Write("Enter new launch month (or press Enter to keep current): ");
byte newLaunchMonth;
if (byte.TryParse(Console.ReadLine(), out newLaunchMonth)) programs[programNumber].Version.LaunchMonth = newLaunchMonth;
Console.Write("Enter new launch year (or press Enter to keep current): ");
ushort newLaunchYear;
if (ushort.TryParse(Console.ReadLine(), out newLaunchYear)) programs[programNumber].Version.LaunchYear = newLaunchYear;
Console.WriteLine("Program updated successfully!");
}
static void DeleteRecord(ref ProgramRecord[] programs, ref int programCount)
{
Console.Write("Enter the program number to delete: ");
int programNumber = int.Parse(Console.ReadLine()) - 1;
if (programNumber < 0 || programNumber >= programCount)
{
Console.WriteLine("Invalid program number.");
return;
}
for (int i = programNumber; i < programCount - 1; i++)
{
programs[i] = programs[i + 1];
}
programCount--;
Console.WriteLine("Program deleted successfully!");
}
static void SortProgramsByName(ref ProgramRecord[] programs, int programCount)
{
Array.Sort(programs, 0, programCount, Comparer.Create((a, b) => a.Name.CompareTo(b.Name)));
Console.WriteLine("Programs sorted alphabetically by name.");
}
static void FixRedundantSpaces(ref ProgramRecord[] programs, int programCount)
{
for (int i = 0; i < programCount; i++)
{
programs[i].Name = System.Text.RegularExpressions.Regex.Replace(programs[i].Name, @"\s+", " ");
}
Console.WriteLine("Redundant spaces in program names fixed.");
}
}