-
Notifications
You must be signed in to change notification settings - Fork 18
Prioritized Queue Exercise
The goal of this exercise is to demonstrate a useful scenario in which C# generics can be applied. The domain is ticket sales, such as for a concert or other event. Ticket sales and payment for tickets are done by separate processes using priority queues. A priority queue supports an Enqueue or Add method that takes in a specific type as well as a separate value indicating its priority. These two things are combined into a single type with a name like QueueItem (but you will need separate specific types for each kind of priority queue). The priority queue also supports a Dequeue method that will return the next item from the queue in priority order, removing it from the queue in the process.
Customers purchase tickets. During busy periods, the rate of customers submitting purchase requests often exceeds the processing speed. In this case, priority is given to longtime customers. Implement a priority queue that takes in a ticket purchase request DTO (its contents do not matter) and is prioritized based on a customer creation date. Write unit tests to verify that the correct item is dequeued from the priority queue. Your queue item class might look something like this:
public class TicketPurchaseQueueItem
{
public DateTime Priority { get; set; }
public PurchaseRequestDTO Request { get; set; }
}Payment processing is done by another separate process. Payments are processed in priority order. Priority is assigned as an integer value, and the smallest numbers should be processed first. Create a new priority queue implementation that works with a process payment command DTO (details do not matter) and is prioritized by an integer. Write unit tests to verify the correct item is dequeued from the priority queue. Your queue item class might look like this:
public class ProcessPaymentQueueItem
{
public int Priority { get; set; }
public ProcessPaymentDTO Request { get; set; }
}Create a generic prioritized queue that works with any type of priority and can handle any kind of request. It can work with a generic QueueItem class. Once you have it written and tested with unit tests, refactor your original two collections you created in steps 1 and 2 so that they inherit from your new generic queue and use the generic queueitem class. Verify the unit tests you wrote for these 2 collections work as they did before.