More User Interface Developments
This section continues the previous one to develop some additional interacting graphical components and panels.
In this section you will learn:
- How to develop some additional types of interacting components and panels
- How to prepare the core system for use by the graphical user interface
- How to define a scrollable, selectable hierarchical tree of objects
Interacting components and panels for managing animals
This section continues the user interface development by creating the classes necessary to manage the addition, modification and removal of animals in the zoo. The classes will follow the same pattern as used in the previous section for zookeepers, although some different components will be used in certain places.
This part of the application will look like this:
Preparing the core system
Just as was done for the zookeeper user interface components, the animal components will communicate with the core system through the ZooAdministrator class. You may recall that Animal objects are being stored inside Pen objects, and that ZooAdministrator has defined three of the latter named lionPen, monkeyPen and penguinPen. Although this course is, for simplicity, sticking to just these three types of animal, it makes sense to design the software to make it easier to enhance in the future for many more animal pens and types. To this end, rather than using three individually named Pen objects you will store a Collection of Pen objects.
In ZooAdministrator replace the following line:
With this:
In the constructor you need to instantiate an empty collection before the call to createExamplePens():
- The collection is sorted so that the pens will be listed alphabetically.
You should define a method that returns the collection:
For the methods that generate sample data, since the individual Pen objects are no longer defined you can pass a reference to them as arguments:
Now modify the createExamplePens() method to create the three required pens and add each to the collection:
In the feedingTime() method you need to replace these lines:
With these:
Because the zoo is restricting itself to only three types of animal, it will prove helpful to define an enum for these types. Create a new enum inside the Animal class called Type:
It may be useful for client objects to easily find out what type a particular Animal object is, so define the following abstract getType() method in Animal:
Now override this method in Lion to return the LION enum value:
Now override this method in Monkey to return the MONKEY enum value:
Now override this method in Penguin to return the PENGUIN enum value:
Factory method
A useful technique when various subtypes of a class can be created is to provide a static factory method that determines what needs to be created and returns an object of that type. In the Animal class declare this method:
By defining the above method, it makes it much easier for client objects to create a particular type of object without having to code their own if...else... statements. The method is static because it doesn't operate on any existing Animal object. You will make use of this method later in this lesson.
It would be prudent here to beef up the validation of new animals. Currently, there is only a validation method for the age, so you will define new methods to validate the name and gender:
Call the above methods from the individual setter methods:
Define a validate() method that invokes the individual validation methods:
Call validate() from the constructor:
Because certain classes will be interested whenever an animal is added, changed or removed you can define an AnimalEvent class and AnimalListener interface in the virtualzoo.core.event package.
First, the AnimalEvent class:
The AnimalListener interface:
In ZooAdministrator you need an instance variable to store a collection of AnimalListener objects:
Instantiate the collection inside the constructor:
And methods to add or remove AnimalListener objects, and ones to fire the event changes:
With the above in place you can define a createAnimal() method that adds a new animal into the specified pen:
- Note the use of the
staticfactory methodcreate()you defined earlier, which will return either aLion,MonkeyorPenguinas appropriate
You need a changeAnimal() method to update an animal's details:
- Note that there is no argument for the animal's type, since once set through the
createAnimal()method it cannot be amended. (If you need to amend this because it was entered in error, then the user would need to remove and recreate the animal) - Note also the check to see whether the animal has been moved to a different pen; if so, the
relinquishAnimal()method removes it from its current pen andacquireAnimal()puts it into the new one
You need a removeAnimal() method to remove an animal:
You need a getAnimals() method that returns a collection of animals in a particular pen: