My night at the movies - Final Code with pluses


#1



https://www.codecademy.com/courses/ruby-beginner-en-0i8v1/0/10?curriculum_id=5059f8619189a5000201fbcb


Hi there, just in case it helps to someone, here's my answer to A Night at the Movies.

I've added a few more features. For example, if you rate a movie below or equal to 0 or higher than 5, it won't allow you to do it. Also, the program won't finish until you told it so. The good thing about it is that you can see that it's working properly. For example, if you "add" a new movie and rate it, then you can go to "display" and see it there :slight_smile:


movies = { 
    Star_Wars: 4,
    Gladiator: 3,
    Amelie: 1,
}
loop do
    puts ""
    puts "What do you want to do? Your options are:"
    puts "|–––––––––––––––––––––––––––––––––––|"
    puts "| add, to add a movie               |"
    puts "| update, to update your rating     |"
    puts "| display, to view your catalogue   |"
    puts "| delete, to delete a movie         |"
    puts "| exit, to leave the program        |"
    puts "|___________________________________|"
    puts ""

    choice = gets.chomp.downcase
    if choice == "add" || choice == "update" || choice == "display" || choice == "delete" || choice == "exit"
        case choice
        when "add"
            puts "Please, write the title for your new movie ❤"
            title = gets.chomp.to_sym
            if movies[title].nil?
              puts "And now, please, give it a rate from 1 to 5! "
              rating = gets.chomp.to_i
              while rating > 5 || rating <= 0
                puts "Please, from 1 to 5!"
                rating = gets.chomp.to_i
              end
              movies[title] = rating
              puts "#{title} has been added to your movies with a rating of #{rating}!"
            else
              puts "#{title} already exists!"
              puts "Its rating is #{movies[title]}."
            end
    
        when "update"
            puts "What movie would you like to update?"
            title = gets.chomp.to_sym
            while movies[title].nil?
                puts "That movie isn't in your library :("
                puts "Please, write an existent movie"
                title = gets.chomp.to_sym
            end
            puts "Please, add the new rating"
            rating = gets.chomp.to_i
            while rating > 5 || rating <= 0
                puts "Please, from 1 to 5!"
                rating = gets.chomp.to_i
            end
            movies[title] = rating
        
        when "display"
            movies.each do |movie, rating|
                puts "#{movie}: #{rating}"    
            end
        
        when "delete"
            puts "What movie would you like to delete?"
            title = gets.chomp.to_sym
            while movies[title].nil?
                puts "That movie isn't in your library :("
                puts "Please, write an existent movie"
                title = gets.chomp.to_sym
            end
                movies.delete(title)
                puts "#{title} has been removed from your library."
        when "exit"
            break
                    
        else
        puts "Error!"
        end
    else
        puts "Error!  Please, write add, update, display or delete."
        choice = gets.chomp.downcase
    end
end


#2

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