Skip to content

Instantly share code, notes, and snippets.

@mattetti
Forked from Lara-zz/new_hash.rb
Created June 2, 2009 06:56
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 mattetti/122115 to your computer and use it in GitHub Desktop.
Save mattetti/122115 to your computer and use it in GitHub Desktop.
def print_out(string)
STDOUT << string
end
def products
@products ||= { 827 => {'name' => "ARM", 'price' => 349.99},
199 => {'name' => "LEG", 'price' => 224.75},
776 => {'name' => "FIRSTBORN", 'price' => 1499.95},
222 => {'name' => "LIFESAVINGS", 'price' => 49.99},
811 => {'name' => "RETIREMENT", 'price' => 49.99}
}
end
puts
print_out "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: "
choice = gets.to_i
def generate_key
key = rand(998) + 1
while products.has_key?(key)
key = rand(998) + 1
end
key
end
def add_product
print_out "Enter a product name: "
# $stdout.flush
name = gets.chomp.upcase
print_out "Enter the product price using a decimal (as in 44.99), with no dollar sign: "
price = gets.chomp.to_f
key = generate_key
products[key] = {'name' => "#{name}", 'price' => price}
puts "You have entered a new product called #{name} with an assigned key of #{key}."
puts products.inspect.upcase
end
def update_product
products.each{ |key, product| puts "#{key} - #{product['name']}, #{product['price']}" }
print_out "Enter the key number of the product you wish to update: "
keynumber = gets.chomp.to_i
until products.has_key?(keynumber)
print_out "This number is not in the list, please try again: "
keynumber = gets.chomp.to_i
end
product = products[keynumber]
print_out "Enter 9 to change the product name, or 10 to change the price: "
entry = gets.chomp.to_i
until (entry == 9 || entry ==10)
print_out "This is not one of the choice, please try again: "
entry = gets.chomp.to_i
end
if entry == 9
print_out "Enter the new product name: "
new_name = gets.chomp.upcase
product['name'] = new_name
puts product.inspect
elsif entry == 10
print_out "Enter the new product price: "
new_price = gets.chomp.to_f
product['price'] = new_price
puts product.inspect
else
print_out "that is not one of the choices"
end
end
case choice
when 1
add_product
when 2
update_product
when 3
print_out "Enter the name of the product you want to delete:  "
name = gets.chomp.upcase
product = products.find{|key, product| product['name'] == name }
if product.nil?
print_out "This product is not in the list.\n"
else
print_out "we have #{products.length} products\n"
key = product.first
products.delete(key)
print_out "and now, we have #{products.length} products\n"
end
when 5
print_out "Type a letter to display all the products starting with that letter:  "
letter = gets.chomp.upcase
products.each do |key, value|
if value['name'].slice(0,1) == letter
print_out("#{key} - #{value['name']}, #{value['price']}\n")
end
end
when 6
flatten_array = products.map do |key, product|
{'key' => key, 'name' => product['name'], 'price' => product['price']}
end
sorted = flatten_array.sort_by{|product| product['price']}
print_out "cheapest: #{sorted.first}"
print_out "most expensive: #{sorted.last}"
when 7
# sum = products.inject(0) do |sum, product|
# sum += product.last['price']
# end
sum = 0
products.each do |key, product|
sum += product['price']
end
print_out sum
else
print_out "booo, try again"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment