Skip to content

Instantly share code, notes, and snippets.

@seldomatt
Created October 19, 2012 19:41
Show Gist options
  • Save seldomatt/3920253 to your computer and use it in GitHub Desktop.
Save seldomatt/3920253 to your computer and use it in GitHub Desktop.
Jukebox rb Redux (object oriented)
#test suite
def assert_equal(actual, expected)
if actual == expected
puts "pass"
else
puts "fail: expected #{expected} but was #{actual}"
end
end
def assert(statement)
if statement
puts "pass"
else
puts "fail: expected #{statement} to be true"
end
end
#----------------------------------------------------------
# Build a Jukebox
# create a file jukebox.rb
# When that program is run, it should introduce itself
# to the user and accept input from the user using the gets command.
# The jukebox should respond to 3 commands, help, play, list and exit.
# The help command should output instructions for the user
# on how to use the jukebox.
class Song
attr_accessor :name, :artist, :genre
def initialize(artist= "nil", name= "nil", genre="nil")
@artist = artist
@name = name
end
end
class Artist
attr_accessor :name, :songs
def initialize(name)
@name = name
end
end
class Jukebox
attr_accessor :songs
def initialize
@songs = []
end
end
class Session
attr_accessor :jukebox, :status, :user_input
def initialize(jukebox)
@jukebox = jukebox
@status = true
end
def prompt
puts "Enter a command ('help' to see a list of command options):"
self.user_input = gets.downcase.strip.split
command= self.user_input[0]
if self.respond_to?(command.to_sym)
self.send(command.to_sym)
else
puts "Sorry, that's not a valid option"
end
end
def play
choice = (self.user_input[1].to_i) - 1
if choice >= 0 && self.jukebox.songs[choice]
puts "Now playing #{self.jukebox.songs[choice].name} by #{self.jukebox.songs[choice].artist}"
else
puts "That's not a valid song choice"
end
end
def list
self.jukebox.songs.each_with_index do |song, index|
puts "play #{index +1 } to hear #{song.name} by #{song.artist}"
end
end
def help
puts "To see a list of songs, type 'list'. To play a song, type 'play [songnumber]'. To exit the program, type 'exit'."
end
def exit
self.status = false
end
end
#can initialize a song
begin
assert Song.new
rescue => e
puts e
end
#a song has a name
begin
song = Song.new
song.name = "1901"
assert_equal song.name, "1901"
rescue => e
puts e
end
#a song has an artist
begin
song = Song.new
song.artist = "The Phoenix"
assert_equal song.artist, "The Phoenix"
rescue => e
puts e
end
#a song can be initalized with a name and an artist
begin
song = Song.new("The Phoenix", "1901")
assert_equal song.artist, "The Phoenix"
assert_equal song.name, "1901"
rescue => e
puts e
end
#an artist can be initialized with a name
begin
artist = Artist.new("The Phoenix")
assert_equal artist.name, "The Phoenix"
rescue => e
puts e
end
#an artist has songs stored in an array
begin
artist = Artist.new("The Phoenix")
artist.songs = ["1901"]
assert_equal artist.songs[0], "1901"
rescue => e
puts e
end
#a jukebox can store multiple songs
begin
song1 = Song.new("The Phoenix", "1901")
song2 = Song.new("Tokyo Police Club", "Wait Up")
jukebox = Jukebox.new
jukebox.songs << song1
jukebox.songs << song2
assert_equal jukebox.songs[0], song1
assert_equal jukebox.songs[1], song2
rescue => e
puts e
end
#a jukebox can play a song by name and returns an array with name and artist
begin
song1 = Song.new("The Phoenix", "1901")
song2 = Song.new("Tokyo Police Club", "Wait Up")
jukebox = Jukebox.new
jukebox.songs << song1
jukebox.songs << song2
assert_equal jukebox.play("1901"), ["1901", "The Phoenix"]
rescue => e
puts e
end
#a jukebox can list all of its songs
begin
song1 = Song.new("The Phoenix", "1901")
song2 = Song.new("Tokyo Police Club", "Wait Up")
jukebox = Jukebox.new
jukebox.songs << song1
jukebox.songs << song2
assert_equal jukebox.list, jukebox.songs
rescue => e
puts e
end
#a sessions can be initialized with a jukebox
begin
jukebox = Jukebox.new
session = Session.new(jukebox)
assert_equal session.jukebox, jukebox
rescue => e
puts e
end
songs_list = [
"The Phoenix - 1901",
"Tokyo Police Club - Wait Up",
"Sufjan Stevens - Too Much",
"The Naked and the Famous - You",
"Tiga - (Far From) Home",
"The Cults - Abducted",
"The Phoenix - Consolation Prizes"
]
jukebox = Jukebox.new
songs_list.each do |song|
jukebox.songs << Song.new(song.split(" - ")[0], song.split(" - ")[1
end
session = Session.new(jukebox)
while session.status
session.prompt
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment