dannyk
dannyk

Reputation: 259

Collecting and Storing User Input with Ruby

I'm working on a program that asks a user for input regarding recent financial transactions and would like some help on storing the user input into an empty array and being able to display that information when called upon. Right now the empty array (it's actually a hash at the moment) doesn't receive anything.

I'm incredibly green when it comes to programming and Ruby in general and would love any and all help!

###I want transactions (below) to house user_input so that when a user
###chooses 'display' it will return what the user has input.
transactions = {}

puts "What would you like to do?"
puts "-- Type 'add' to add a transaction."
puts "-- Type 'update' to update a transaction."
puts "-- Type 'display' to display all transactions."
puts "-- Type 'delete' to delete a transaction."

choice = gets.chomp.downcase
case choice
when 'add'
  puts "What transaction would you like to add?"
  user_input = gets.chomp
  if transactions[user_input.to_sym].nil?
    puts "What's the rating? (Type a number 0 to 4.)"
    rating = gets.chomp
    transactions[user_input.to_sym] = rating.to_i
    puts "#{user_input} has been added with a rating of #{rating}."
  else
    puts "That transaction already exists! Its rating is #{transactions[title.to_sym]}."
  end
when 'update'
  puts "What transaction do you want to update?"
  user_input = gets.chomp
  if transaction[title.to_sym].nil?
    puts "Transaction not found!"
  else
    puts "What's the new rating? (Type a number 0 to 4.)"
    rating = gets.chomp
    transactions[title.to_sym] = rating.to_i
    puts "#{user_input} has been updated with new rating of #{rating}."
  end
when 'display'
  transactions.each do |receipt, rating| ###figure out how to change this so that the
    puts "#{receipt}: #{rating}"         ###user's input is returned
  end
when 'delete'
  puts "What transaction do you want to delete?"
  user_input = gets.chomp
  if transactions[user_input.to_sym].nil?
    puts "Transaction not found!"
  else
    transactions.delete(user_input.to_sym)
    puts "#{user_input} has been removed."
  end
else
  puts "Sorry, I didn't understand you."
end

Upvotes: 1

Views: 3174

Answers (2)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

Setting aside how you get user input, you may find that you're simply using the wrong type of object to store your data. For example, consider the following rewrite using an Array of Struct objects.

require 'pp'

class Transaction < Struct.new(:title, :rating)
end

transactions = []
transactions.push(Transaction.new 'example1', 1)
transactions.push(Transaction.new 'example2', 3.5)

pp transactions

t = transactions.find { |s| s.title == 'example1' }
t.rating = 4

pp transactions

You could extend Transaction to solicit user input directly, sanitize member values, or use something like highline to create a menu of some sort. In my opinion, the details of this don't really matter as much as being able to set and retrieve members of your Struct in a sensible and consistent fashion, but your mileage may vary.

Upvotes: 1

cupholdR
cupholdR

Reputation: 75

Here is some info on this http://gistpages.com/2013/07/28/ruby_arrays_insert_append_length_index_remove

You can use something like

transaction = Array.new
user_input = gets.chomp
transaction.insert(user_input)

Upvotes: 0

Related Questions