-
Notifications
You must be signed in to change notification settings - Fork 27
/
chpt7-jukebox.js
82 lines (66 loc) · 2.01 KB
/
chpt7-jukebox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Design a jukebox using object oriented principles */
// Think about the characteristics and functions of a jukebox.
// Think about different types of jukeboxes, which one are we designing?
// Let's design a jukebox that takes money, assigns credit, has a lookup feature, and can give change
function JukeBox() {
this.songList = [];
this.playList = [];
this.credits = 0;
this.change = [
{ "amount" : 1.00,
"number": 20 },
{ "amount" : 0.25,
"number": 50 },
{ "amount" : 0.10,
"number": 25 },
{ "amount" : 0.05,
"number": 25 },
{ "amount" : 0.01,
"number": 50 }
];
}
// When we add a song, let's put them in alphabetical order so we can retrieve them in O(lg n) runtime
//
JukeBox.prototype.addSong = function(name, artist, length) {
var newSong = new Song(name, artist, length);
if (!this.songList.length) {
this.songList.push(newSong);
}
var start = 0;
var end = this.songList.length - 1;
var middle = Math.floor((end - start) / 2 + start);
while (start <= end) {
middle = Math.floor((end - start) / 2 + start);
if (this.songList[middle].name < newSong.name) {
start = middle + 1;
} else {
end = middle - 1;
}
}
this.songList.splice(middle, 0, newSong);
};
JukeBox.prototype.addCredits = function(money) {
while (money >= 0.25 && this.credits <= 5) {
money -= 0.25;
this.credits++;
}
while (money > 0) {
this.change.forEach(function(denom) {
while (money >= denom.amount) {
money = (money - denom.amount).toFixed(2);
denom.number--;
}
});
}
};
function Song(name, artist, length) {
this.name = name;
this.artist = artist;
this.length = length;
}
var jukeBox = new JukeBox();
jukeBox.addSong("Hey Ya", "Outkast", "3:15");
jukeBox.addSong("Gimme Shelter", "Rolling Stones", "5:00");
jukeBox.addSong("Bohemian Rhapsody", "Queen", "7:30");
jukeBox.addSong("Rapper's Delight", "Sugerhill Gang", "6:45");
jukeBox.addSong("What's Goin' On", "Marvin Gaye", "4:15");