C# Employee ArrayList Menu Driven Application
C# Employee ArrayList Menu Driven Application
Here is a sample C# program that implements a menu-driven application that prompts the user to enter details for five employees and saves them in an ArrayList. The program includes explanations of the source code using comments:
using System;
using System.Collections.Generic;
namespace EmployeeApplication
{
class Program
{
static void Main(string[] args)
{
// Create a list to store the employee details
List<Employee> employees = new List<Employee>();
// Initialize a string variable to store the user input
string input = "";
// Keep looping until the user enters "quit"
while (!input.Equals("quit", StringComparison.OrdinalIgnoreCase))
{
// Prompt the user to enter the name of the employee
Console.WriteLine("Enter the name of the employee:");
string name = Console.ReadLine();
// If the user enters "quit", break out of the loop
if (name.Equals("quit", StringComparison.OrdinalIgnoreCase))
{
break;
}
// Prompt the user to enter the salary of the employee
Console.WriteLine("Enter the salary of the employee:");
double salary = double.Parse(Console.ReadLine());
// Prompt the user to enter the department of the employee
Console.WriteLine("Enter the department of the employee:");
string department = Console.ReadLine();
// Create a new Employee object using the input provided by the user
// and add it to the list
employees.Add(new Employee(name, salary, department));
// If the list has reached a size of 5, break out of the loop
if (employees.Count == 5)
{
break;
}
}
// Print the details of all the employees in the list
Console.WriteLine("Employee Details:");
foreach (Employee employee in employees)
{
Console.WriteLine(employee);
}
}
}
// This class represents an employee with a name, salary, and department
class Employee
{
public string Name { get; set; }
public double Salary { get; set; }
public string Department { get; set; }
// Constructor to initialize the employee object with the given name, salary, and department
public Employee(string name, double salary, string department)
{
Name = name;
Salary = salary;
Department = department;
}
// Overridden ToString method to return a string representation of the employee object
public override string ToString()
{
return $"Employee{{ name='{Name}', salary={Salary}, department='{Department}' }}";
}
}
}
VISUAL STUDIO OUTPUT
Enter the name of the employee:
Jane
Enter the salary of the employee:
200000000
Enter the department of the employee:
Enter the name of the employee:
John
Enter the salary of the employee:
3000
Enter the department of the employee:
IT
Enter the name of the employee:
quit
Employee Details:
Employee{ name='Jane', salary=200000000, department='' }
Employee{ name='John', salary=3000, department='IT' }
D:\MauriceMuteti\C SHARP\OTHER VIDEOS\C# Employee ArrayList Menu Driven Application\MenuDrivenEmployeeApp\MenuDrivenEmployeeApp\bin\Debug\net5.0\MenuDrivenEmployeeApp.exe (process 7676) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
This program uses a while
loop to keep looping until the user enters “quit”. It also uses a List
to store the employee details entered by the user.
Inside the loop, the program prompts the user to enter the name, salary, and department of the employee. It then creates a new Employee
object using the input provided by the user and adds it to the List
.
Here is the rest of the explanation for the sample C# program that I provided above:
The Employee
class is a simple class that represents an employee with a name, salary, and department. It has three public properties Name
, Salary
, and Department
to store the respective values for an employee. It also has a constructor that initializes an Employee
object with the given name, salary, and department. Finally, it has an overridden ToString
method that returns a string representation of the Employee
object in the format “Employee{ name=’name’, salary=salary, department=’department’ }”.
In the Main
method of the Program
class, a List
of Employee
objects is created to store the employee details. A while
loop is used to keep looping until the user enters “quit”. Inside the loop, the program prompts the user to enter the name, salary, and department of the employee. It then creates a new Employee
object using the input provided by the user and adds it to the List
. If the List
reaches a size of 5, the loop is broken.
Finally, the program prints out the details of all the employees in the List
by iterating through the list and calling the ToString
method on each Employee
object.
You can Clone The Code From This Github Repository Link – https://github.com/mauricemuteti/MenuDrivenEmployeeApp