Lab-07 — Add Sorting and Property-Change-Handling to Lab-06-VehicleFleet

1. Specification

1.1. Add Sorting with Natural Ordering

Add natural ordering to Classes InventoryCode and Vehicle by implementing Interface Comparable<>.

InventoryCode should use the textual representation for that.

Vehicle should use the sorting of InventoryCode.

1.2. Add 2 Comparator Classes for Vehicle

The first comparator class should use the numeric part of InventoryCode objects. For this purpose write a int extractInvtNr() within class InventoryCode which uses Integer.parseInt(…​) and substring(beginIdx)) to get the number.

The second one should use attributes since and if the same the emptyWeight. LocalDate implements Comparable<..>, so this one can be used.

1.3. Add a sorting variant of printVehicles(..)

The signature is printVehiclesSorted(title: String, Comparator<Vehicle> comparator, vehicles Collection<Vehicle>). Better would probably be to implement as a static method (same true for printVehicles(..)), so it is clear it will/can not use the map, but instead gets the collection as a parameter. The content of the collection has to be put into an ArrayList first (only a vew Collection classes are able to sort, so we have to transfer the content).

1.4. Add calls to printVehiclesSorted() to the Tests

Both comparators and also natural sorting (using aVehicle.compareTo(…​)) should be tested. Natural Ordering is done by setting the comparator object to null, the other cases need a comparator object (e.g. by new TheComparator()) as parameter.

1.5. Add Property-Change Handling

Extend Vehicle and its subclasses Car, Truck, Trailer with setters for every attribute but invtCode (which is immutable for us). Implement PropertyChangeSupport for them.

Attribute pcs (Class PropertyChangeSupport) has to be reachable by subclass objects, but not for everyone. Luckily Java has a visibility level protected (like private and public) which allows access only for members of the same package AND subclass objects.

Class VehicleFleet should register itself as a listener. After every change a new printout of the fleet is done (within method propertyChange(…​) of Interface PropertyChangeListener).

Add all the necessary to see the effect within the test Class/method.

2. Optional

To make the test more interactive, use Scanner class to read new Values for one or more of the attributes.