西电java宠物商店程序
时间: 2023-11-28 19:05:26 浏览: 67
很高兴为你提供帮助!以下是一个简单的Java宠物商店程序的示例代码:
```java
import java.util.Scanner;
public class PetShop {
private static Scanner scanner = new Scanner(System.in);
private static Animal[] animals = new Animal[10];
public static void main(String[] args) {
System.out.println("欢迎来到宠物商店!");
int choice;
do {
System.out.println("请选择您要进行的操作:");
System.out.println("1. 添加宠物");
System.out.println("2. 展示所有宠物");
System.out.println("3. 查找宠物");
System.out.println("4. 退出");
choice = scanner.nextInt();
switch (choice) {
case 1:
addPet();
break;
case 2:
showPets();
break;
case 3:
searchPet();
break;
case 4:
System.out.println("谢谢惠顾!");
break;
default:
System.out.println("无效的选择!");
}
} while (choice != 4);
}
private static void addPet() {
System.out.println("请输入宠物类型(1. 猫,2. 狗):");
int type = scanner.nextInt();
System.out.println("请输入宠物名称:");
String name = scanner.next();
System.out.println("请输入宠物年龄:");
int age = scanner.nextInt();
System.out.println("请输入宠物性别(1. 雄性,2. 雌性):");
int gender = scanner.nextInt();
Animal animal = null;
if (type == 1) {
System.out.println("请输入猫的品种:");
String breed = scanner.next();
animal = new Cat(name, age, gender, breed);
} else if (type == 2) {
System.out.println("请输入狗的品种:");
String breed = scanner.next();
animal = new Dog(name, age, gender, breed);
} else {
System.out.println("无效的宠物类型!");
return;
}
for (int i = 0; i < animals.length; i++) {
if (animals[i] == null) {
animals[i] = animal;
System.out.println("添加宠物成功!");
return;
}
}
System.out.println("宠物库存已满,无法添加新宠物!");
}
private static void showPets() {
for (Animal animal : animals) {
if (animal != null) {
System.out.println(animal);
}
}
}
private static void searchPet() {
System.out.println("请输入宠物名称:");
String name = scanner.next();
for (Animal animal : animals) {
if (animal != null && animal.getName().equals(name)) {
System.out.println(animal);
return;
}
}
System.out.println("未找到该宠物!");
}
}
abstract class Animal {
private String name;
private int age;
private int gender;
public Animal(String name, int age, int gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getGender() {
return gender;
}
public abstract String getBreed();
@Override
public String toString() {
return "宠物类型:" + getClass().getSimpleName() + ",姓名:" + name + ",年龄:" + age + ",性别:" + (gender == 1 ? "雄性" : "雌性") + ",品种:" + getBreed();
}
}
class Cat extends Animal {
private String breed;
public Cat(String name, int age, int gender, String breed) {
super(name, age, gender);
this.breed = breed;
}
@Override
public String getBreed() {
return breed;
}
}
class Dog extends Animal {
private String breed;
public Dog(String name, int age, int gender, String breed) {
super(name, age, gender);
this.breed = breed;
}
@Override
public String getBreed() {
return breed;
}
}
```
这个程序实现了一个简单的宠物商店,用户可以添加、展示、查找宠物。程序中使用了抽象类Animal表示宠物的基本信息,然后定义了猫和狗两个类继承Animal,并在其中添加了品种的属性。用户可以通过输入宠物类型、名称、年龄、性别和品种来添加新宠物,然后查看所有宠物或者通过宠物名称查找特定宠物。
阅读全文