interview-preparation / what-we-do

0 stars 8 forks source link

[Object-Oriented Design] interview questions #3 #110

Closed btakeya closed 5 years ago

btakeya commented 5 years ago

Jukebox: Design a musical jukebox using object-oriented principles.

RoyMoon commented 5 years ago

class Artist {
 public:
  string name;
};

struct CD {
 public:
  long Id;
  Artist artist;
};

class Song {
 public:
  long id;
  string title;
  string date;
}; 

class Playlist {
  queue<Song> q;
 public : 

  Song getCurrentSong() {
    return q.front();  
  }
  void reserveSong(Song s){
   q.push(s)
 }
  Song NextSong(){
      q.pop();
    return q.front(); 
  }
};

class CDPlayer {
  Playlist playlist; 
  CD cd;
 public :
  CDPlayer(Playlist p) : playlist(p) {};

  void playSong(Song s) {};

  Playlist getPlayList() { return playlist; } 
  void setPlayList(Playlist p) { playlist = p; }
};

class User{
  string name;
  long id;
 public :
  User(string n, long i) : name(n), id(i) {}
  string getName() { return name;}
  long getID() { return id;}
  static User * addUser(string name, long id){
    return new User(name, id);
  }
};

class Jukebox {
  map<string, CD> cdMap;
  CDPlayer cdplayer;
  User user;
  Playlist playlist;
 public:
  Jukebox(CDPlayer p, User u) : user(u), cdplayer(p){ }

  Song getCurrentSong() { playlist.getCurrentSong();}

  void setUser(User u) {user= u;}
};