Untitled

Anonymous
Plain Text
09/02/2025 6:03 AM
3.7 KB
17
Indexable
Never
using System;
using System.Collections.Generic;
using System.Linq;

class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
}

class Program
{
    static List<Employee> employees = new List<Employee>();
    static int nextId = 1;

    static void Main()
    {
        while (true)
        {
            Console.WriteLine("\nEmployee Management System");
            Console.WriteLine("1. Create Employee");
            Console.WriteLine("2. Read All Employees");
            Console.WriteLine("3. Update Employee");
            Console.WriteLine("4. Delete Employee");
            Console.WriteLine("5. Exit");
            Console.Write("Choose an option: ");
            string option = Console.ReadLine();

            switch (option)
            {
                case "1":
                    CreateEmployee();
                    break;
                case "2":
                    ReadEmployees();
                    break;
                case "3":
                    UpdateEmployee();
                    break;
                case "4":
                    DeleteEmployee();
                    break;
                case "5":
                    return;
                default:
                    Console.WriteLine("Invalid option, try again.");
                    break;
            }
        }
    }

    static void CreateEmployee()
    {
        Console.Write("Enter employee name: ");
        string name = Console.ReadLine();
        Console.Write("Enter position: ");
        string position = Console.ReadLine();

        Employee emp = new Employee
        {
            Id = nextId++,
            Name = name,
            Position = position
        };
        employees.Add(emp);
        Console.WriteLine("Employee created with ID: " + emp.Id);
    }

    static void ReadEmployees()
    {
        if (employees.Count == 0)
        {
            Console.WriteLine("No employees to show.");
            return;
        }

        foreach (var emp in employees)
        {
            Console.WriteLine($"ID: {emp.Id}, Name: {emp.Name}, Position: {emp.Position}");
        }
    }

    static void UpdateEmployee()
    {
        Console.Write("Enter employee ID to update: ");
        if (int.TryParse(Console.ReadLine(), out int id))
        {
            var emp = employees.FirstOrDefault(e => e.Id == id);
            if (emp == null)
            {
                Console.WriteLine("Employee not found.");
                return;
            }

            Console.Write("Enter new name (leave blank to keep current): ");
            string newName = Console.ReadLine();
            if (!string.IsNullOrWhiteSpace(newName))
                emp.Name = newName;

            Console.Write("Enter new position (leave blank to keep current): ");
            string newPosition = Console.ReadLine();
            if (!string.IsNullOrWhiteSpace(newPosition))
                emp.Position = newPosition;

            Console.WriteLine("Employee updated.");
        }
        else
        {
            Console.WriteLine("Invalid ID.");
        }
    }

    static void DeleteEmployee()
    {
        Console.Write("Enter employee ID to delete: ");
        if (int.TryParse(Console.ReadLine(), out int id))
        {
            var emp = employees.FirstOrDefault(e => e.Id == id);
            if (emp == null)
            {
                Console.WriteLine("Employee not found.");
                return;
            }

            employees.Remove(emp);
            Console.WriteLine("Employee deleted.");
        }
        else
        {
            Console.WriteLine("Invalid ID.");
        }
    }
}
Editor is loading...
Leave a Comment