This should be written using JAVA and JSON. Please comment your code, thanks. Go
ID: 3833648 • Letter: T
Question
This should be written using JAVA and JSON. Please comment your code, thanks.
Goal:
To simulate a task management system with a priority queue.
Problem:
Provide a RESTful service which accepts as a POST of JSON a list of enqueue and dequeue statements onto an in-memory job queue.
Each job definition contains a name and a priority, with 0 being the best priority and positive integers representing lower priorities.
Return the JSON representing the state of the queue (the list of job names, in priority order), after all enqueue and dequeue statements have been processed.
Example input: { “inList” : [ { “cmd” : “enqueue”, “name” : ”job1”, “pri” : 4 },
{ “cmd” : “enqueue”, “name” : ”job2”, “pri” : 3 },
{ “cmd” : “dequeue” },
{ “cmd” : “enqueue”, “name” : ”job3”, “pri” : 0 },
{ “cmd” : “enqueue”, “name” : ”job4”, “pri” : 1 },
{ “cmd” : “dequeue” }
] }
Example output: { “outList” : [ “job4”, “job1” ] }
Erroneous input (e.g. malformed JSON) should be handled gracefully.
Explanation / Answer
Create A Operation Class
----------------------------------------------------------------------
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Operation {
private String cmd;
private String name;
private int pri;
public Operation(){
super();
}
public Operation(String cmd, String name, int pri) {
super();
this.cmd = cmd;
this.name = name;
this.pri = pri;
}
public String getCmd() {
return cmd;
}
public void setCmd(String cmd) {
this.cmd = cmd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPri() {
return pri;
}
public void setPri(int pri) {
this.pri = pri;
}
}
--------------------------------------------------------------------------------
create a TaskManagementSystem.java
----------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/TaskManagementSystem")
public class TaskManagementSystem {
@POST
@Path("/read")
// @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public List<String> readOperations(List<Operation> listOperation) {
// creating a priority queue which operates based on Priority
PriorityQueue<Operation> pq = new PriorityQueue<>(new Comparator<Operation>() {
@Override
public int compare(Operation o1, Operation o2) {
// TODO Auto-generated method stub
return o1.getPri() - o2.getPri();
}
});
for (Operation operation : listOperation) {
switch (operation.getCmd()) {
case "enqueue":
pq.add(operation);
break;
case "dequeue":
pq.poll();
break;
}
}
List<String> outList = new ArrayList<>();
while (!pq.isEmpty()) {
outList.add(pq.poll().getName());
}
return outList;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.