Hello okay so my assignment was to create a priority queue for Airline Passengers. Here is what I have done so far:
//Driver
package priorityqueuestandby; import java.util.NoSuchElementException; import javax.swing.JOptionPane; public class PriorityQueueStandBy { public static void main(String[] args) { { String firstName; String lastName; String middleInitial; String longevity; int number = 1; StandByPassenger standbypassengerReference; boolean done = false; PryorityQueueDLList priorityQueue0 = new PryorityQueueDLList(); String[] choices = {"enqueue", "dequeue", "full?", "empty?", "quit"}; while (!done) { int choice = JOptionPane.showOptionDialog( null, "Click a choice", "Queue Operations Menu", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]); try { switch (choice) { case 0: firstName = JOptionPane.showInputDialog( "Enter your first name"); lastName = JOptionPane.showInputDialog( "Enter your last name"); middleInitial = JOptionPane.showInputDialog( "Enter your middle Initial"); longevity = JOptionPane.showInputDialog( "Enter the year, month and day you started" +"working for the airline ex 200515 = May 1, 2005"); int longevityInt = Integer.parseInt(longevity); standbypassengerReference = new StandByPassenger(firstName, lastName, middleInitial, longevityInt, number); priorityQueue0.enqueue(standbypassengerReference); number++; break; case 1: if (priorityQueue0.isEmpty()) { JOptionPane.showMessageDialog( null, "Cannot dequeue; queue is empty!"); } else { standbypassengerReference = (StandByPassenger) priorityQueue0.dequeue(); JOptionPane.showMessageDialog( null, "The next stand-by passenger in the queue was " + standbypassengerReference.getFirstName() + " " + standbypassengerReference.getMiddleInitial() + " " + standbypassengerReference.getLastName() + " " + "T" + standbypassengerReference.getNumber() + " " + "The passenger's longevity is: " + standbypassengerReference.getLongevity() + "."); } break; case 2: JOptionPane.showMessageDialog( null, "This queue will never be full!"); break; case 3: if (priorityQueue0.isEmpty()) { JOptionPane.showMessageDialog( null, "The queue is empty!"); } else { JOptionPane.showMessageDialog( null, "The queue is not empty!"); } break; case 4: done = true; break; default: JOptionPane.showMessageDialog( null, "Invalid selection; try again!"); } //end switch } // end try catch (NoSuchElementException e) { JOptionPane.showMessageDialog( null, "The queue is Empty", "", JOptionPane.ERROR_MESSAGE); } // end catch } // end while } } }
//DLList class
package priorityqueuestandby; public class PryorityQueueDLList<E>{ PryorityQueueNode<E> head; //Head node PryorityQueueNode<E> tail; //Tail node int size; public PryorityQueueDLList(){ head = new PryorityQueueNode<>(null, null, null); tail = new PryorityQueueNode<>(null, null, null); size = 0; //Set references head.setNext(tail); tail.setPrev(head); } //Return the next Node based on the refnode public PryorityQueueNode<E> getNext(PryorityQueueNode<E> referenceNode){ return referenceNode.getNext(); } //Return the previous Node based on the refnode public PryorityQueueNode<E> getPrev(PryorityQueueNode<E> referenceNode){ return referenceNode.getPrev(); } //Add an element before the refnode private void addBefore(PryorityQueueNode<E> referenceNode, E element){ PryorityQueueNode<E> prevNode = this.getPrev(referenceNode); //Set reference PryorityQueueNode<E> newNode = new PryorityQueueNode<>(element, prevNode, referenceNode); referenceNode.setPrev(newNode); prevNode.setNext(newNode); size++; } //Add an element after the refnode private void addAfter(PryorityQueueNode<E> referenceNode, E element){ PryorityQueueNode<E> nextNode = this.getNext(referenceNode); //Set reference PryorityQueueNode<E> newNode = new PryorityQueueNode<E>(element, referenceNode, nextNode); referenceNode.setNext(newNode); nextNode.setPrev(newNode); size++; } //Add an element at the front of the DLL public void addFirst(E element){ this.addAfter(head, element); } //Add an element at the rear of the DLL public void enqueue(E element){ this.addBefore(tail, element); } //Method to remove an element private E remove(PryorityQueueNode<E> removeNode){ PryorityQueueNode<E> prevNode = this.getPrev(removeNode); //Node1 PryorityQueueNode<E> nextNode = this.getNext(removeNode); //Node2 //Dereference the node which is removed removeNode.setPrev(null); removeNode.setNext(null); //Set references prevNode.setNext(nextNode); nextNode.setPrev(prevNode); size--; //return element of removeNode return removeNode.getElement(); } //Method to remove the last element of the DLL public E removeLast(){ PryorityQueueNode<E> tempNode = this.getPrev(tail); return this.remove(tempNode); } //Method to remove the first element of the DLL public E dequeue(){ PryorityQueueNode<E> tempNode = this.getNext(head); return this.remove(tempNode); } public boolean isEmpty(){ return (this.size == 0); } public boolean isFull(){ return false; } public int size(){ return this.size; } }
//DLList node class
package priorityqueuestandby; public class PryorityQueueNode<E> { PryorityQueueNode<E> prevNode; PryorityQueueNode<E> nextNode; E element; public PryorityQueueNode(E element, PryorityQueueNode<E> prevNode, PryorityQueueNode<E> nextNode) { this.prevNode = prevNode; this.nextNode = nextNode; this.element = element; } public E getElement() { return this.element; } public void setElement(E newElement) { this.element = newElement; } public PryorityQueueNode<E> getPrev() { return prevNode; } public void setPrev(PryorityQueueNode<E> newPrev) { this.prevNode = newPrev; } public PryorityQueueNode<E> getNext(){ return nextNode; } public void setNext(PryorityQueueNode<E> newNext){ this.nextNode = newNext; } }
//Passenger builder class
package priorityqueuestandby; public class StandByPassenger { private String firstName; private String lastName; private String middleInitial; private int longevity; int number; public StandByPassenger(String firstName, String lastName, String middleInitial, int longevity, int number){ this.firstName = firstName; this.lastName = lastName; this.middleInitial = middleInitial; this.longevity = longevity; this.number = number; } public void setFirstName(String firstName){ this.firstName = firstName; } public String getFirstName(){ return firstName; } public void setLastName(String lastName){ this.lastName = lastName; } public String getLastName(){ return lastName; } public void setMiddleInitial(String middleInitial){ this.middleInitial = middleInitial; } public String getMiddleInitial(){ return middleInitial; } public void setLongevity(int longevity){ this.longevity = longevity; } public int getLongevity(){ return longevity; } public int getNumber(){ return number; } }
So the part that I cant figure out is:
b. When a standby passenger is to be enqueued into the priority queue, it must be done so that at the moment of each dequeue operation, the item at the head of the queue is the standby passenger with the longest longevity, and also so that passengers with the same longevity are dequeued in a first-come-first-served fashion.
he says that we need to "Make your program so that it extends Comparable and implements the compareTo() method properly..."
So I was looking at the Comparable class and I could't find a compareTo() method... I am not confident I know how extends works either. Can someone please take a look at how my program is set up and give me an idea of where to start. I am assuming I need a new class if I am going to be extending another class. Right now I am taking in longevity as a String and converting it to an int because my last ditch effort is going to be to set up a loop that will organize longevity into a/an circular array based on the size of the incoming integer.