java动物园系统源代码 java 动物园

java编程几个类,可以实现动物园中几个科下动物的增删改查import java.util.ArrayList;
import java.util.List;
class Animal {
public String name;
public String color;
public String toString() {
return " Name: " + this.name + "Color: " + this.color;
}
/**
* 动物吃东西
*
* @param foodName
*食物名称
*/
public void eat(String foodName) {
}
}
class Dog extends Animal {
public Dog() {
}
public Dog(String name, String color) {
this.name = name;
this.color = color;
}
public void eat(String foodName) {
System.out.println("Dog: " + this.name + " like to eat: " + foodName);
}
}
class Cat extends Animal {
public Cat() {
}
public Cat(String name, String color) {
this.name = name;
this.color = color;
}
public void eat(String foodName) {
System.out.println("Cat: " + this.name + " like to eat: " + foodName);
}
}
class Tiger extends Animal {
public Tiger() {
}
public Tiger(String name, String color) {
this.name = name;
this.color = color;
}
public void eat(String foodName) {
System.out.println("Tiger: " + this.name + " like to eat: " + foodName);
}
}
class Lion extends Animal {
public Lion() {
}
public Lion(String name, String color) {
this.name = name;
this.color = color;
}
public void eat(String foodName) {
System.out.println("Tiger: " + this.name + " like to eat: " + foodName);
}
}
public class Zoo {
public static ListAnimal zooList = new ArrayListAnimal();
/**
* 添加Animal对象
*
* @param obj
*/
【java动物园系统源代码 java 动物园】 public void addAnimal(Animal obj) {
zooList.add(obj);
}
/**
* 根据animal的名字删除对象
*
* @param obj
*Animal对象
*/
public void deleteAnimal(Animal obj) {
boolean deleteFlag = false;
for (int index = 0; null != zooListindexzooList.size(); index++) {
if (zooList.get(index).name.equals(obj.name)) {
zooList.remove(index);
deleteFlag = true;
System.out.println("删除: " + obj + " 成功");
}
}
if (!deleteFlag) {
System.out.println("找不到该动物: " + obj);
}
}
/**
* 更新Animal对象信息
*
* @param obj
*Animal对象
*/
public void updateAnimal(Animal obj) {
boolean flag = false;
for (int index = 0; null != zooListindexzooList.size(); index++) {
if (zooList.get(index).name.equals(obj.name)) {
zooList.get(index).name = obj.name;
zooList.get(index).color = obj.color;
flag = true;
System.out.println("\n修改成功");
}
}
if (!flag) {
System.out.println("找不到该动物: " + obj);
}
}
/**
* 根据动物名字查询
*
* @param animalName
*动物名字
*/
public void checkAniaml(String animalName) {
boolean flag = false;
for (int index = 0; null != zooListindexzooList.size(); index++) {
if (zooList.get(index).name.equals(animalName)) {
System.out.println(zooList.get(index));
flag =true;
}
}
if (!flag) {
System.out.println("找不到该动物: " + animalName);
}
}
public void display(ListAnimal list) {
for (int index = 0; null != listindexlist.size(); index++) {
System.out.println("\n" + list.get(index));
}
}
public static void main(String[] args) {
Zoo zoo = new Zoo();
zoo.addAnimal(new Dog("dog1", "black"));
zoo.addAnimal(new Dog("dog2", "black"));
zoo.addAnimal(new Lion("Lion1", "black"));

推荐阅读