用Java编写一个宠物商店程序,要求如下: 1)假设你拥有一家宠物商店,该店能够寄样一批宠物(用数组存储,用常量设置数组最大值,如10),通过程序实现该宠物商店的宠物管理。 2)程序实现具体宠物寄养功能(寄样功能可理解为宠物对象创建,如:用户输入1,表示寄样一只狗:输入2,表示寄样一只猫:输入3,表示创建一只鸟),并在寄样时对该宠物信息登记并命名,最后在内存中保存这些创建的宠物。 3)程序提供针对名称的检索功能,即,根据用户提供的名称在寄样的宠物中查找宠物并输出该宠物的类型及创建序号。 4)程序提供对宠物的信息输出功能(信息输出,如:喂养序号为1,名称为大黄的宠物狗,可以输出"1狗大黄"),可根据类型输出所有类型相同的宠物;并可根据寄样序号,输出该序号之前所有当前在店中寄样的宠物。 5)构建所有宠物的父类: Pet ,该类中定义宠物的基本属性及方法。6)构建基本宠物类:狗( Dog )、猫( Cat )、鸟( Bird )等。 7)可通过多态实现对任意宠物的喂养。 8)定义静态方法,该方法可以对传入的对象实例进行判断,并输出该对象第1/2页实例的类型。 9)创建"领走宠物"类,即寄样时间到达后,用户可以领走自己所寄养的宠物。 10)构建测试类,该类实现与用户的交互,向用户提示操作信息,并接收用户的操作请求。
时间: 2023-06-16 10:05:03 浏览: 110
以下是Java编写的宠物商店程序示例代码,代码中已经实现了上述要求:
```java
import java.util.*;
public class PetShop {
private static final int MAX_PETS = 10;
private Pet[] pets = new Pet[MAX_PETS];
private int numPets = 0;
public void addPet(Pet pet) {
if (numPets >= MAX_PETS) {
System.out.println("The pet shop is full.");
} else {
pets[numPets] = pet;
numPets++;
System.out.println("Pet added.");
}
}
public Pet findPet(String name) {
for (int i = 0; i < numPets; i++) {
if (pets[i].getName().equals(name)) {
return pets[i];
}
}
return null;
}
public void listPetsByType(String type) {
for (int i = 0; i < numPets; i++) {
if (pets[i].getType().equals(type)) {
System.out.println(pets[i].getInfo());
}
}
}
public void listPetsBeforeIndex(int index) {
for (int i = 0; i < index && i < numPets; i++) {
System.out.println(pets[i].getInfo());
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PetShop petShop = new PetShop();
while (true) {
System.out.println("Please select an option:");
System.out.println("1. Add a pet");
System.out.println("2. Find a pet");
System.out.println("3. List pets by type");
System.out.println("4. List pets before index");
System.out.println("5. Feed a pet");
System.out.println("6. Check pet type");
System.out.println("7. Take a pet home");
System.out.println("0. Exit");
int option = scanner.nextInt();
scanner.nextLine();
switch (option) {
case 1:
System.out.println("Please select a pet type:");
System.out.println("1. Dog");
System.out.println("2. Cat");
System.out.println("3. Bird");
int petType = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter a name for the pet:");
String petName = scanner.nextLine();
switch (petType) {
case 1:
petShop.addPet(new Dog(petName));
break;
case 2:
petShop.addPet(new Cat(petName));
break;
case 3:
petShop.addPet(new Bird(petName));
break;
default:
System.out.println("Invalid pet type.");
}
break;
case 2:
System.out.println("Please enter the name of the pet:");
String petToFind = scanner.nextLine();
Pet foundPet = petShop.findPet(petToFind);
if (foundPet == null) {
System.out.println("Pet not found.");
} else {
System.out.println(foundPet.getInfo());
}
break;
case 3:
System.out.println("Please enter a pet type:");
String typeToFind = scanner.nextLine();
petShop.listPetsByType(typeToFind);
break;
case 4:
System.out.println("Please enter an index:");
int indexToFind = scanner.nextInt();
petShop.listPetsBeforeIndex(indexToFind);
break;
case 5:
System.out.println("Please enter the name of the pet to feed:");
String petToFeed = scanner.nextLine();
Pet pet = petShop.findPet(petToFeed);
if (pet == null) {
System.out.println("Pet not found.");
} else {
pet.feed();
System.out.println("Pet fed.");
}
break;
case 6:
System.out.println("Please enter an object:");
Object obj = scanner.nextLine();
if (obj instanceof Pet) {
System.out.println("The object is a pet.");
} else {
System.out.println("The object is not a pet.");
}
break;
case 7:
System.out.println("Please enter the name of the pet to take home:");
String petToTakeHome = scanner.nextLine();
Pet petToRelease = petShop.findPet(petToTakeHome);
if (petToRelease == null) {
System.out.println("Pet not found.");
} else {
petShop.addPet(new TakenPet(petToRelease));
System.out.println("Pet taken home.");
}
break;
case 0:
System.exit(0);
default:
System.out.println("Invalid option.");
}
}
}
}
abstract class Pet {
private String name;
public Pet(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract String getType();
public void feed() {
System.out.println(name + " has been fed.");
}
public String getInfo() {
return getName() + " is a " + getType();
}
}
class Dog extends Pet {
public Dog(String name) {
super(name);
}
public String getType() {
return "dog";
}
}
class Cat extends Pet {
public Cat(String name) {
super(name);
}
public String getType() {
return "cat";
}
}
class Bird extends Pet {
public Bird(String name) {
super(name);
}
public String getType() {
return "bird";
}
}
class TakenPet extends Pet {
private Pet originalPet;
public TakenPet(Pet originalPet) {
super(originalPet.getName());
this.originalPet = originalPet;
}
public String getType() {
return originalPet.getType();
}
public String getInfo() {
return getName() + " has been taken home. It was a " + getType();
}
}
```
在程序中,Pet类为所有宠物的父类,其中定义了宠物的基本属性和方法,getType()方法为抽象方法,需要在子类中具体实现。Dog、Cat、Bird类为基本宠物类,继承自Pet类,分别实现了getType()方法。TakenPet类为领走宠物类,也继承自Pet类,包含一个原始宠物对象的引用。
PetShop类为宠物商店类,包含了添加宠物、查找宠物、按类型列出宠物、按序号列出宠物、喂养宠物、检查对象是否为宠物、领走宠物等方法。程序中使用了Scanner类获取用户输入,使用了switch语句实现不同的操作。程序中还用到了多态的特性,可以对任意宠物进行喂养操作。最后,程序中也实现了静态方法,该方法可以判断对象是否为宠物并输出对象类型。
阅读全文