Home > Software engineering >  How to add element in main list instead of the instance created?
How to add element in main list instead of the instance created?

Time:02-18

I created a constructor Employee(string id, string firstname, string lastname, string roleid) and added a class library as Business logic and a console app to run the program. What I am trying to do is add and view elements in the list Employees.

//Logic
public class Domain
    {
        public List<Employee> Employees { get; set; } = new List<Employee>();

        public void AddEmployee(Employee employee)
        {
            Employees.Add(employee);
        }

        public List<Employee> GetAllEmployee()
        {
            return Employees;
        }

    }

In console app, I created a program for the same, but instead of adding in the list, the new elements get added to the instance I created.

//Console App
static class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("PPM Project");
            Console.WriteLine("1 Add employee");
            Console.WriteLine("2 View Employee");

            var userInput = Console.ReadLine();
            var business1 = new Domain();
            var allemployee = business1.GetAllEmployee();

            while (true)
            {


                switch (userInput)
                {
                    case "1":
                                       
                            Console.WriteLine("adding employee first name:");
                            var FName = Console.ReadLine();

                            Console.WriteLine("adding employee last name:");
                            var LName = Console.ReadLine();

                            Console.WriteLine("adding 3digit Role Id:");
                            var RoleId = Console.ReadLine();

                            Console.WriteLine("adding 4digit Employee Id:");
                            var EmpId = Console.ReadLine();

                            var newEmployee = new Employee(EmpId, FName, LName, RoleId);
                            business1.AddEmployee(newEmployee);
                            break;

                    case "2":

                        foreach (var employee in allemployee)
                        {
                            Console.WriteLine($"Employee: {employee.EmpId}\t {employee.FName} {employee.LName}, is assigned a RoleID : {employee.RoleId}");
                        }
                        break;

                    default:
                        Console.WriteLine("Select");
                        break;

                }

                Console.WriteLine("Select option");
                userInput = Console.ReadLine();

            }
            }
        }

The code runs correctly and I can add and view both employees and projects but neither the project or employee is added to the list Projects and Employees I declared in business logic, but rather in the new instance I created to use. How to add elements in the list and which part of code should I modify?

CodePudding user response:

You are adding new Employee class instance to the Domain.Employees appropriately, Actual problem is in your second switch case.

In case 2 case, you are iterating allemployee which is fetched at the beginning, Instead of that fetch all the employees every time whenever user enters 2 as an option.

In this way you will store latest employee record in allemployee variable in each call.

public static class Program
{
    public static void Main(string[] args)
    {
        
        //Your existing code goes here
        //var allemployee = business1.GetAllEmployee();   //Don't call it here
        while (true)
        {
            switch (userInput)
            {
                case "1":                    
                  //Your existing code goes here
                break;
                case "2":
                    var allemployee = business1.GetAllEmployee();  //Call GetAllEmployee inside case 2 not on top
                    foreach (var employee in allemployee)
                    {
                        Console.WriteLine($"Employee: {employee.EmpId}\t {employee.FName} {employee.LName}, is assigned a RoleID : {employee.RoleId}");
                    }
                    break;
               //Your existing code goes here
            }
        }
    }
}
  • Related