3.7

本文介绍了一个动物收容所管理系统的设计与实现,该系统采用严格的“先入先出”原则,并支持按类型领养动物的功能。通过使用Java中的LinkedList实现队列,确保能够高效地管理收容所中的猫和狗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Topic 3.7 An animal shelter holds only dogs and cats, and operates on a strictly “first in, first out” basis. People must adopt either the “oldest”(based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structures to maintain this system and implement operations such as enqueue, dequeueAny, dequeueDog and dequeueCat. You may use the built-in LinkedList data structure.

//方法:建两个队列,需要的时候pop即可,需要最老的,比较两个队列的peek,挑出最老的。Use separate queues for dogs and cats, place them within a wrapper class. When dequeueAny, compare the peeks and return the oldest.

// 需要添加额外的特征,比如年龄,名字,性别,不要用数组,建一个类,声明属性,然后写一个方法返回该属性即可
public class Animal {
private int order; //只能被自己人修改,年龄
protected String name; 

public Animal(String name){
	this.name=name;
}

public String name(){
	return name;
}

public void setOrder(int order) {
	this.order = order;
}

public int getOrder() {
	return order;
}

public boolean isOlderThan(Animal a) {//先进来的顺序小
	return this.order < a.getOrder();
}
}
public class Cat extends Animal {
	public Cat(String name){//这是一个构造方法
		super(name);
}
}
public class Dog extends Animal{
	public Dog(String type){//这是一个构造方法,仅此就够了
		super(type);
	}
}
import java.util.LinkedList;

public class AnimalQueue {
	LinkedList<Dog> dogQueue = new LinkedList<Dog>();
	LinkedList<Cat> catQueue = new LinkedList<Cat>();
	private int order = 0;
	
	//用两个LinkekdList来模拟队列,先进来的在头,进一个就放到next,出的时候出LinkedList的peek即可
	public void enqueue(Animal a) {
		a.setOrder(order);
		order++;//序号是不能变的,进队列它的序号一定要加
		if (a instanceof Dog) {
			dogQueue.addLast((Dog) a);
		} else if (a instanceof Cat) {
			catQueue.addLast((Cat)a);
		}
	}
	
	//返回所有动物里最老的
	public Animal peek() {
		if (dogQueue.size() == 0) {
			return catQueue.peek();
		} else if(catQueue.size() == 0) {
			return dogQueue.peek();
		}
		Dog dog = dogQueue.peek();
		Cat cat = catQueue.peek();
		if (dog.isOlderThan(cat)) {
			return dog;
		} else {
			return cat;
		}
	}
	
	//LinkedList的poll方法弹出并返回LinkedList的头结点;
	//peek()方法只返回头结点,不弹出;
	//pop()方法是pop一个由此List代表的栈的顶端
	//remove()方法移除并返回头结点,同poll
	public Dog dequeueDogs() {
		return dogQueue.poll();
	}
	
	public Cat dequeueCats() {
		return catQueue.poll();
	}
	
	public Dog peekDogs() {
		return dogQueue.peek();
	}

	public Cat peekCats() {
		return catQueue.peek();
	}
	
	public Animal dequeueAny() {
		if (dogQueue.size() == 0) {
			return dequeueCats();
		} else if (catQueue.size() == 0) {
			return dequeueDogs();
		}
		Dog dog = dogQueue.peek();
		Cat cat = catQueue.peek();
		if (dog.isOlderThan(cat)) {
			return dequeueDogs();
		} else {
			return dequeueCats();
		}
	}

	public static void main(String[] args) {
		AnimalQueue animals = new AnimalQueue();
		animals.enqueue(new Cat("Callie"));
		animals.enqueue(new Cat("Kiki"));
		animals.enqueue(new Dog("Fido"));
		animals.enqueue(new Dog("Dora"));
		animals.enqueue(new Dog("Dapa"));
		System.out.println(animals.dequeueCats().name());	
		System.out.println(animals.dequeueDogs().name());		
		System.out.println(animals.dequeueAny().name());	
		}
}
//结果
Callie
Fido
Kiki






 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值