Untitled

 avatar
unknown
plain_text
a year ago
3.3 kB
2
Indexable
using System;
using System.Collections.Generic;
using System.Linq;

struct Employee
{
    private string lastName;
    private string firstName;
    private string middleName;
    private string position;
    private int hireYear;

    public Employee(string lastName, string firstName, string middleName, string position, int hireYear)
    {
        this.lastName = lastName;
        this.firstName = firstName;
        this.middleName = middleName;
        this.position = position;
        this.hireYear = hireYear;
    }

    public string LastName { get { return lastName; } }
    public string FirstName { get { return firstName; } }
    public string MiddleName { get { return middleName; } }
    public string Position { get { return position; } }

    public string Info
    {
        get
        {
            return $"Employee: {lastName} {firstName} {middleName}, Position: {position}, Year of Hiring: {hireYear}";
        }
    }

    public int GetYearsOfWork(int currentYear)
    {
        return currentYear - hireYear;
    }
}

class Program
{
    static void Main()
    {
        List<Employee> employees = new List<Employee>();

        employees.Add(new Employee("Smith", "John", "Doe", "Manager", 2015));
        employees.Add(new Employee("Johnson", "Alice", "Mary", "Developer", 2017));
        employees.Add(new Employee("Williams", "David", "Robert", "HR Specialist", 2018));
        employees.Add(new Employee("Brown", "Sarah", "Jane", "Designer", 2016));
        employees.Add(new Employee("Jones", "Michael", "Patrick", "Analyst", 2019));

        Console.WriteLine("Enter new employee details:");
        Console.Write("Last Name: ");
        string newLastName = Console.ReadLine();
        Console.Write("First Name: ");
        string newFirstName = Console.ReadLine();
        Console.Write("Middle Name: ");
        string newMiddleName = Console.ReadLine();
        Console.Write("Position: ");
        string newPosition = Console.ReadLine();
        Console.Write("Year of Hiring: ");
        int newHireYear = Convert.ToInt32(Console.ReadLine());
        employees.Add(new Employee(newLastName, newFirstName, newMiddleName, newPosition, newHireYear));

        employees = employees.OrderBy(e => e.LastName).ToList();

        Console.WriteLine("Enter the details of the employee to be removed:");
        Console.WriteLine("Last Name: ");
        string targetLastName = Console.ReadLine();
        Console.WriteLine("First Name: ");
        string targetFirstName = Console.ReadLine();
        Console.WriteLine("Middle Name: ");
        string targetMiddleName = Console.ReadLine();

        Employee employeeToRemove = employees.FirstOrDefault(e => e.LastName == targetLastName && e.FirstName == targetFirstName && e.MiddleName == targetMiddleName);

        if (employeeToRemove.LastName != null)
        {
            employees.Remove(employeeToRemove);
            Console.WriteLine("Employee removed successfully.");
        }
        else
        {
            Console.WriteLine("Employee not found in the list.");
        }

        Console.WriteLine("\nList of Employees:");
        foreach (Employee employee in employees)
        {
            Console.WriteLine(employee.Info);
        }
    }
}
Leave a Comment