mercredi 18 mars 2015

Expanding queue overwrites all previous entries with new entry



I have written a queuing program that allows the user to enter student objects into a queue. Here is my code:


Queue Class



public class Queue {

private StudentListing[] data;
private int size, numOfNodes, front, rear;

public Queue(int n) {
size = n;
numOfNodes = 0;
front = 0;
rear = 0;
data = new StudentListing[100];
}

private void resize(int max) {
assert max >= numOfNodes;
StudentListing[] temp = (StudentListing[]) new Object[max];
for (int i = 0; i < numOfNodes; i++) {
temp[i] = data[(front + i) % data.length];
}
data = temp;
front = 0;
rear = numOfNodes;
}

public void enqueue(StudentListing newNode) {
if (numOfNodes == data.length) {
resize(data.length * 2);
}
data[rear++] = newNode;
if (rear == data.length) {
rear = 0;
}
numOfNodes++;
}

public boolean isEmpty() {
if (numOfNodes == 0) {
return true;
} else {
return false;
}
}

public boolean isFull() {
if (size == numOfNodes) {
return true;
} else {
return false;
}
}

public StudentListing peek() {
int frontLocation;

if (numOfNodes == 0) {
return null;
} else {
frontLocation = front;
front = (front) % size;
return data[frontLocation];
}
}

public StudentListing dequeue() {
int frontLocation;

if (numOfNodes == 0) {
return null;
} else {
frontLocation = front;
front = (front + 1) % size;
numOfNodes = numOfNodes - 1;
return data[frontLocation];
}
}

public void reinitialize() {
front = 0;
rear = 0;
numOfNodes = 0;
}

public void showAll() {
int i = front;

for (int c = 1; c <= numOfNodes; c++) {
System.out.println(data[i].toString());
i = (i + 1) % size;
}
}


Main Class



public static void main(String[] args) {
JFrame frame = new JFrame();

int max = Integer.parseInt(JOptionPane.showInputDialog("Enter maximum "
+ "amount of students in the database."));

Queue database = new Queue(max);
int n = Integer.parseInt(JOptionPane.showInputDialog("Enter the amount "
+ "of students you want to add."));

StudentListing student = new StudentListing();

for (int i = 0; i < n; i++) {
student.input();
database.enqueue(student);
}

int prompt = Integer.parseInt(JOptionPane.showInputDialog("Please type "
+ "in the number next to the statement to perform the operation: \n\n"
+ "1. ENQUEUE STUDENT\n"
+ "2. DEQUEUE STUDENT\n"
+ "3. FETCH NEXT STUDENT IN QUEUE\n"
+ "4. OUTPUT ALL STUDENTS ON CONSOLE\n"
+ "5. FULL QUEUE CHECK\n"
+ "6. EMPTY QUEUE CHECK\n"
+ "7. CLEAR QUEUE\n"
+ "8. EXIT\n"));

while (true) {
switch (prompt) {
case 1:
student.input();
database.enqueue(student);
break;
case 2:
JOptionPane.showMessageDialog(frame,
database.dequeue().toString(), "Student Info",
JOptionPane.PLAIN_MESSAGE);
break;
case 3:
JOptionPane.showMessageDialog(frame,
database.peek().toString(), "Student Info",
JOptionPane.PLAIN_MESSAGE);
break;
case 4:
database.showAll();
break;
case 5:
if(database.isFull() == true) {
JOptionPane.showMessageDialog(frame,
"The queue is full.", "Full Queue Check",
JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(frame,
"The queue is not full.", "Full Queue Check",
JOptionPane.PLAIN_MESSAGE);
}
break;
case 6:
if(database.isEmpty() == true) {
JOptionPane.showMessageDialog(frame,
"The queue is empty.", "Empty Queue Check",
JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(frame,
"The queue is not empty.", "Empty Queue Check",
JOptionPane.PLAIN_MESSAGE);
}
break;
case 7:
database.reinitialize();
JOptionPane.showMessageDialog(frame,
"The queue has been cleared.", "Success!",
JOptionPane.PLAIN_MESSAGE);
break;
case 8:
System.exit(0);
break;
}

prompt = Integer.parseInt(JOptionPane.showInputDialog("Please type "
+ "in the number next to the statement to perform the operation: \n\n"
+ "1. ENQUEUE STUDENT\n"
+ "2. DEQUEUE STUDENT\n"
+ "3. FETCH NEXT STUDENT IN QUEUE\n"
+ "4. OUTPUT ALL STUDENTS ON CONSOLE\n"
+ "5. FULL QUEUE CHECK\n"
+ "6. EMPTY QUEUE CHECK\n"
+ "7. CLEAR QUEUE\n"
+ "8. EXIT\n"));
}
}


Almost all of the code works fine, but the problem I am having is that when I go to add a new student into the queue and it is full, it carries out the resize method and overwrites all of my previous entries with the new student entry.


For example: a student named John gets added to the queue with a maximum size of 1. The user goes to add another student named Tom to the queue; it expands the queue, but then overwrites John. The end result is that there are two Toms in the queue instead of one John and one Tom.


I need help figuring out how to stop it from overwriting and instead add all previous entries to the newly created expanded queue, and then add the new entry to the end of said queue. Thank you in advance :)




Aucun commentaire:

Enregistrer un commentaire