import java.util.Scanner;

public class ArrayLoopQueueDemo {
    public static void main(String[] args) {

        // 创建队列
        ArrayLoopQueue queue = new ArrayLoopQueue(3); // 实际容量为2,有1个预留空间
        char key = ' '; // 接收用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true; // 保证程序一直执行
        while (loop) { // 输出菜单
            System.out.println("s(show):--显示队列-");
            System.out.println("e(exit):--退出程序-");
            System.out.println("g(get):----出队----");
            System.out.println("a(add):----入队----");
            System.out.println("r(remove):---出队--");
            System.out.println("h(head):显示队列头部");
            key = scanner.next().charAt(0); // 获取操作字符
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数:");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':

                    try {
                        int res = queue.getQueue();
                        System.out.println("取出的数据是" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'r':

                    try {
                        queue.removeQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':

                    try {
                        int res = queue.headQueue();
                        System.out.println("取出的数据是" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
    }
}

/**
 * 使用数组模拟循环队列
 */
class ArrayLoopQueue {
    private int maxSize; // 数组的最大容量
    private int front; // 队列头:front指向队列的第一个元素
    private int rear; // 队列尾:rear指向队列的最后一个元素的后一个位置(约定预留一个空间)
    private int[] array; // 该数组用于存放数据,模拟队列

    /**
     * 创建队列的构造器
     *
     * @param arrMaxSize 数组容量
     */
    public ArrayLoopQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        array = new int[maxSize];
//        front = 0;
//        rear = 0;
    }

    /**
     * 判断队列是否满
     *
     * @return
     */
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    /**
     * 判断队列是否满
     *
     * @return
     */
    public boolean isEmpty() {
        return front == rear;
    }

    /**
     * 入队
     *
     * @param n
     */
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已满,无法加入~");
            return;
        }
        array[rear] = n; // 加入数据(直接加到后一个位置)
        rear = (rear + 1) % maxSize; // 将rear后移,考虑取模
    }

    /**
     * 获取队列头(出队)
     *
     * @return
     */
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法获取~");
        }
        int temp = array[front];
        front = (front + 1) % maxSize;
        return temp;
    }

    /**
     * 获取队列头(出队)
     *
     * @return
     */
    public void removeQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法获取~");
        }
        front = (front + 1) % maxSize; // 头部指针后移
    }

    /**
     * 显示队列
     */
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("对列为空,没有数据~");
            return;
        }
        // 思路:从front开始遍历,遍历多少个元素
        for (int i = front; i < (front + size()); i++) {
            System.out.printf("array[%d]=%d\n", i % maxSize, array[i % maxSize]);
        }
    }

    /**
     * 获取队列头
     *
     * @return
     */
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法获取~");
        }
        return array[front];
    }

    /**
     * 求出当前队列有效数据的个数
     */
    public int size() {
//        System.err.println((rear + maxSize - front) % maxSize);
        return (rear + maxSize - front) % maxSize; // 末减初加一(rear实际就是末尾+1)
    }
}