Please conduct class modeling (draw class diagram) and implement corresponding generation code according to the above scenarios.
The scenario is as follows:
1) Suppose there is a Zoo (implemented by the Zoo class), the name of the Zoo is defined by the user, and there are many animals in the Zoo (Animal class);
2) public void add(Animal animal) and remove(Animal animal) can be added to the zoo
3) You can count the number of zoos in the zoo public int count()
4) You can list all the animals in the zoo. public void listAnimals(), which displays the class name and animal name of the animal
5) There are many kinds of animals, and each animal has a name. All animals make noises
6) Considering the time relationship, it is assumed that there are only two kinds of animals, namely tigers (Tiger) and snakes (Snake). The sounds of tigers and snakes are roaring and hissing, respectively
7) There is a Manager to manage the zoo. He can release animals to the zoo or remove animals from the zoo. When the animal is put in, the animal will make its own sound. Call format: animal name is call!
8) Suppose there is a Game class to complete the project of simulating the management of releasing animals to and removing animals from the zoo. Refer to the main method in the code Game.
be careful:
1) Need to complete the design of class diagram
2) Complete the code
3) Think about what else can be better or better
Input requirements:
Multiple sets of inputs.
Format:
Zoo name manager name tiger name 1 tiger name 2 snake name 1 snake name 2 (spaces are not allowed between names)
Output requirements:
Refer to the Main method in Game.
Data example 1:
Input:
ZooKeeper Tom
LELE FANFAN XVXV SS
Output:
LELE is roaring!
FANFAN is roaring!
XVXV is hissing!
SS is hissing!
There are 4 Animals in Zoo ZooKeeper
Tiger:LELE
Tiger:FANFAN
Snake:XVXV
Snake:SS
Train of thought tips:
1) Animal is an abstract class, and animal cry wow() is an abstract method
2) Suppose o is an object o.getclass() Getname() can get the class name
3) ArrayList traversal problem
import java.util.*; import java.util.ArrayList; /***## 3528f7795fbd51e3 ##***/ class Manager { private String name; private Zoo manage; public Manager() { } public Manager(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Zoo getManage() { return manage; } public void setManage(Zoo manage) { this.manage = manage; } public void add(Animal a) { System.out.println(a.getName() + " is " + a.wow()); manage.add(a); } public void remove(Animal a) { manage.remove(a); } } class Zoo { private String name; private ArrayList<Animal> animals = new ArrayList<>(); public Zoo() { } public Zoo(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void add(Animal a ) { this.animals.add(a); } public void remove(Animal a) { this.animals.remove(a); } public int count() { return this.animals.size(); } public void listAnimals() { for(Animal a : animals) { System.out.println(a.getClass().getSimpleName() + ":" + a.getName()); } } } abstract class Animal { private String name; protected Animal() { } protected Animal(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract String wow(); } class Snake extends Animal { public Snake() { } public Snake(String name) { super(name); } public String wow(){ return "hissing!"; } } class Tiger extends Animal { public Tiger() { } public Tiger(String name) { super(name); } public String wow(){ return "roaring!"; } } public class Game { public static void main(String[] args) { // TODO Auto-generated method stub //The user inputs the name of the zoo, the name of the administrator, the name of the Tiger 1, the name of the Tiger 2, the name of the snake 1, and the name of the snake 2 (no spaces are allowed between names) Scanner input = new Scanner(System.in); while (input.hasNext()) { // Create a zoo Zoo zoo = new Zoo(input.next()); // There is an administrator Manager manager = new Manager(input.next()); // Manage the zoo you just created manager.setManage(zoo); // Create 2 tigers Tiger tiger1 = new Tiger(input.next()); Tiger tiger2 = new Tiger(input.next()); // Create 2 snakes Snake snake1 = new Snake(input.next()); Snake snake2 = new Snake(input.next()); // The keeper put them in the zoo Every time an animal is released to the zoo, the animal will make a cry //Call format: animal name is call! //The tiger's cry is roaring and the snake's cry is hissing manager.add(tiger1); manager.add(tiger2); manager.add(snake1); manager.add(snake2); // Check the number of animals in the zoo and list all animals System.out.println( "There are " + manager.getManage().count() + " Animals in Zoo " + manager.getManage().getName()); manager.getManage().listAnimals(); // After removing a tiger, check the animal manager.remove(tiger2); System.out.println( "There are " + manager.getManage().count() + " Animals in Zoo " + manager.getManage().getName()); manager.getManage().listAnimals(); } } }