movies = {
forrest_gump: 5,
the_beach: 2,
inception: 5
}
puts "Choose an action (add, update, display or delete): "
choice = gets.chomp.downcase
case choice
when "add"
puts "Add a film:"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Rate the movie (1 to 5):"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
else
puts "That movie already exists! Has a rating of #{movies[title.to_sym]}."
end
when "update"
puts "What is the movie you are looking for?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "This movie does not exist."
else
puts "What is your new rating (1 to 5)?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
end
when "display"
movies.each do |movies, rating|
puts "#{movies}: #{rating}"
end
when "delete"
puts "What is the movie you need to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "This movie does not exist."
else
movies.delete(title.to_sym)
end
else
puts"Error!"
end
I have a problem with name of movie with several words. When I add a film that already exists, I should have an error message but I have this :
Choose an action (add, update, display or delete): add Add a film: forrest gump Rate the movie (1 to 5):
There’s not a single solution. And none of them require any programming knowledge, it’s text, you just have to decide what to do.
Change one or the other. Or change it when you read it.
The supposed issue you’re having is that it doesn’t recognize the name of the movie…
It does! You’re putting in two different movie names.
Put more logically - “forrest gump” is not the same as “forrest_gump”… You’re trying to match two strings that are not equal, because of the underscore you’re using. Change one of the two inputs.
The problem is in the hash syntax we can’t put spaces in the symbol. So when a user enters a name of the movie, he enters spaces when the name of the movie has multiple words.
If you move the words around a bit in what you just said, then you did just say that symbols can contain spaces. (Ignoring the first sentence, which is wrong)
google:
ruby space symbol
Or use what you already used, because before comparing, you did create a symbol containing space. You could do the same in your code.
movies = {
"forrest gump": 5,
"the beach": 2,
inception: 5
}
puts "Choose an action (add, update, display or delete): "
choice = gets.chomp.downcase
case choice
when "add"
puts "Add a film:"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Rate the movie (1 to 5):"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
else
puts "That movie already exists! Has a rating of #{movies[title.to_sym]}."
end
when "update"
puts "What is the movie you are looking for?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "This movie does not exist."
else
puts "What is your new rating (1 to 5)?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
end
when "display"
movies.each do |movies, rating|
puts "#{movies}: #{rating}"
end
when "delete"
puts "What is the movie you need to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "This movie does not exist."
else
movies.delete(title.to_sym)
end
else
puts"Error!"
end