实现一种狗猫队列的结构,要求如下: 用户可以调用add方法将cat类或dog类的实例放入队列中; 用户可以调用pollAll方法,将队列中所有的实例按照进队列的先后顺序依次弹出;用户可以调用pollDog方法,将队列中dog类的实例按照进队列的先后顺序依次弹出; 用户可以调用pollCat方法,将队列中cat类的实 例按照进队列的先后顺序依次弹出; 用户可以调用isEmpty方法,检查队列中是否还有dog或cat的实例; 用户可以调用isDogEmpty方法,检查队列中是否有dog 类的实例;用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例。
import java.util.LinkedList;
import java.util.Queue;
public class e06DogCatQueue {
public static class Pet{
private String type;
private String name;
public Pet(String type,String name){
this.type=type;
this.name=name;
}
public String getPetType() {
return type;
}
@Override
public String toString() {
return "Pet{" +
"type='" + type + '\'' +
", name='" + name + '\'' +
'}';
}
}
public static class Dog extends Pet{
public Dog(String name){
super("dog",name);
}
}
public static class Cat extends Pet{
public Cat(String name){
super("cat",name);
}
}
public static class PetEnter{
private Pet pet;
private long count;
public PetEnter(Pet pet,long count){
this.pet=pet;
this.count=count;
}
public Pet getPet() {
return this.pet;
}
public long getCount(){
return this.count;
}
public String getEnterPetType(){
return this.pet.getPetType();
}
}
public static class DogCatQueue{
private Queue<PetEnter> dogQ;
private Queue<PetEnter> catQ;
private long count;
public DogCatQueue(){
this.dogQ=new LinkedList<>();
this.catQ=new LinkedList<>();
this.count=0;
}
public void add(Pet pet){
if (pet.getPetType().equals("dog")){
dogQ.add(new PetEnter(pet,this.count++));
}else if (pet.getPetType().equals("cat")){
catQ.add(new PetEnter(pet,this.count++));
}else{
throw new RuntimeException("not a cat or not a dog");
}
}
public Cat pollCat(){
if (!catQ.isEmpty()){
return (Cat)catQ.poll().getPet();
}else{
throw new RuntimeException("cat empty");
}
}
public Dog pollDog(){
if (!dogQ.isEmpty()){
return (Dog)dogQ.poll().getPet();
}else{
throw new RuntimeException("dog empty");
}
}
public Pet pollAll(){
if (!catQ.isEmpty()&&!dogQ.isEmpty()){
if (catQ.peek().getCount()<dogQ.peek().getCount()){
return catQ.poll().getPet();
}else{
return dogQ.poll().getPet();
}
}else if (!catQ.isEmpty()){
return catQ.poll().getPet();
}else if(!dogQ.isEmpty()){
return dogQ.poll().getPet();
}else{
throw new RuntimeException("all empty");
}
}
public boolean isEmpty(){
return this.catQ.isEmpty()&&this.dogQ.isEmpty();
}
public boolean isDogQueueEmpty(){
return this.dogQ.isEmpty();
}
public boolean isCatQueueEmpty(){
return this.catQ.isEmpty();
}
}
public static void main(String[] args) {
Cat c1=new Cat("大花");
Cat c2=new Cat("二花");
Cat c3=new Cat("三花");
Dog d1=new Dog("大黑");
Dog d2=new Dog("二黑");
Dog d3=new Dog("三黑");
DogCatQueue dogCatQueue=new DogCatQueue();
dogCatQueue.add(c2);
dogCatQueue.add(d2);
dogCatQueue.add(c1);
dogCatQueue.add(d1);
dogCatQueue.add(d3);
dogCatQueue.add(c3);
System.out.println(dogCatQueue.pollDog());
System.out.println(dogCatQueue.pollCat());
System.out.println(dogCatQueue.pollAll());
System.out.println(dogCatQueue.pollAll());
System.out.println(dogCatQueue.pollAll());
System.out.println(dogCatQueue.pollAll());
System.out.println(dogCatQueue.pollAll());
}
}