前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >猫狗队列

猫狗队列

作者头像
HelloVass
发布2018-09-12 10:29:19
6180
发布2018-09-12 10:29:19
举报

题目

实现一个猫狗队列


// 宠物类
public class Pet {

    private String mType;

    public Pet(String type) {
        mType = type;
    }

    public String getType() {
        return mType;
    }
}

// 狗
public class Dog extends Pet {

    public Dog(String type) {
        super(type);
    }
}

// 喵
public class Cat extends Pet {

    public Cat(String type) {
        super(type);
    }
}

实现一种猫狗队列的结构,要求:

  • add 方法将 cat 或者 dog 放入到队列中
  • pollAll 方法将队列中所有的实例按照入队的先后顺序依次弹出
  • pollCat 方法将队列中的 cat 按照入队先后顺序依次弹出
  • pollDog 方法将队列中的 dog 按照入队先后顺序依次弹出
  • isEmpty 方法检查队列中是否还有 dog 或者 cat 的实例
  • isCatQueueEmpty 方法检查 cat 队列是否还有 cat
  • isDogQueueEmpty 方法检查 dog 队列是否还有 dog

实现

使用组合,增加一个时间戳的字段


public class PetWithTimestamp {

    private Pet mPet;

    private long mTimestamp;

    public PetWithTimestamp(Pet pet, long timestamp) {
        mPet = pet;
        mTimestamp = timestamp;
    }

    public Pet getPet() {
        return mPet;
    }

    public long getTimestamp() {
        return mTimestamp;
    }

    public String getType() {
        return mPet.getType();
    }
}

CatDogQueue 类设计


public class CatDogQueue {

    private Queue<PetWithTimestamp> mCatQueue;

    private Queue<PetWithTimestamp> mDogQueue;


    public CatDogQueue() {
        mCatQueue = new LinkedList<>();
        mDogQueue = new LinkedList<>();
    }

    public void add(Pet pet) {

        if (pet == null) {
            throw new IllegalArgumentException("pet can't be null");
        }

        switch (pet.getType()) {

            case "cat":
                mCatQueue.add(new PetWithTimestamp(pet, System.currentTimeMillis()));
                break;

            case "dog":
                mDogQueue.add(new PetWithTimestamp(pet, System.currentTimeMillis()));
                break;

            default:
                break;
        }
    }

    public Pet pollAll() {

        if (!mCatQueue.isEmpty() && !mDogQueue.isEmpty()) {

            if (mCatQueue.peek().getTimestamp() < mDogQueue.peek().getTimestamp()) {
                return mCatQueue.poll().getPet();
            } else {
                return mDogQueue.poll().getPet();
            }

        } else if (!mCatQueue.isEmpty()) {

            return mCatQueue.poll().getPet();

        } else if (!mDogQueue.isEmpty()) {

            return mDogQueue.poll().getPet();

        } else {
            throw new IllegalStateException("queue is empty!");
        }
    }

    public Cat pollCat() {
        if (!mCatQueue.isEmpty()) {
            return (Cat) mCatQueue.poll().getPet();
        } else {
            throw new IllegalStateException("dog queue is empty!");
        }
    }

    public Dog pollDog() {
        if (!mDogQueue.isEmpty()) {
            return (Dog) mDogQueue.poll().getPet();
        } else {
            throw new IllegalStateException("dog queue is empty!");
        }
    }
    
    public boolean isEmpty() {
        return mCatQueue.isEmpty() && mCatQueue.isEmpty();
    }

    public boolean isCatQueueEmpty() {
        return mCatQueue.isEmpty();
    }

    public boolean isDogQueueEmpty() {
        return mDogQueue.isEmpty();
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
    • 实现一种猫狗队列的结构,要求:
    • 实现
      • 使用组合,增加一个时间戳的字段
        • CatDogQueue 类设计
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档