Skip to content

Instantly share code, notes, and snippets.

@Lara-zz
Created June 1, 2009 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Lara-zz/121863 to your computer and use it in GitHub Desktop.
Save Lara-zz/121863 to your computer and use it in GitHub Desktop.
product = {827 => ["ARM",349.99],
199 => ["LEG", 224.75],
776 => ["FIRSTBORN",1499.95],
222 => ["LIFESAVINGS",49.99],
811 => ["RETIREMENT",49.99]}
choice = 0
if choice <= 7
puts
print "1. Add a product
2. Update a product
3. Delete a product
4. View all products
5. View names that start with a letter you choose
6. View highest-priced product and lowest-priced product
7. View sum of all product prices
8. Quit
Choose an option: "
$stdout.flush
choice = gets.to_i
if choice == 1
while product.has_key?(key)
key = rand(900) + 100
end
print "Enter a product name: "
$stdout.flush
name = gets.chomp.upcase
print "Enter the product price using a decimal (as in 44.99), with no dollar sign: "
$stdout.flush
price = gets.chomp.to_f
puts product[key] = ["#{name}", price]
puts "You have entered a new product called #{name} with an assigned key of #{key}."
puts product.inspect.upcase
end
if choice == 2
product.each{ |key, value|
puts "#{key} - #{value[0]}, #{value[1]}"
}
print "Enter the key number of the product you wish to update: "
$stdout.flush
keynumber = gets.chomp.upcase
print "Enter 9 to change the product name, or 10 to change the price: "
$stdout.flush
entry = gets.chomp.to_i
if entry == 9
if product.has_key?(keynumber)
puts product[key, value]
print "Enter the new product name: "
$stdout.flush
new_name = gets.chomp.upcase
puts new_name
product.delete(keynumber)
puts product.inspect.upcase
elsif ! product.has_key?(keynumber)
print "This product is not in the list.\n"
end
end
if entry == 10
if product.has_value?(name)
print "Enter the new product price: "
$stdout.flush
new_price = gets.chomp.to_f
product[ key ][1].upcase = new_price
puts product.inspect.upcase
end
end
end
if choice == 3
print "Enter the name of the product you want to delete: "
$stdout.flush
name = gets.chomp.upcase
findkey = product.index("name")
product.delete(key)
puts product.inspect.upcase
if ! product.has_key?[name, price]
print "This product is not in the list.\n"
end
end
if choice == 4
product.each{ |key, value|
puts "#{key} - #{value[0]}, #{value[1]}"
}
end
if choice == 5
print "Type a letter to display all the products starting with that letter: "
$stdout.flush
letter = gets.chomp.upcase
product.each do |key, value|
if value[0].slice(0,1) == letter
puts "#{key} - #{value[0]}, #{value[1]}"
end
end
if choice == 6
product.each { |key, value| }
low_price = product.sort.first
position = product.index(value[1])
product[value[position]]
print "$#{low_price}"
high_price = product.sort.last
# position = product.index(value[1])
product[key, value[1]]
print "$#{high_price}"
end
if choice == 7
sum = 0
product.each { |key, value| }
puts "#{key[1]}"
sum += [value[1]]
print "The sum of all prices is #{sum}."
end
if choice == 8
exit
end
if choice > 8
puts "That\'s not one of the choices."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment