1.3-Object-Georienteerd-Programmeren

CD Collection

In this assignment you are going to model a CD collection (as in the actual CD’s your parents might have!). The intention is that you provide an interface to manage this collection, so that you can easily add CDs to this collection, can remove CD’s and determine some statistics of your collection. Obviously, as there are potential mistakes / error that can occur, we are going to use our own Exception to handle these situations.

A CD collection consists of CDs. Each CD has an artist and a title. On each CD there are tracks. Each track has a title and a length (time). From every CD the total length of all tracks must be retrievable, including all relevant attributes. In addition, the toString() must return a neatly numbered track listing (see below).

To easily work with all those different CDs, you need to write a CDCollection class that keeps track of which CDs you have in our possession. The following methods must be present in this collection class:

Challenge: The Duration class

Each track has a certain duration, previously called “length”. Although you might be tempted to store this as an integer, a more appropiate datatype would be an instance Duration.

We challenge you to do this exercise using the Duration class. Obviously, you need to read up on the Duration class yourself!

Example

Feel free to add any data that you want, but the following CD …

CD rushOfBlood = new CD("Coldplay", "A rush of blood to the head");
rushOfBlood.addTrack(new Track("Politik", Duration.ofSeconds(318)));
rushOfBlood.addTrack(new Track("In my place", Duration.ofSeconds(228)));
rushOfBlood.addTrack(new Track("God put a smile upon my face", Duration.ofSeconds(297)));
rushOfBlood.addTrack(new Track("The scientist", Duration.ofSeconds(309)));
rushOfBlood.addTrack(new Track("Clocks", Duration.ofSeconds(307)));
rushOfBlood.addTrack(new Track("Daylight", Duration.ofSeconds(327)));
rushOfBlood.addTrack(new Track("Green eyes", Duration.ofSeconds(223)));
rushOfBlood.addTrack(new Track("Warning sign", Duration.ofSeconds(331)));
rushOfBlood.addTrack(new Track("A whisper", Duration.ofSeconds(238)));
rushOfBlood.addTrack(new Track("A rush of blood to the head", Duration.ofSeconds(351)));
rushOfBlood.addTrack(new Track("Amsterdam", Duration.ofSeconds(319)));

… will produce the following output (toString):

Album: Coldplay - A rush of blood to the head
Total time: 0:54:08
Tracks:
1. Politik (05:18)
2. In my place (03:48)
3. God put a smile upon my face (04:57)
4. The scientist (05:09)
5. Clocks (05:07)
6. Daylight (05:27)
7. Green eyes (03:43)
8. Warning sign (05:31)
9. A whisper (03:58)
10. A rush of blood to the head (05:51)
11. Amsterdam (05:19)