using System;
public class ComputerPrograms
{
struct versionType
{
public string number;
public byte month;
public ushort year;
}
struct program
{
public string name;
public string category;
public string description;
public versionType version;
}
public static void Main()
{
int capacity = 1000;
program[] data = new program[capacity];
bool repeat = true;
string option;
int counter = 0;
do
{
Console.WriteLine();
Console.WriteLine("Computer programas database");
Console.WriteLine();
Console.WriteLine("1.- Add data.");
Console.WriteLine("2.- View names of the programs.");
Console.WriteLine("3.- Search programs.");
Console.WriteLine("4.- Modify program.");
Console.WriteLine("5.- Delete Program.");
Console.WriteLine("6.- Sort alphabetically");
Console.WriteLine("7.- Fix redundant spaces");
Console.WriteLine("0.-Exit.");
Console.Write("Option: ");
option = Console.ReadLine();
switch (option)
{
case "1": //add
if (counter > capacity - 1)
{
Console.WriteLine("Database full!");
break;
}
do
{
Console.Write("Enter Name: ");
data[counter].name = Console.ReadLine();
if (data[counter].name.Length == 0)
Console.Write("Cannot be empty");
} while (data[counter].name.Length == 0);
do
{
Console.Write("Enter category: ");
data[counter].category = Console.ReadLine();
if (data[counter].category.Length > 30)
Console.Write("Too long. 30 letters max, please");
} while (data[counter].category.Length > 30);
Console.Write("Enter Description: ");
data[counter].description = Console.ReadLine();
if (data[counter].description.Length > 100)
data[counter].description = data[counter].description.Substring(0, 100);
Console.Write("Enter the version number: ");
data[counter].version.number = Console.ReadLine();
Console.Write("Enter the version month: ");
data[counter].version.month = Convert.ToByte(Console.ReadLine());
Console.Write("Enter the version year: ");
data[counter].version.year = Convert.ToUInt16(Console.ReadLine());
counter++;
break;
case "2": //view
if (counter == 0)
Console.WriteLine("No data!");
else
for (int i = 0; i < counter; i++)
{
Console.WriteLine("{0}: {1}", i + 1, data[i].name);
if (i % 20 == 19)
{
Console.Write("Press Enter...");
Console.ReadLine();
}
}
break;
case "3": //search
Console.Write("Enter part of the name, description, etc... (case sensitive): ");
string search = Console.ReadLine();
bool found = false;
for (int i = 0; i < counter; i++)
{
if (data[i].name.Contains(search)
|| data[i].description.Contains(search)
|| data[i].category.Contains(search))
{
Console.WriteLine("{0}: {1}", i + 1, data[i].name);
found = true;
}
}
if (!found)
Console.WriteLine("Not found!");
break;
case "4":
Console.Write("Enter the program number: ");
int progNumber = Convert.ToInt32(Console.ReadLine()) - 1;
if ((progNumber > counter) || (progNumber < 0))
{
Console.Write("Out of range!");
break;
}
Console.Write("Program name (was {0}; hit ENTER to leave as is): ",
data[progNumber].name);
string newText = Console.ReadLine();
if (newText != "")
data[progNumber].name = newText;
Console.Write("Program category (was {0}; hit ENTER to leave as is): ",
data[progNumber].category);
newText = Console.ReadLine();
if (newText != "")
data[progNumber].category = newText;
Console.Write("Program description (was {0}; hit ENTER to leave as is): ",
data[progNumber].description);
newText = Console.ReadLine();
if (newText != "")
data[progNumber].description = newText;
Console.Write("Program version (number) (was {0}; hit ENTER to leave as is): ",
data[progNumber].version.number);
newText = Console.ReadLine();
if (newText != "")
data[progNumber].version.number = newText;
Console.Write("Program version (month) (was {0}; hit ENTER to leave as is): ",
data[progNumber].version.month);
newText = Console.ReadLine();
if (newText != "")
data[progNumber].version.month = Convert.ToByte(newText);
Console.Write("Program version (year) (was {0}; hit ENTER to leave as is): ",
data[progNumber].version.year);
newText = Console.ReadLine();
if (newText != "")
data[progNumber].version.year = Convert.ToUInt16(newText);
break;
case "5": //delete
int position = 0;
Console.Write("Enter the position number to delete: ");
position = Convert.ToInt32(Console.ReadLine()) - 1;
if (position > counter)
Console.WriteLine("Error: out of range");
else
{
for (int i = position; i < counter; i++)
data[i] = data[i + 1];
counter--;
}
break;
case "6": // Sort
program aux;
for (int i = 0; i < counter - 1; i++)
{
for (int j = i + 1; j < counter; j++)
{
if (data[i].name.CompareTo(data[j].name) > 0)
{
aux = data[i];
data[i] = data[j];
data[j] = aux;
}
}
}
break;
case "7": //replace " " x " "
for (int i = 0; i < counter; i++)
{
while (data[i].name.Contains(" "))
data[i].name = data[i].name.Replace(" ", " ");
}
break;
case "0":
repeat = false;
break;
default:
Console.WriteLine("Wrong option!");
break;
}
}
while (repeat != false);
}
}