Added some functionality to this, see what else you can add!


#1



I went a little further with this exercise and added some extra functionality to it. Feel free to tweak this and add some more. Maybe add a loop that keeps the program running until the user inputs "exit" or give the user the ability to switch case statements after the initial choice.

Any comments or thoughts would be appreciated, let's see what you got!


movies = {
    Clerks: 10,
}

puts "What would you like to do?"
puts "-- 'add' to add a new title."
puts "-- 'update' to update an existing title."
puts "-- 'display' to see a list of existing titles."
puts "-- 'delete' to delete an existing title."
choice = gets.chomp.downcase

case choice.downcase
when "add"
    puts "Enter title."
    title = gets.chomp.to_sym.capitalize
        if movies[title] == nil
            movies[title] = title
        else
            while movies[title].nil? == false
                puts "That movie has already been added! Please enter title."
                title = gets.chomp.to_sym.capitalize
            end
        end 
    puts "What rating would you like to give this title? (1-10)"
    rating = gets.chomp.to_i
        if rating > 10
            rating = 10
        elsif rating < 1
            rating = 1
        else
            rating
        end
    puts "#{title} has been added with the rating of #{rating}"
    movies[title] = rating
when "update"
    puts "Enter the title you wish to update."
    title = gets.chomp.to_sym.capitalize
        while movies[title].nil?
            puts "That movie has not been added yet, would you like to add it? y or n?"
            update_new = gets.chomp.downcase
            if update_new == "y"
                movies[title] = title
            else
                puts "Enter the title you wish to update."
                title = gets.chomp.to_sym.capitalize
            end
        end
        if !movies[title].nil?
            puts "What would you like to rate this title?"
            rating = gets.chomp
            movies[title.to_sym] = rating.to_i
            puts "#{title} has been updated with new rating of #{rating}."
        end
when "display"
    movies.each do |x,y|
        puts "#{x}: #{y}"
    end
when "delete"
    puts "Enter the title you wish to delete."
    title = gets.chomp.to_sym.capitalize
    while movies[title].nil?
        puts "That movie does not exist in this system."
        puts "Please enter the title you wish to delete."
        title = gets.chomp.to_sym.capitalize
    end
    puts "Are you sure you want to delte #{title}?"
    confirm = gets.chomp.downcase
    if confirm == "y"
        movies.delete(title.to_sym)
        puts "#{title} has been removed."
    end
else
    puts "Error!"
end


#2

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