Suppose you have been contracted to build a property catalougue application for an estate agent. The application should manage the following types of properties:
- houses, which are either detached, semidetached or terraced
- bungalows, which are one storey houses and are also either detached, semidetached or terraced
- flats
- maisonettes, which are flats that take up more than one floor of a house
Write a set of skeleton Java classes and/or interfaces to model the entities in this application domain. The idea is to create appropriate classes and interfaces, and define the relationship between them. You do not need to add any methods to these classes/interfaces, and you do not need to add any fields unless you believe the relationship between the classes should be captured through composition.
Hint: at the top of your inheritance hierarchy there should be a class, Property
,
from which all properties are descended.
Explain the choices behind your design.
Next, write a PropertyCollection
class. An instance of PropertyCollection
should
hold a set of properties. PropertyCollection
should support these methods (at least; you can add
corresponding methods for other property types if you wish):
// Add a property to the collection
public void addProperty(Property p);
// Return the set of all houses in the collection
public Set<House> getHouses();
// Return the set of all bungalows in the collection
public Set<Bungalow> getBungalows();
// Return the set of all flats in the collection
public Set<Flat> getFlats();
// Return the set of all maisonettes in the collection
public Set<Maisonette> getMaisonettes();
// Return the set of all terraced houses in the collection
public Set<TeracedHouse> getTerracedHouses();
There are two ways you can implement these methods. One way is to use instanceof
to test the type of each property. The other way is to add (possibly abstract) methods isHouse()
, isBungalow()
,
isFlat()
, etc., to Property
and then override these methods in the various property subclasses
to return appropriate boolean values. Explain the pros and cons of each approach, decide which to use, and write a justification
of your choice.
Finally, write a PropertyDemo
class with a main
method.
The main
method should create a property collection, and add to it
the following:
- 1000 semi-detached houses
- 1000 terraced houses
- 100 detached houses
- 100 semi-detached bungalows
- 100 flats
- 20 maisonettes
- 20 detached bungalows
- 20 terraced bungalows
where the houses and flats are not bungalows or maisonettes, respectively.
After this, the main
method should use the methods you have
implemented in PropertyCollection
to check (using assertions) that the collection
contains:
- 2240 houses
- 140 bungalows
- 120 flats
- 20 maisonettes
- 1020 terraced houses
where bungalows and maisonettes are regarded as being houses and flats, respectively.