Topic 3.7 An animal shelter holds only dogs and cats, and operates on a strictly “first in, first out” basis. People must adopt either the “oldest”(based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structures to maintain this system and implement operations such as enqueue, dequeueAny, dequeueDog and dequeueCat. You may use the built-in LinkedList data structure.
//方法:建两个队列,需要的时候pop即可,需要最老的,比较两个队列的peek,挑出最老的。Use separate queues for dogs and cats, place them within a wrapper class. When dequeueAny, compare the peeks and return the oldest.
// 需要添加额外的特征,比如年龄,名字,性别,不要用数组,建一个类,声明属性,然后写一个方法返回该属性即可
public class Animal {
private int order; //只能被自己人修改,年龄
protected String name;
public Animal(String name){
this.name=name;
}
public String name(){
return name;
}
public void setOrder(int order) {
this.order = order;
}
public int getOrder() {
return order;
}
public boolean isOlderThan(Animal a) {//先进来的顺序小
return this.order < a.getOrder();
}
}
public class Cat extends Animal {
public Cat(String name){//这是一个构造方法
super(name);
}
}
public class Dog extends Animal{
public Dog(String type){//这是一个构造方法,仅此就够了
super(type);
}
}
import java.util.LinkedList;
public class AnimalQueue {
LinkedList<Dog> dogQueue = new LinkedList<Dog>();
LinkedList<Cat> catQueue = new LinkedList<Cat>();
private int order = 0;
//用两个LinkekdList来模拟队列,先进来的在头,进一个就放到next,出的时候出LinkedList的peek即可
public void enqueue(Animal a) {
a.setOrder(order);
order++;//序号是不能变的,进队列它的序号一定要加
if (a instanceof Dog) {
dogQueue.addLast((Dog) a);
} else if (a instanceof Cat) {
catQueue.addLast((Cat)a);
}
}
//返回所有动物里最老的
public Animal peek() {
if (dogQueue.size() == 0) {
return catQueue.peek();
} else if(catQueue.size() == 0) {
return dogQueue.peek();
}
Dog dog = dogQueue.peek();
Cat cat = catQueue.peek();
if (dog.isOlderThan(cat)) {
return dog;
} else {
return cat;
}
}
//LinkedList的poll方法弹出并返回LinkedList的头结点;
//peek()方法只返回头结点,不弹出;
//pop()方法是pop一个由此List代表的栈的顶端
//remove()方法移除并返回头结点,同poll
public Dog dequeueDogs() {
return dogQueue.poll();
}
public Cat dequeueCats() {
return catQueue.poll();
}
public Dog peekDogs() {
return dogQueue.peek();
}
public Cat peekCats() {
return catQueue.peek();
}
public Animal dequeueAny() {
if (dogQueue.size() == 0) {
return dequeueCats();
} else if (catQueue.size() == 0) {
return dequeueDogs();
}
Dog dog = dogQueue.peek();
Cat cat = catQueue.peek();
if (dog.isOlderThan(cat)) {
return dequeueDogs();
} else {
return dequeueCats();
}
}
public static void main(String[] args) {
AnimalQueue animals = new AnimalQueue();
animals.enqueue(new Cat("Callie"));
animals.enqueue(new Cat("Kiki"));
animals.enqueue(new Dog("Fido"));
animals.enqueue(new Dog("Dora"));
animals.enqueue(new Dog("Dapa"));
System.out.println(animals.dequeueCats().name());
System.out.println(animals.dequeueDogs().name());
System.out.println(animals.dequeueAny().name());
}
}
//结果
Callie
Fido
Kiki