When creating a division, the method does not find a director, it creates one, but then tries to create a division again, and so on in a circle. How can I get out of this recursion?
Only these 3 methods are looped, and yes, I understand that they call each other. How can I avoid looping?
static public Employee GetOrCreateEmployee()
{
Console.WriteLine("Enter the full name of the teacher:");
string employeeName = Console.ReadLine();
Employee employee = DB.employees.FirstOrDefault(e => $"{e.Name} {e.Surname} {e.Patronymic}".ToLower() == employeeName.ToLower());
if (employee == null)
{
Console.WriteLine("Enter the employee's name:");
string name = Console.ReadLine();
Console.WriteLine("Enter the employee's last name:");
string surname = Console.ReadLine();
Console.WriteLine("Enter the employee's middle name:");
string patronymic = Console.ReadLine();
Position position = GetOrCreatePosition();
employee = new Employee(name, surname, patronymic, position);
DB.employees.Add(employee);
}
return employee;
}
static public Position GetOrCreatePosition()
{
Console.WriteLine("Enter the title of the position:");
string jobTitleName = Console.ReadLine();
Position position = DB.positions.FirstOrDefault(j => j.Title.ToLower() == jobTitleName.ToLower());
if (position == null)
{
position = DB.positions.FirstOrDefault(j => j.Title.ToLower() == jobTitleName.ToLower());
}
Console.WriteLine("Enter the title of the position:");
string name = Console.ReadLine();
Console.WriteLine("Enter the salary of the position:");
int.TryParse(Console.ReadLine(), out int salary);
Console.WriteLine("Enter the name of the department:");
string subdivisionName = Console.ReadLine();
Division division = DB.divisions.FirstOrDefault(s => s.Name.ToLower() == subdivisionName.ToLower());
if (division == null)
{
Console.WriteLine("The division was not found. Create it");
GetOrCreateDivision();
}
division = DB.divisions.FirstOrDefault(s => s.Name.ToLower() == subdivisionName.ToLower());
Position speciality = new Position(name, salary, division);
DB.positions.Add(speciality);
Console.WriteLine("The position has been successfully created.");
return position;
}
static public void GetOrCreateDivision()
{
Console.WriteLine("Enter the name of the department:");
string name = Console.ReadLine();
Console.WriteLine("Enter the last name, first name and patronymic of the director:");
string directorFullName = Console.ReadLine();
Employee director = DB.employees.FirstOrDefault(e => $"{e.Name} {e.Surname} {e.Patronymic}" == directorFullName);
if (director == null)
{
Console.WriteLine("The director has not been found. Create it.");
GetOrCreateEmployee();
director = DB.employees.Last();
}
Console.WriteLine("Enter the name of the organization:");
string organizationName = Console.ReadLine();
Organisation organization = DB.organisations.FirstOrDefault(o => o.Name.ToLower() == organizationName.ToLower());
if (organization == null)
{
Console.WriteLine("The organization was not found. Create it");
GetOrCreateOrganization();
}
organization = DB.organisations.FirstOrDefault(o => o.Name.ToLower() == organizationName.ToLower());
Division subdivision = new Division(name, director, organization);
DB.divisions.Add(subdivision);
Console.WriteLine("The division has been successfully created.");
}
I tried to do it through flags and method overloading, but nothing worked:(