Home > Mobile >  According to the type T passed in the RequestQueue<T> generic class(department or employee),I&
According to the type T passed in the RequestQueue<T> generic class(department or employee),I&

Time:09-07

What I'm trying to achieve: According to the type of object passed in the RequestQueue generic class(department or employee), I am trying to hit the corresponding add function which adds the request depending upon which T type is passed to it on compile time and also hit the processDeptRequest or processEmpRequest to process that request - lets say, department type then it should hit the Add method and add the deoartment type request (tRef) to the listOfItems. this part is working fine.

Q. What do i pass in the Employee and Department in the Add function parameter? It has to be the class type. If I pass a string or a number, it won't let me do that and gives a compilation error as the actual parameter has to be Employee/Department type. Right now when I pass that empTicket/deptTicket variable, it goes as null as it doesn't have any value. So what value to give to it so something gets added to the respective listOfRequest?

Class diagram:

CLASS DIAGRAM

Class Employee and Class Department implementation

RaiseRequest()

This method present in the Employee class and the Department class should call the Add() 
of the respective queue of HelpDesk class based on the object that was created.

My code:

GENERIC CLASS REQUESTQUEUE

public class RequestQueue<T>
{
    private T[] listOfRequest;
    private static int pointer;


    public RequestQueue(int size)
    {
        this.listOfRequest = new T[size]; 
        pointer = 0;
    }


    public bool Add(T tRef)
    {
        if(pointer < this.listOfRequest.Length)
        {
            this.listOfRequest[pointer] = tRef;
            pointer  = 1;
            return true;
        }
        else { return false; }  
    }

    public bool ProcessItem()
    {
        if(pointer > 0)
        {
            var newArray = new int[this.listOfRequest.Length];
            this.listOfRequest[0] = default(T);                                
            Array.Copy(this.listOfRequest, 1, newArray, 0, 
            this.listOfRequest.Length - 1);
            return true;
        }
        else { return false; }
    }
}

CLASS HELPDESK:

public class Helpdesk
{
    private static RequestQueue<Department> depQueue;
    private static RequestQueue<Employee> empQueue;
    public static RequestQueue<Department> DepQueue 
    {
        get => depQueue;          
        set => depQueue = value;
    }
    public static RequestQueue<Employee> EmpQueue 
    { 
        get => empQueue;
        set => empQueue = value;
    }

    static Helpdesk()
    {
        DepQueue = new RequestQueue<Department>(100);
        EmpQueue = new RequestQueue<Employee>(10);

    }

    public static bool ProcessDepRequest()
    {
        return DepQueue.ProcessItem();
    }

    public static bool ProcessEmpRequest()
    {
        return EmpQueue.ProcessItem();
    }
}

CLASS DEPT:

public class Department
{      
    Department deptTicket;
    public bool RaiseRequest()
    {
        return Helpdesk.DepQueue.Add(deptTicket);
    }
}

CLASS EMPLOYEE:

public class Employee
{        
    Employee empTicket;
    
    public bool RaiseRequest()
    {
        return Helpdesk.EmpQueue.Add(empTicket);
    }
}

MAIN:

    static void Main(string[] args)
    {
        
        Department department = new Department();

        if (department.RaiseRequest())
        {
            Console.WriteLine("Department ticket created");
        }
        if (department.RaiseRequest())
        {
            Console.WriteLine("Department ticket created");
        }
        if (department.RaiseRequest())
        {
            Console.WriteLine("Department ticket created");
        }

        Helpdesk.ProcessDepRequest(); 
    }

HELP IS VERY MUCH APPRECIATED, THANKS!

CodePudding user response:

I think the implementation shown in the class diagram is incomplete and they want us to run our horses and add the properties for Employee and Department classes in the respective classes only on our own. That being said I have added a property called Request, initialized it by making a constructor in the class and then pass it as an actual parameter to the add method so it doesn't go as null. Below is the example shown only for Department class. We can perform the same for the Employee class as well.

public class Department
{
    public string Request { get; set; }
    public Department(string request)
    {
        this.Request = request;
    }


    public static bool RaiseRequest(Department depReq)
    {
        return Helpdesk.DepQueue.Add(depReq);
    }
}

MAIN:

static void Main(string[] args)
    {
        string Request1 = "Clicking error";
        Department department1 = new Department(Request1);
        if (Department.RaiseRequest(department1))
        {
            Console.WriteLine("1st Department request type ticket created");
        }

        string Request2 = ".jpeg error";
        Department department2 = new Department(Request2);                  
        if (Department.RaiseRequest(department2))
        {
            Console.WriteLine("2nd Department request type ticket created.");
        }

        string Request3 = "404 error";
        Department department3 = new Department(Request3);
        if (Department.RaiseRequest(department2))
        {
            Console.WriteLine("2nd Department request type ticket created.");
        }

        //fulfilling the first request
        if (Helpdesk.ProcessDepRequest())
        {
            Console.WriteLine("Department type Request 1 has been processed and fulfilled by Helpdesk!");
        }
        



    }

CodePudding user response:

So as a technically correct answer for a quick fix:

In each RaiseRequest() method, you can just pass this in to the Add-call like:

public class Department
{      
    public bool RaiseRequest()
    {
        return Helpdesk.DepQueue.Add(this);
    }
}

Although this would only put the department or employee itself into the queue, which might be or might not be enough for your concrete use case (which your question doesn't include, so I don't know). This would basically just say "This department/employee has a helpdesk request" and nothing more.

For the next bigger version that might help you more in the long run, look at Varun Gupta's answer.

For an even bigger answer, turn the answer of Varun Gupta around and do it the other way:

Make a class Request<T> which holds a reference to a T (maybe called requester or something), and also a string RequestText or something, and then have your Queue be depQueue = new RequestQueue<Request<Department> so you can put requests in there. Then you can have the RaiseRequest method create a new Request object each time, which includes the raising object (in that context, this), a request text, and possibly other things like a timestamp when it was created.

  • Related