貓狗隊列
【題目】
寵物、狗和貓的類如下:
class Pet {
private String type;
public Pet(String type){
this.type = type;
}
public String getPetType(){
return this.type;
}
}
class Dog extends Pet{
public Dog(){
super("dog");
}
}
class Cat extends Pet{
public Cat(){
super("cat");
}
}
實現一種貓狗隊列的結構,要求如下:
- 用戶可以調用 push 的方法,將 cat 類或者 dog 類的實例放入隊列中;
- 用戶可以調用 popAll 的方法,將隊列中所有的實例按照進隊列的先後順序依次彈出;
- 用戶可以調用 popDog 的方法,將隊列中的 dog 類實例按照進隊列的先後順序依次彈出;
- 用戶可以調用 popCat 的方法,將隊列中的 cat 類實例按照進隊列的先後順序依次彈出;
- 用戶可以調用 isEmpty 的方法,檢查隊列中是否還有 dog 或者 cat 類的實例;
- 用戶可以調用 isDogEmpty 的方法,檢查隊列中是否還有 dog 類的實例;
- 用戶可以調用 isCatEmpty 的方法,檢查隊列中是否還有 cat 類的實例。
【思路】
以時間戳的方法區分貓狗的進入順序,分別在DogCatQueue中創建兩個Pet子類隊列(Cat,Dog),在添加貓狗實體時,將時間戳加一,在彈出時,比較隊列peek()中時間戳,選擇彈出哪個隊列中的實體對象。
package com.javaman.chap1;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class CatDog{
public static void main(String[]args){
Scanner in = new Scanner(System.in);
DogCatQueue dogCatQueue = new DogCatQueue();
int n = in.nextInt();
for(int i=1 ;i<=n;++i){
String temp = in.next();
if(temp.equals("cat")){
dogCatQueue.add(new Cat());
}else if(temp.equals("dog")){
dogCatQueue.add(new Dog());
}
}
while (!dogCatQueue.isEmpty()){
System.out.println(dogCatQueue.pollAll().toString());
}
}
}
class Pet {
private String type;
public Pet(String type){
this.type = type;
}
public String getPetType(){
return this.type;
}
}
class Dog extends Pet{
public Dog(){
super("dog");
}
}
class Cat extends Pet{
public Cat(){
super("cat");
}
}
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 getEnterPetType(){
return this.pet.getPetType();
}
}
class DogCatQueue{
private Queue<PetEnterQueue> dogQ;
private Queue<PetEnterQueue> 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")){
this.dogQ.add(new PetEnterQueue(pet,this.count++));
}else if(pet.getPetType().equals("cat")){
this.catQ.add(new PetEnterQueue(pet,count++));
}else{
throw new RuntimeException("err,not cat or dog");
}
}
public Pet pollAll(){
if(!this.dogQ.isEmpty()&&!this.catQ.isEmpty()){
if(this.dogQ.peek().getCount()<this.catQ.peek().getCount()){
return this.dogQ.poll().getPet();
}else {
return this.catQ.poll().getPet();
}
}else if(!this.dogQ.isEmpty()){
return this.dogQ.poll().getPet();
}else if(!this.catQ.isEmpty()){
return this.catQ.poll().getPet();
}else {
throw new RuntimeException("err,quwuw is empty");
}
}
public Dog pollDog(){
if(!this.dogQ.isEmpty()){
return (Dog)this.dogQ.poll().getPet();
}else {
throw new RuntimeException("Dog queue is empty");
}
}
public Cat pollCat(){
if(!this.catQ.isEmpty()){
return (Cat) this.catQ.poll().getPet();
}else {
throw new RuntimeException("Cat quque is empty");
}
}
public boolean isEmpty(){
return this.dogQ.isEmpty()&&this.catQ.isEmpty();
}
public boolean isDogQueueEmpty(){
return this.dogQ.isEmpty();
}
public boolean isCatQueueEmpty(){
return this.catQ.isEmpty();
}
}
【總結】
存儲同一父類的不同子類實體時,可以創建不同的子類隊列進行存儲,利用時間戳等進行狀態標註,在彈出時,返回值可以爲同一父類類型。當彈出特定子類對象時,可以進行向下轉型。