Thursday, March 27, 2014

Design a musical juke box using object oriented principles

Problem

Design a musical juke box using object oriented principles.
Solution

We have already discussed the database design of how we will save information in the DB. Please refer this post for the same.

Our OOP design will be incorporating most of the design from there only.

Lets start with the design now.
class Song
1. has a song name, song artist,year,genre and so on.
   Song will be part of playlist 

class Playlist
1. has a track, current song (maybe)
2. has a queue of Songs.
3. constructor, current track and queue of songs.
4. getNextTrackToPlay(), return queue.peek()
5. queueUpTrack(), add to queue.

class CD : is a cd
 
class CD player:
1. playlist, get()
2. which cd, set(), get()
3. construct with either cd, playlist. or both
4. playTrank().

class User - He will play the cd player
1. has a name.
2. has a ID
3. get/set name & ID
4. constructor, given id and name
5. addUser(), given id and name, return new user.
 
class JukeBox:
1. has a CDplayer
2. has a User
3. has a collection of CDs
4. has a TrackSelector
5. construct with all above
6. getCurrentTrack(), return playing track.
7. processOneUser(User u), set the current user.

class TrackSelector
1. has a currentSong.
2. constructor, given current song.
3. setTrack(), set current song.
4. getCurrentSong(), return current song.
 

Now that we have a design lets have a look at the class diagram:
 Here is the code in java:
public class CD { }
public class CDPlayer {
    private Playlist p;
    private CD c;
    public Playlist getPlaylist() { return p; }
    public void setPlaylist(Playlist p) { this.p = p; }
    public CD getCD() { return c; }
    public void setCD(CD c) { this.c = c; }
    public CDPlayer(Playlist p) { this.p = p; }
    public CDPlayer(CD c, Playlist p) { ... }
    public CDPlayer(CD c) { this.c = c; }
    public void playTrack(Song s) { ... }
}
 
public class JukeBox {
    private CDPlayer cdPlayer;
    private User user;
    private Set<CD> cdCollection;
    private TrackSelector ts;
 
    public JukeBox(CDPlayer cdPlayer, User user, Set<CD> cdCollection,
    TrackSelector ts) { ... }
    public Song getCurrentTrack() { return ts.getCurrentSong(); }
    public void processOneUser(User u) { this.user = u; }
}
 
public class Playlist {
    private Song track;
    private Queue<Song> queue;
    public Playlist(Song track, Queue<Song> queue) { ... }
    public Song getNextTrackToPlay(){ return queue.peek(); }
    public void queueUpTrack(Song s){ queue.add(s); }
}
 
public class Song {
    private String songName;
}
 
public class TrackSelector {
    private Song currentSong;
    public TrackSelector(Song s) { currentSong=s; }
    public void setTrack(Song s) { currentSong = s; }
    public Song getCurrentSong() { return currentSong; }
}
 
public class User {
    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public long getID() { return ID; }
    public void setID(long iD) { ID = iD; }
    private long ID;
    public User(String name, long iD) { ... }
    public User getUser() { return this; }
    public static User addUser(String name, long iD) { ... }
}

You can download the complete code from github.

0 comments:

Post a Comment