Java动物收容所管理系统代码
时间: 2023-09-30 11:04:25 浏览: 80
关于动物园管理系统的程序代码
5星 · 资源好评率100%
很抱歉,我无法提供完整的Java动物收容所管理系统代码,但是我可以提供一个大致的代码框架:
```
import java.util.ArrayList;
import java.util.Scanner;
class Animal {
private String name;
private int age;
private String species;
public Animal(String name, int age, String species) {
this.name = name;
this.age = age;
this.species = species;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getSpecies() {
return species;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSpecies(String species) {
this.species = species;
}
public String toString() {
return "Name: " + name + ", Age: " + age + ", Species: " + species;
}
}
class AnimalShelter {
private ArrayList<Animal> animals = new ArrayList<>();
public void addAnimal(Animal animal) {
animals.add(animal);
}
public void removeAnimal(Animal animal) {
animals.remove(animal);
}
public ArrayList<Animal> getAnimals() {
return animals;
}
}
public class AnimalShelterManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
AnimalShelter animalShelter = new AnimalShelter();
while (true) {
System.out.println("Select an option:");
System.out.println("1. Add an animal");
System.out.println("2. Remove an animal");
System.out.println("3. View animals");
System.out.println("4. Exit");
int option = scanner.nextInt();
switch (option) {
case 1:
System.out.println("Enter animal name:");
String name = scanner.next();
System.out.println("Enter animal age:");
int age = scanner.nextInt();
System.out.println("Enter animal species:");
String species = scanner.next();
Animal animal = new Animal(name, age, species);
animalShelter.addAnimal(animal);
System.out.println("Animal added successfully");
break;
case 2:
System.out.println("Enter animal name:");
name = scanner.next();
System.out.println("Enter animal age:");
age = scanner.nextInt();
System.out.println("Enter animal species:");
species = scanner.next();
animal = new Animal(name, age, species);
if (animalShelter.getAnimals().contains(animal)) {
animalShelter.removeAnimal(animal);
System.out.println("Animal removed successfully");
} else {
System.out.println("Animal not found");
}
break;
case 3:
ArrayList<Animal> animals = animalShelter.getAnimals();
if (animals.isEmpty()) {
System.out.println("No animals in the shelter");
} else {
for (Animal a : animals) {
System.out.println(a);
}
}
break;
case 4:
System.exit(0);
default:
System.out.println("Invalid option");
break;
}
}
}
}
```
该代码实现了一个简单的动物收容所管理系统,可以添加、删除和查看动物。您可以根据需要进行修改和扩展。
阅读全文