Here I have implemented a queue. Is this a correct, efficient queue implementation? what issues are there if any and what can be done to make it better?
import java.util.*;
public class Queue2{
private int amountInQueue;
private int[] queue;
private int rearIndex;
private int frontIndex;
public Queue2(){
amountInQueue = 0;
queue = new int[10];
rearIndex = 0;
frontIndex = 0;
}
public Queue2(int queueCapacity){
amountInQueue = 0;
queue = new int[queueCapacity];
rearIndex = 0;
frontIndex = 0;
}
public void enqueue(int valueToAdd){
if(amountInQueue == queue.length){
throw new FullQueueException();
}
else if(rearIndex == queue.length -1){
rearIndex = 0;
queue[rearIndex] = valueToAdd;
amountInQueue++;
}
else if(amountInQueue == 0){
queue[rearIndex] = valueToAdd;
amountInQueue++;
}
else{
rearIndex++;
queue[rearIndex] = valueToAdd;
amountInQueue++;
}
}
public int dequeue(){
int frontValue = frontIndex;
if(amountInQueue == 0){
throw new EmptyQueueException();
}
if(frontIndex == queue.length -1){
frontValue = queue[frontIndex];
frontIndex = 0;
amountInQueue--;
}
else{
frontValue = queue[frontIndex];
frontIndex++;
amountInQueue--;
}
return frontValue;
}
public boolean isEmpty(){
return amountInQueue == 0;
}
public int valueAtRear(){
return queue[rearIndex];
}
public int valueAtFront(){
return queue[frontIndex];
}
public int currentSize(){
return amountInQueue;
}
class FullQueueException extends RuntimeException{
public FullQueueException(){
super("You cannot add values to a queue that is full");
}
}
class EmptyQueueException extends RuntimeException{
public EmptyQueueException(){
super("You cannot remove values from a queue that is empty");
}
}
}