My solution with loops


#1



A Night at the Movies: Nice Work!


Here's my solution with loops for update and delete if the movie isn't in the database. Could use an outer loop as well, and maybe another for the 'add' function.


movies = {
    grit: 5   
}
puts "Enter add, update, display, or delete."
choice = gets.chomp.downcase

case choice
    when 'add'
        puts "Enter movie title: "
        title = gets.chomp
        if movies[title.to_sym].nil?
            puts "Enter the rating of #{title}:"
            rating = gets.chomp
            movies[title.to_sym] = rating
            puts "Added: '#{title}' with rating: #{rating}"
        else
            puts "Movie already in database."
        end
    when 'update'
        puts "Enter movie title"
        title = gets.chomp
        if movies[title.to_sym].nil?
            puts "Error. '#{title}' not found in database."
        else
            puts "Enter new rating for '#{title}' :"
            new_rating = gets.chomp
            if movies[title.to_sym] != new_rating.to_i
                 movies[title.to_sym] = new_rating.to_i
            else
                repeat = true
                while (repeat)
                    puts "Rating already exists, try again: "
                    new_rating = gets.chomp
                    if movies[title.to_sym] != new_rating.to_i
                        repeat = false
                        movies[title.to_sym] = new_rating.to_i
                    end
                end
            end
            puts "'#{title}' has been udpated with a rating of #{movies[title.to_sym]}"
       end
    when 'display'
        movies.each { |movie, rating|
        puts "#{movie}: #{rating}"
        }
    when 'delete'
        puts"Enter movie title to remove: "
        user_input = gets.chomp
        if movies[user_input.to_sym].nil? == false
           movies.delete(user_input.to_sym)
           puts "#{user_input} has been deleted"
        else
            repeat = true
            while (repeat)
                puts "Error. '#{user_input}' not found, try again: "
                user_input = gets.chomp
                if movies[user_input.to_sym].nil? == false
                    repeat = false
                end
            end
            puts "#{user_input} has been deleted"
        end
    else
        puts "Error!"
end

puts movies


#2

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