Imagine you are designing an email management system that needs to manage email addresses. There are two types of email address: an individual address, which identifies a single email inbox, and a group address which acts as a container for other addresses. A group can be a member of other groups; furthermore, a (group or single) address can be a member of several groups.
Write a set of Java classes that models individual and group email addresses. Your classes should fulfil the following requirements:
- Every email address (individual or group) should have an associated string identifier
- Two email addresses should be regarded as equal if they have the same identifier. Don't forget that if you override
equals()
, you must also overridehashCode()
- On construction, a group email address should initially have no members
- It should be possible to add an email address to a group email address
- All email addresses should support a method,
getTargets()
. When invoked on email addressa
, this method should return the set of individual addresses to which an email addressed toa
must be sent
First, assume that all group memberships are acyclic; that is, no group can contain itself (directly or indirectly) as a member.
Once you have a working solution for the acyclic scenario, write an extended version that will work even in the presence of cycles.
You should also write a demonstration program to show your classes in action.