用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-15 07:08:11 浏览: 95
以下是一个简单的宠物商店程序,实现了上述要求:
Pet.java
```java
public abstract class Pet {
protected static int count = 0;
protected int id;
protected String name;
protected int age;
protected String type;
public Pet(String name, int age, String type) {
this.id = ++count;
this.name = name;
this.age = age;
this.type = type;
}
public abstract void feed();
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getType() {
return type;
}
public String toString() {
return id + " " + type + " " + name;
}
}
```
Dog.java
```java
public class Dog extends Pet {
public Dog(String name, int age) {
super(name, age, "狗");
}
public void feed() {
System.out.println(name + "正在吃骨头");
}
}
```
Cat.java
```java
public class Cat extends Pet {
public Cat(String name, int age) {
super(name, age, "猫");
}
public void feed() {
System.out.println(name + "正在吃鱼");
}
}
```
Bird.java
```java
public class Bird extends Pet {
public Bird(String name, int age) {
super(name, age, "鸟");
}
public void feed() {
System.out.println(name + "正在吃鸟粮");
}
}
```
PetShop.java
```java
import java.util.Scanner;
public class PetShop {
private static final int MAX_PETS = 10;
private Pet[] pets = new Pet[MAX_PETS];
private int petCount = 0;
public void addPet(Pet pet) {
if (petCount < MAX_PETS) {
pets[petCount++] = pet;
System.out.println("添加成功");
} else {
System.out.println("宠物商店已满,添加失败");
}
}
public void searchByName(String name) {
boolean found = false;
for (int i = 0; i < petCount; i++) {
if (pets[i].getName().equals(name)) {
System.out.println(pets[i].getType() + " " + pets[i].getId());
found = true;
}
}
if (!found) {
System.out.println("未找到宠物");
}
}
public void outputByType(String type) {
for (int i = 0; i < petCount; i++) {
if (pets[i].getType().equals(type)) {
System.out.println(pets[i]);
}
}
}
public void outputByIndex(int index) {
for (int i = 0; i < index && i < petCount; i++) {
System.out.println(pets[i]);
}
}
public void feedPet(int id) {
boolean found = false;
for (int i = 0; i < petCount; i++) {
if (pets[i].getId() == id) {
pets[i].feed();
found = true;
}
}
if (!found) {
System.out.println("未找到宠物");
}
}
public void checkOut(int id) {
boolean found = false;
for (int i = 0; i < petCount; i++) {
if (pets[i].getId() == id) {
System.out.println("领走宠物:" + pets[i].getName());
pets[i] = pets[--petCount];
found = true;
}
}
if (!found) {
System.out.println("未找到宠物");
}
}
public static void main(String[] args) {
PetShop shop = new PetShop();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.添加宠物 2.按名字查找宠物 3.按类型输出宠物 4.按序号输出宠物 5.喂养宠物 6.领走宠物 7.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请选择宠物类型:1.狗 2.猫 3.鸟");
int petType = scanner.nextInt();
System.out.println("请输入宠物名字:");
String petName = scanner.next();
System.out.println("请输入宠物年龄:");
int petAge = scanner.nextInt();
switch (petType) {
case 1:
shop.addPet(new Dog(petName, petAge));
break;
case 2:
shop.addPet(new Cat(petName, petAge));
break;
case 3:
shop.addPet(new Bird(petName, petAge));
break;
default:
System.out.println("无效的宠物类型");
break;
}
break;
case 2:
System.out.println("请输入宠物名字:");
String name = scanner.next();
shop.searchByName(name);
break;
case 3:
System.out.println("请输入宠物类型:");
String type = scanner.next();
shop.outputByType(type);
break;
case 4:
System.out.println("请输入序号:");
int index = scanner.nextInt();
shop.outputByIndex(index);
break;
case 5:
System.out.println("请输入宠物ID:");
int id = scanner.nextInt();
shop.feedPet(id);
break;
case 6:
System.out.println("请输入宠物ID:");
int checkoutId = scanner.nextInt();
shop.checkOut(checkoutId);
break;
case 7:
return;
default:
System.out.println("无效的操作");
break;
}
}
}
}
```
阅读全文