Last active
May 23, 2025 11:36
-
-
Save Strelok78/4e147afdc712b5f3302c5964e19b14da to your computer and use it in GitHub Desktop.
Workers records manager (using only arrays for the workers, positions and final record combinations)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace iJuniorPractice | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const int AddRecordCommand = 1; | |
const int ShowRecordsCommand = 2; | |
const int DeleteRecordCommand = 3; | |
const int SearchBySurnameCommand = 4; | |
const int ExitCommand = 5; | |
string[] commands = | |
{ | |
"Add Record", | |
"Show Records", | |
"Delete Record", | |
"Search By Surname", | |
"Exit" | |
}; | |
string[] workers = new string[0]; | |
string[] positions = new string[0]; | |
bool isWork = true; | |
while (isWork) | |
{ | |
Console.WriteLine(new string('-', 30)); | |
ShowMenu(commands); | |
Console.WriteLine("\nEnter command..."); | |
switch (ReadInt()) | |
{ | |
case AddRecordCommand: | |
AddRecord( ref workers, ref positions); | |
break; | |
case ShowRecordsCommand: | |
ShowRecords(workers, positions); | |
break; | |
case DeleteRecordCommand: | |
DeleteRecord(ref workers, ref positions); | |
break; | |
case SearchBySurnameCommand: | |
SearchBySurname(workers, positions); | |
break; | |
case ExitCommand: | |
isWork = false; | |
Console.WriteLine("Goodbye!"); | |
break; | |
default: | |
Console.WriteLine("Invalid command!"); | |
break; | |
} | |
} | |
} | |
static void ShowMenu(string[] commands) | |
{ | |
Console.WriteLine("Main menu:"); | |
for (int i = 0; i < commands.Length; i++) | |
{ | |
Console.WriteLine($"{i + 1}. {commands[i]}."); | |
} | |
} | |
static int ReadInt() | |
{ | |
int digit; | |
string inputDigit = ""; | |
string inputMessage = "Enter digit here: "; | |
inputDigit = GetStringWithMessage(inputMessage); | |
while (int.TryParse(inputDigit, out digit) == false) | |
{ | |
Console.WriteLine("Incorrect input, try again."); | |
inputDigit = GetStringWithMessage(inputMessage); | |
} | |
return digit; | |
} | |
static void AddRecord(ref string[] workers, ref string[] positions) | |
{ | |
AddFullName(ref workers); | |
AddNewPosition(ref positions); | |
} | |
static void DeleteRecord(ref string[] workers, ref string[] positions) | |
{ | |
if (workers.Length > 0) | |
{ | |
Console.WriteLine("Enter record ID to delete worker:"); | |
int recordId = ReadInt() - 1; | |
if (recordId < 0 || recordId >= workers.Length) | |
{ | |
Console.WriteLine("Invalid ID."); | |
} | |
else | |
{ | |
workers = RemoveArrayElementAt(workers, recordId); | |
positions = RemoveArrayElementAt(positions, recordId); | |
Console.WriteLine("Record deleted."); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("No records to remove."); | |
} | |
} | |
static void AddFullName(ref string[] workers) | |
{ | |
string newFullName, name, surname, patronymic; | |
string nameInputMessage = "Enter name: "; | |
string surnameInputMessage = "Enter surname: "; | |
string patronymicInputMessage = "Enter patronymic: "; | |
name = GetStringWithMessage(nameInputMessage); | |
surname = GetStringWithMessage(surnameInputMessage); | |
patronymic = GetStringWithMessage(patronymicInputMessage); | |
newFullName = String.Join(" ", surname, name, patronymic); | |
AddValueToArray(ref workers, newFullName); | |
} | |
static void AddNewPosition(ref string[] positions) | |
{ | |
string newPosiotion; | |
string message = "Enter position name: "; | |
newPosiotion = GetStringWithMessage(message); | |
AddValueToArray(ref positions, newPosiotion); | |
Console.WriteLine("New position added."); | |
} | |
static string GetStringWithMessage(string message = "") | |
{ | |
string inputString; | |
Console.WriteLine(new string('-', 30)); | |
Console.Write(message); | |
inputString = Console.ReadLine(); | |
return inputString; | |
} | |
static void AddValueToArray(ref string[] array, string newValue) | |
{ | |
string[] newArray = new string[array.Length + 1]; | |
for (int i = 0; i < array.Length; i++) | |
{ | |
newArray[i] = array[i]; | |
} | |
newArray[newArray.Length - 1] = newValue; | |
array = newArray; | |
} | |
static string[] RemoveArrayElementAt(string[] currentArray, int cutElementId) | |
{ | |
string[] newArray = new string[currentArray.Length - 1]; | |
for (int i = 0, cutedArrayIndex = 0; i < currentArray.Length; i++) | |
{ | |
if (i != cutElementId) | |
{ | |
newArray[cutedArrayIndex] = currentArray[i]; | |
cutedArrayIndex++; | |
} | |
} | |
return newArray; | |
} | |
static void ShowRecords(string[] workers, string[] positions) | |
{ | |
Console.WriteLine(new string('-', 30)); | |
Console.WriteLine("Result:"); | |
if (workers.Length > 0) | |
{ | |
for (int i = 0; i < workers.Length; i++) | |
{ | |
Console.WriteLine($"{i + 1}. {workers[i]} - {positions[i]}"); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("No records found."); | |
} | |
} | |
static void SearchBySurname(string[] workers, string[] positions) | |
{ | |
string surname; | |
string surnameInputMessage = "Enter surname: "; | |
string[] filteredWorkers = new string[0]; | |
string[] filteredPositions = new string[0]; | |
int surnameId = 0; | |
surname = GetStringWithMessage(surnameInputMessage); | |
for (int i = 0; i < workers.Length; i++) | |
{ | |
if (workers[i].Split()[surnameId].ToLower() == surname.ToLower()) | |
{ | |
AddValueToArray(ref filteredWorkers, workers[i]); | |
AddValueToArray(ref filteredPositions, positions[i]); | |
} | |
} | |
ShowRecords(filteredWorkers, filteredPositions); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment