I have two constructors Employee Employee(string id, string FirstName, string LastName)
and ProjectProject(string PID, string PName)
. To add an employee or view the whole project list, I have created a business logic :
public class Employeeclass
{
public List<Employee> Employees { get; set; } = new List<Employee>();
public void AddEmployee(Employee employee)
{
Employees.Add(employee);
}
public List<Employee> GetAllEmployee()
{
return Employees;
}
}
public class Projectclass
{
public List<Project> Projects { get; set; } = new List<Project>();
public void AddProject(Project project)
{
Projects.Add(project);
}
public List<Project> GetAllProject()
{
return Projects;
}
}
In this business logic, I want to assign a project to an employee. For this I generated a separate constructor Assign(string EmpId, string PID)
and created a class to assign the projects. First I add a employee and then a project. By using EmpId and PID, I want to assign the projects to employee. It was possible to do so when all the business logic was under a single class.
public class Assignclass
{
public List<Assign> Assigns { get; set; } = new List<Assign>();
//assign employees to project
public void Add(Assign assign)
{
var id = assign.EmpId;
var pid = assign.PID;
var emp = Employees.Find(a => a.EmpId == id);
var prjct = Projects.Find(c => c.PID == pid);
if (emp != null || prjct != null)
{
Assigns.Add(assign);
}
}
}
I could use this code when I have put Employeeclass, Projectclass and Assignclass as a single class. Now that I have segregated the classes in Employeeclass, Projectclass, Assignclass; I can't use the same code as Employees
and Projects
can't exist without context so I created reference variables for Employees and Projects.
public void Add(Assign assign)
{
Employeeclass classA = new Employeeclass();
Projectclass classB = new Projectclass();
List<Employee> Employes = classA.Employees;
List<Project> Projcts = classB.Projects;
//List<Assign> Assignss = classC.Assigns;
var id = assign.EmpId;
var pid = assign.PID;
var emp = Employes.Find(a => a.EmpId == id);
var prjct = Projcts.Find(c => c.PID == pid);
if (emp != null || prjct != null)
{
Assigns.Add(assign);
}
}
But it shows while debugging that there are no elements in Employes. I can't use Find to find an employee with the same EmpId; same with Projects and Projcts. How should I assign the project to employees and what am I doing wrong? I am adding the complete code below:
//Class Library Constructors
using System;
namespace ClassLibrary1
{
public class Employee
{
public Employee(string id, string firstname, string lastname, string roleid)
{
EmpId = id;
FName = firstname;
LName = lastname;
RoleId = roleid;
}
public string FName { get; set; }
public string LName { get; set; }
public string EmpId { get; set; }
public string RoleId { get; set; }
}
public class Project
{
public Project(string id, string name)
{
PID = id;
PName = name;
}
public string PID { get; set; }
public string PName { get; set; }
}
public class Assign
{
public Assign(string eid, string pid)
{
EmpId = eid;
PID = pid;
}
public string EmpId { get; set; }
public string PID { get; set; }
}
}
//Business logic Domain.cs
using ClassLibrary1;
using System;
using System.Collections.Generic;
namespace ClassLibrary2
{
public class Employeeclass
{
public List<Employee> Employees { get; set; } = new List<Employee>();
public void AddEmployee(Employee employee)
{
Employees.Add(employee);
}
public List<Employee> GetAllEmployee()
{
return Employees;
}
}
public class Projectclass
{
public List<Project> Projects { get; set; } = new List<Project>();
public void AddProject(Project project)
{
Projects.Add(project);
}
public List<Project> GetAllProject()
{
return Projects;
}
}
public class Assignclass
{
public List<Assign> Assigns { get; set; } = new List<Assign>();
//assign employees to project
public void Add(Assign assign)
{
Employeeclass classA = new Employeeclass();
Projectclass classB = new Projectclass();
Assignlogic classC = new Assignlogic();
List<Employee> Employes = classA.Employees;
List<Project> Projcts = classB.Projects;
//List<Assign> Assignss = classC.Assigns;
var id = assign.EmpId;
var pid = assign.PID;
//copied code
var emp = Employes.Find(a => a.EmpId == id);
var prjct = Projcts.Find(c => c.PID == pid);
if (emp != null || prjct != null)
{
Assigns.Add(assign);
}
}
public List<Assign> GetAllAssignedProject()
{
return Assigns;
}
}
}
//Console Application
using ClassLibrary1;
using ClassLibrary2;
using System;
namespace EmployeeApp
{
static class Program
{
static void Main(string[] args)
{
Console.WriteLine("PPM Project");
Console.WriteLine("1. Add employee");
Console.Write("2. Add project");;
Console.WriteLine("3. Assign project to employee");
Console.WriteLine("4. View assign list");
var userInput = Console.ReadLine();
var business1 = new Employeeclass();
var business2 = new Projectclass();
var business3 = new Assignclass();
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 "4":
Console.WriteLine("adding prject id:");
var PID = Console.ReadLine();
Console.WriteLine("adding project name:");
var PName = Console.ReadLine();
var newProject = new Project(PID, PName);
business2.AddProject(newProject);
break;
case "6":
Console.WriteLine("Enter project id then enter, employee id");
var pid = Console.ReadLine();
var id = Console.ReadLine();
var newAssign = new Assign(id, pid);
business3.Add(newAssign);
break;
break;
case "7":
var allassign = business3.GetAllAssignedProject();
foreach (var project in allassign)
{
Console.WriteLine($"Project: {project.PID}\t assigned to Employee{project.EmpId}");
}
break;
default:
Console.WriteLine("Select");
break;
}
Console.WriteLine("Select option");
userInput = Console.ReadLine();
}
}
}
}
CodePudding user response:
This design looks like a database design. In C# it is easy to have relations, for example by a class simply having a property with type of another class.
I would just give a quick hint for a different design (instead of having an entity for the relation itself):
class Project
{
public List<Employee> Employees { get; set; } = new List<Employee>();
// (...) other properties
}
So when you want to assign an Employee
to a project, you can simply do
Employee e = GetEmployee(...);
project.Employees.Add(e);
Does that help?
CodePudding user response:
In your public void Add(Assign assign) function you make classA and classB, these are new objects of this class and have no values in them. If you want to use the function like this you could make the function like this: public void Add(Assign assing, Employeeclass classA, Projectclass classB) ... And then you can give your existing object with there value with them.