貓狗隊(duì)列

【題目】
寵物、狗和貓的類如下: public class Pet{
private String type;
public Pet(String type){
this.type = type;
}

public string getPetType(){
return this.type;
}

public class Dog extends Pet {
public Dog(){
super("dog");
}
}

public class Cat extends Pet {
public Cat(){
super("cat");
}
}
實(shí)現(xiàn)一種狗貓隊(duì)列的結(jié)構(gòu),要求如下: 用戶可以調(diào)用add方法將cat類或dog類的實(shí)例放入隊(duì)列中; 用戶可以調(diào)用pollAll方法,將隊(duì)列中所有的實(shí)例按照進(jìn)隊(duì)列的先后順序依次彈出; 用戶可以調(diào)用pollDog方法,將隊(duì)列中dog類的實(shí)例按照進(jìn)隊(duì)列的先后順序依次彈出; 用戶可以調(diào)用pollCat方法,將隊(duì)列中cat類的實(shí)例按照進(jìn)隊(duì)列的先后順序依次彈出; 用戶可以調(diào)用isEmpty方法,檢查隊(duì)列中是否還有dog或cat的實(shí)例; 用戶可以調(diào)用isDogEmpty方法,檢查隊(duì)列中是否有dog類的實(shí)例; 用戶可以調(diào)用isCatEmpty方法,檢查隊(duì)列中是否有cat類的實(shí)例。

【要求】
要求即題目。

【解答】
見源碼!

package pers.mao.stackAndQueue.demo_4;

/**
 * @author Mao Qingbo
 * @date 2020-10-03
 */
public class PetEnterQueue {
    private Pet pet;
    private long count;

    public PetEnterQueue(Pet pet,long count){
        this.pet = pet;
        this.count = count;
    }

    public Pet getPet() {
        return this.pet;
    }

    public long getCount() {
        return this.count;
    }

    public String getPetEntranceType(){
        return this.pet.getPetType();
    }
}
package pers.mao.stackAndQueue.demo_4;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Mao Qingbo
 * @date 2020-10-03
 */
public class DogCatQueue {
    private Queue<PetEnterQueue> DogQ;
    private Queue<PetEnterQueue> CatQ;
    private long count;

    public DogCatQueue() {
        this.DogQ = new LinkedList<PetEnterQueue>();
        this.CatQ = new LinkedList<PetEnterQueue>();
        this.count = 0;
    }

    public void add(Pet pet){
        if(pet.getPetType().equals("dog")){
             this.DogQ.add(new PetEnterQueue(pet,this.count++));
        }
        else if(pet.getPetType().equals("cat")) {
            this.CatQ.add(new PetEnterQueue(pet, this.count++));
        }
        else{
                throw new RuntimeException("error,not dog or cat");
            }
        }

        public Pet pollAll(){
            if(!DogQ.isEmpty()&&!CatQ.isEmpty()){
                if(DogQ.peek().getCount()<CatQ.peek().getCount()){
                    return this.DogQ.poll().getPet();
                }
                else{
                    return this.CatQ.poll().getPet();
                }
            }
            else if(!DogQ.isEmpty()){
                return this.DogQ.poll().getPet();
            }
            else if(!CatQ.isEmpty()){
                return this.CatQ.poll().getPet();
            }
            else{
                throw new RuntimeException("error,Queue is empty!");
            }
        }

        public Pet pollDog(){
            if(!this.isDogEmpty()){
                return  this.DogQ.poll().getPet();
            }
            else
                throw new RuntimeException("no dog!");
        }

    public Pet pollCat(){
        if(!this.isCatEmpty()){
            return  this.CatQ.poll().getPet();
        }
        else
            throw new RuntimeException("no cat!");
    }

    public boolean isEmpty(){
        return this.DogQ.isEmpty()&&this.CatQ.isEmpty();
    }

    public boolean isDogEmpty(){
        return this.DogQ.isEmpty();
    }

    public boolean isCatEmpty(){
        return this.CatQ.isEmpty();
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容