Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 17, 2023 06:40
Show Gist options
  • Save Strelok78/1d01c5cdbf9a7717368e358f5537e7be to your computer and use it in GitHub Desktop.
Save Strelok78/1d01c5cdbf9a7717368e358f5537e7be to your computer and use it in GitHub Desktop.
Filling in arrays of personal files, formatted output, search by last name and deleting personal files.
internal class Program
{
static void Main(string[] args)
{
const string CommandAddPerson = "1";
const string CommandShowData = "2";
const string CommandRemovePerson = "3";
const string CommandSearchPerson = "4";
const string CommandExit = "5";
bool isOpen = true;
int textEnterLine = 20;
string[] personList = { "Alex Pirson Gate", "Morgan Freeman Ohoho", "Eva 01 Anime" };
string[] positionList = { "driver", "person", "action animation" };
string enterCommand;
string menuText = $"Enter comand number to use it:\n" +
$"{CommandAddPerson} - to Add Person\n" +
$"{CommandShowData} - to show full list of persons with their position info\n" +
$"{CommandRemovePerson} - to remove persons data from base by his number in list\n" +
$"{CommandSearchPerson} - to search person by his surname in our base\n" +
$"{CommandExit} - to leave the program\n";
while (isOpen)
{
Console.WriteLine(menuText);
Console.SetCursorPosition(0, textEnterLine);
enterCommand = Console.ReadLine();
switch (enterCommand)
{
case CommandAddPerson:
AddPerson(ref personList, ref positionList);
break;
case CommandShowData:
ShowPersonsList(personList, positionList);
break;
case CommandRemovePerson:
RemovePerson(ref personList, ref positionList);
break;
case CommandSearchPerson:
SearchPerson(personList, positionList);
break;
case CommandExit:
isOpen = false;
break;
default:
Console.WriteLine("\nError, incorrect value!");
break;
}
Console.WriteLine("\nClick any key");
Console.ReadKey();
Console.Clear();
}
}
static void SearchPerson(string[] persons, string[] position)
{
Console.Write("Enter surname to search: ... ");
char space = ' ';
string searchSurname = Console.ReadLine();
string[] surname;
bool searchSucceded = false;
for (int i = 0; i < persons.Length; i++)
{
surname = persons[i].Split(space);
if (surname[0].ToLower() == searchSurname.ToLower())
{
Console.Write(persons[i] + "\n" + position[i]);
searchSucceded = true;
}
}
if (!searchSucceded)
{
Console.WriteLine("There is no such person in our base");
}
}
static void RemovePerson(ref string[] persons, ref string[] positions)
{
Console.WriteLine("\nEnter persons number in list to remove the data");
int index = int.Parse(Console.ReadLine());
if (index > persons.Length || index < 0)
{
Console.WriteLine("Error wrong number!");
}
else
{
index--;
AssignArray(ref persons, false, index);
AssignArray(ref positions, false, index);
}
}
static void ShowPersonsList(string[] persons, string[] positions)
{
for (int i = 0; i < persons.Length; i++)
{
Console.Write($"{i + 1}. {persons[i]} - {positions[i]}; ");
}
}
static void AddPerson(ref string[] personsList, ref string[] positionsList)
{
Console.Write("\nEnter persons name: ... ");
string name = Console.ReadLine();
Console.Write("\nEnter persons position: ... ");
string position = Console.ReadLine();
AssignArray(ref personsList);
AssignArray(ref positionsList);
personsList[personsList.Length - 1] = name;
positionsList[positionsList.Length - 1] = position;
}
static void AssignArray(ref string[] array, bool adding = true, int index = 0)
{
int length;
if (!adding)
{
length = -1;
}
else
{
length = 1;
}
string[] newArray = new string[array.Length + length];
for (int i = 0; i < array.Length && i < newArray.Length; i++)
{
if (!adding && i == index)
{
newArray[i] = array[i+1];
index++;
}
else
{
newArray[i] = array[i];
}
}
array = newArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment