zoukankan      html  css  js  c++  java
  • 猫狗队列

    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"); } }
    实现一种狗猫队列的结构,要求如下: 用户可以调用add方法将cat类或dog类的 实例放入队列中; 用户可以调用pollAll方法,将队列中所有的实例按照进队列 的先后顺序依次弹出; 用户可以调用pollDog方法,将队列中dog类的实例按照 进队列的先后顺序依次弹出; 用户可以调用pollCat方法,将队列中cat类的实 例按照进队列的先后顺序依次弹出; 用户可以调用isEmpty方法,检查队列中是 否还有dog或cat的实例; 用户可以调用isDogEmpty方法,检查队列中是否有dog 类的实例; 用户可以调用isCatEmpty方法,检查队列中是否有cat类的实例

      1 package my_basic.class_3;
      2 
      3 import java.util.LinkedList;
      4 import java.util.Queue;
      5 
      6 /*猫狗队列*/
      7 public class Code_04_CatDogQueue {
      8     public static class Pet{
      9         private String type;
     10 
     11         public Pet(String type) {
     12             this.type = type;
     13         }
     14         public  String getPetType() {
     15             return this.type;
     16         }
     17     }
     18     public static class Dog extends Pet{
     19         public Dog() {
     20             super("dog");
     21         }
     22     }
     23     public static class Cat extends Pet{
     24         public Cat() {
     25             super("cat");
     26         }
     27     }
     28     
     29     public static class PetEnterQueue{
     30         private Pet pet;
     31         private Integer count;
     32         
     33         public PetEnterQueue(Pet pet, Integer count) {
     34             this.pet = pet;
     35             this.count = count;
     36         }
     37         public Pet getPet() {
     38             return pet;
     39         }
     40         
     41         public Integer getCount() {
     42             return count;
     43         }
     44     }
     45     
     46     public static class DogCatQueue{
     47         private Queue<PetEnterQueue> dogQ;
     48         private Queue<PetEnterQueue> catQ;
     49         private int count =0 ;
     50         
     51         
     52         public DogCatQueue() {
     53             super();
     54             this.dogQ = new LinkedList<PetEnterQueue>();
     55             this.catQ = new LinkedList<PetEnterQueue>();
     56             this.count = 0;
     57         }
     58         
     59         public void add(Pet pet) {
     60             if (pet.getPetType().equals("dog")) {
     61                 this.dogQ.add(new PetEnterQueue(pet, this.count++));
     62             }else if (pet.getPetType().equals("cat")) {
     63                 this.catQ.add(new PetEnterQueue(pet, count++));
     64             }else {
     65                 throw new IllegalArgumentException("不是猫或狗");
     66             }
     67         }
     68         public Pet pollAll() {
     69             if (!dogQ.isEmpty() && !catQ.isEmpty()) {
     70                 if (this.dogQ.peek().getCount() < this.catQ.peek().getCount()) {
     71                     return dogQ.poll().getPet();
     72                 }else {
     73                     return catQ.poll().getPet();
     74                 }
     75             }else if (!dogQ.isEmpty()) {
     76                 return dogQ.poll().getPet();
     77             }else if (!catQ.isEmpty()) {
     78                 return catQ.poll().getPet();
     79             }else {
     80                 throw new RuntimeException("queue is empty!");
     81             }
     82         }
     83         public Pet pollDog() {
     84             if (!isDogEmpty()) {
     85                 return dogQ.poll().getPet();
     86             }
     87             throw new RuntimeException("dogQ is empty!" );
     88         }
     89         public Pet pollCat() {
     90             if (!isCatEmpty()) {
     91                 return catQ.poll().getPet();
     92             }
     93             throw new RuntimeException("dogQ is empty!" );
     94         }
     95         public boolean isEmpty() {
     96             return (dogQ.isEmpty() && catQ.isEmpty())? true : false;
     97         }
     98         public boolean isDogEmpty() {
     99             return dogQ.isEmpty();
    100         }
    101         public boolean isCatEmpty() {
    102             return catQ.isEmpty();
    103         }
    104         
    105     }
    106     
    107     /*for test*/
    108     public static void main(String[] args) {
    109         DogCatQueue test = new DogCatQueue();
    110 
    111         Pet dog1 = new Dog();
    112         Pet cat1 = new Cat();
    113         Pet dog2 = new Dog();
    114         Pet cat2 = new Cat();
    115         Pet dog3 = new Dog();
    116         Pet cat3 = new Cat();
    117 
    118         test.add(dog1);
    119         test.add(cat1);
    120         test.add(dog2);
    121         test.add(cat2);
    122         test.add(dog3);
    123         test.add(cat3);
    124 
    125         test.add(dog1);
    126         test.add(cat1);
    127         test.add(dog2);
    128         test.add(cat2);
    129         test.add(dog3);
    130         test.add(cat3);
    131 
    132         test.add(dog1);
    133         test.add(cat1);
    134         test.add(dog2);
    135         test.add(cat2);
    136         test.add(dog3);
    137         test.add(cat3);
    138         while (!test.isDogEmpty()) {
    139             System.out.println(test.pollDog().getPetType());
    140         }
    141         while (!test.isEmpty()) {
    142             System.out.println(test.pollAll().getPetType());
    143         }
    144     }
    145 
    146 }
  • 相关阅读:
    delete、truncate、drop的区别
    Java闭包
    visio 画网格图
    GPU服务器中了木马病毒
    visio 同时标注上下标
    自动缩放字体
    latex 图片
    多GPU训练
    texstudio 外部查看器错误
    Linux lsof命令详解
  • 原文地址:https://www.cnblogs.com/lihuazhu/p/10835139.html
Copyright © 2011-2022 走看看