Programming Course in C# ¡Free!

Computer programs

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

Create a C# program that can store up to 1000 records of computer programs. For each program, you must keep the following data:


* Name
* Category
* Description
* Version (is a set of 3 data: version number -text-, launch month -byte- and launch year -unsigned short-)

The program should allow the user the following operations:

1 - Add data of a new program (the name must not be empty, category must not be more than 30 letters (or should be re-entered), and for the description, it will accept only the first 100 letters entered and skip the rest; the version needs no validation).

2 - Show the names of all the stored programs. Each name must appear on one line. If there are more than 20 programs, you must pause after displaying each block of 20 programs, and wait for the user to press Enter before proceeding. The user should be notified if there is no data.

3 - View all data for a certain program (from part of its name, category or description, case sensitive). If there are several programs that contain that text, the program will display all of them, separated by a blank line. The user should be notified if there are no matches found.

4 - Update a record (asking the user for the number, the program will display the previous value of each field, and the user can press Enter not to modify any of the data). He should be warned (but not asked again) if he enters an incorrect record number. It is not necessary to validate any of the fields.

5 - Delete a record, whose number will be indicated by the user. He should be warned (but not asked again) if he enters an incorrect number.

6 - Sort the data alphabetically by name.

7 - Fix redundant spaces (turn all the sequences of two or more spaces into a single space, only in the name, for all existing records).

X - Exit the application (as we do not store the information, data will be lost).


Output



Solution


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()
{
program[] data = new program[1000];
bool repeat = true;
string option;
int i = 0;

do
{
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("Enter a option: ");
option = Console.ReadLine();

switch (option)
{
case "1":
if (i > capacity - 1)
{
Console.WriteLine("Database full!");
break;
}

do
{
Console.Write("Enter Name: ");
data[i].name = Console.ReadLine();

if (data[i].name.Length == 0)
Console.Write("Cannot be empty");
} 
while (data[i].name.Length == 0);

do
{
Console.Write("Enter category: ");
data[i].category = Console.ReadLine();

if (data[i].category.Length > 30)
Console.Write("Too long. 30 letters max");
} 
while (data[i].category.Length > 30);

Console.Write("Enter description: ");
data[i].description = Console.ReadLine();

if (data[i].description.Length > 100)
data[i].description = data[i].description.Substring(0, 100);

Console.Write("Enter the version number: ");
data[i].version.number = Console.ReadLine();

Console.Write("Enter the version month: ");
data[i].version.month = Convert.ToByte(Console.ReadLine());

Console.Write("Enter the version year: ");
data[i].version.year = Convert.ToUInt16(Console.ReadLine());

i++;
break;

case "2":
if (i == 0)
Console.WriteLine("No data!");
else
for (int i = 0; i < i; i++)
{
Console.WriteLine("{0}: {1}", i+1, data[i].name);
if (i % 20 == 19)
{
Console.Write("Press Enter...");
Console.ReadLine();
}
}
break;

case "3":
Console.Write("Enter part of the name, description, etc... (case sensitive): ");
string search = Console.ReadLine();
bool found = false;

for (int i = 0; i < i; 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 > i) || (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": 
int position = 0;
Console.Write("Enter the position number to delete: ");
position = Convert.ToInt32(Console.ReadLine())-1;

if (position > i)
Console.WriteLine("Error: out of range");
else
{
for (int i = position; i < i; i++)
data[i] = data[i + 1];
i--;
}
break;

case "6":
program aux;

for (int i = 0; i < i - 1; i++)
{
for (int j = i + 1; j < i; j++)
{
if (data[i].name.CompareTo(data[j].name) > 0)
{
aux = data[i];
data[i] = data[j];
data[j] = aux;
}
}
}
break;

case "7":
for (int i = 0; i < i; 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);
}
}