0% found this document useful (0 votes)
16 views

Assignment 1

The document outlines a C# program that defines an Employee class with private properties for Id, Name, and DepartmentName, along with methods to retrieve these properties and an event that triggers when any method is called. It also includes a Program class that prompts the user for employee details, creates an Employee object, and allows for updating the employee's properties through overloaded methods. The program displays the employee's details before and after updates, demonstrating the functionality of the Employee class and its methods.

Uploaded by

meetsavaliya0503
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Assignment 1

The document outlines a C# program that defines an Employee class with private properties for Id, Name, and DepartmentName, along with methods to retrieve these properties and an event that triggers when any method is called. It also includes a Program class that prompts the user for employee details, creates an Employee object, and allows for updating the employee's properties through overloaded methods. The program displays the employee's details before and after updates, demonstrating the functionality of the Employee class and its methods.

Uploaded by

meetsavaliya0503
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a C# program that satisfies the below requirements

1. Create a class called Employee

 It has three private properties called Id, Name, DepartmentName


 Its constructor should accept all of these three properties while creating
an object.
 It should have three methods that individually returns the above properties
(i.e. GetId(), GetName(), GetDepartmentName())

2. Create a Program that accepts details from the user

 It should ask the user for Id, Name, DepartmentName


 After getting details from the user create an object of the Employee class
with provided details.
 After creating an object print all three properties one by one using the
above-created methods.

3. Create an event in Employee Class which is fired when any of the


class’ methods are being called and outputs a message (i.e.
“GetName() method called”)]
4. Create 3 overloaded methods that allow updating any of the Employee
property (Id, Name, DepartmentName)
Program.cs
using System;

class Program
{
static void Main()
{
// Taking user input for Employee details
Console.Write("Enter Employee Id: ");
int id = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter Employee Name: ");


string name = Console.ReadLine();

Console.Write("Enter Department Name: ");


string department = Console.ReadLine();

// Creating Employee object


Employee employee = new Employee(id, name, department);

// Subscribing to event to display method calls


employee.OnMethodCalled += (message) => Console.WriteLine(message);

// Printing Employee details using getter methods


Console.WriteLine("\nEmployee Details:");
Console.WriteLine("Id: " + employee.GetId());
Console.WriteLine("Name: " + employee.GetName());
Console.WriteLine("Department: " + employee.GetDepartmentName());

// Updating Employee details using correct overloaded methods


Console.WriteLine("\nUpdating Employee Details:");

Console.Write("Enter new Id: ");


employee.UpdateEmployee(Convert.ToInt32(Console.ReadLine())); // Calls int
overload

Console.Write("Enter new Name: ");


employee.UpdateEmployee(Console.ReadLine()); // Calls string overload

Console.Write("Enter new Department Name: ");


employee.UpdateEmployee(Console.ReadLine(), 0); // Calls string + int
overload

// Display updated details


employee.DisplayEmployeeDetails();
}
}
Employee.cs
using System;

public class Employee


{
// Private properties
private int Id;
private string Name;
private string DepartmentName;

// Event declaration for method call notifications


public delegate void MethodCalledHandler(string methodName);
public event MethodCalledHandler OnMethodCalled;

// Constructor to initialize Employee object


public Employee(int id, string name, string departmentName)
{
Id = id;
Name = name;
DepartmentName = departmentName;
}

// Getter methods with event triggers


public int GetId()
{
OnMethodCalled?.Invoke("GetId() method called");
return Id;
}

public string GetName()


{
OnMethodCalled?.Invoke("GetName() method called");
return Name;
}

public string GetDepartmentName()


{
OnMethodCalled?.Invoke("GetDepartmentName() method called");
return DepartmentName;
}

// Method Overloading to Update Employee Properties

// Overloaded method to update Id


public void UpdateEmployee(int id)
{
Id = id;
Console.WriteLine("Id updated successfully.");
}
// Overloaded method to update Name
public void UpdateEmployee(string name)
{
Name = name;
Console.WriteLine("Name updated successfully.");
}

// Overloaded method to update DepartmentName


public void UpdateEmployee(string departmentName, int dummy) // Dummy
parameter to differentiate
{
DepartmentName = departmentName;
Console.WriteLine("Department Name updated successfully.");
}

// Method to display updated details


public void DisplayEmployeeDetails()
{
Console.WriteLine("\nUpdated Employee Details:");
Console.WriteLine($"Id: {GetId()}");
Console.WriteLine($"Name: {GetName()}");
Console.WriteLine($"Department: {GetDepartmentName()}");
}
}

You might also like