package queue;

import java.util.LinkedList;

public class AnimalQueue {
    LinkedList<Dog> dogs = new LinkedList<>();
    LinkedList<Cat> cats = new LinkedList<>();
    private int order = 0;

    public void enqueue(Animal a) {
        a.order = order;
        order++;

        if (a instanceof Dog) {
            dogs.addLast((Dog)a);
        } else if (a instanceof Cat){
            cats.addLast((Cat)a);
        }
    }

    public Animal dequeueAny(){
        if(dogs.size() == 0){
            return dequeueCats();
        } else if(cats.size() == 0){
            return dequeueDogs();
        }

        Dog dog = dogs.peek();
        Cat cat = cats.peek();
        if(dog.compareTo(cat) == 1){ //order越大, 越新来
            return dequeueCats();
        } else {
            return dequeueDogs();
        }
    }

    private Animal dequeueDogs() {
        return dogs.poll();
    }

    private Animal dequeueCats() {
        return cats.poll();
    }
}