A night at the movies


#1



10. Nice Work


This is a questions about functionality. The code below passes the test, but it still has some bugs.

1) Update: if I write 'asdfdfas' it will never display "The movie is not in the hash.", always "What's the movie's rating?"

2) Delete (else): It always displays 'The movie is not in our database', doesn't matter if the movie exists or not.

3) If you have a comment on the style or optimization of the code, please let me know.
Thanks!

movies = {Die_hard: 4, WWII: 2}

puts "Choose an option:" 
puts "1. ADD a movie"
puts "2. UPDATE an entry"
puts "3. DISPLAY movies"
puts "4. DELETE an entry\n"

choice = gets.chomp.downcase 

case choice
when "add"
    puts "What title?"
        title = gets.chomp
    puts "What's the movie rating?"
        rating = gets.chomp
        
    if movies[title.to_sym] != nil
        puts 'The movie is already there'
    else
        movies[title.to_sym] = rating.to_i
    puts "The movie #{title} and #{rating} have been added"    
    end
        
when "update"
    puts "What's the title?"
    title =gets.chomp
    
    if movies[title] = nil
        puts "The movie is not in the hash."
    else
        puts "What's the movie's rating?"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
    end
    
when 'display'
    movies.each do |title, rating |
        puts "#{title}: #{rating}"
    end
    
else 
    puts "Which movie would you like to delete?"
    title = gets.chomp
    if title != movies
        puts 'The movie is not in our database'
    else
        movies.delete(title)
    end
end


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.