Skip to content

Instantly share code, notes, and snippets.

@Lara-zz
Forked from mattetti/Hash.rb
Created June 2, 2009 00:21
Show Gist options
  • Save Lara-zz/121905 to your computer and use it in GitHub Desktop.
Save Lara-zz/121905 to your computer and use it in GitHub Desktop.
def products
@products ||= { 827 => {'product' => "ARM", 'price' => 349.99},
199 => {'product' => "LEG", 'price' => 224.75},
776 => {'product' => "FIRSTBORN", 'price' => 1499.95},
222 => {'product' => "LIFESAVINGS", 'price' => 49.99},
811 => {'product' => "RETIREMENT", 'price' => 49.99}
}
end
def products
@products ||= { 827 => ["ARM",349.99],
199 => ["LEG", 224.75],
776 => ["FIRSTBORN",1499.95],
222 => ["LIFESAVINGS",49.99],
811 => ["RETIREMENT",49.99]}
end
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
def generate_key
key = rand(998) + 1
while products.has_key?(key)
key = rand(998) + 1
end
end
def add_product
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
key = generate_key
puts products[key] = ["#{name}", price]
puts "You have entered a new product called #{name} with an assigned key of #{key}."
puts products.inspect.upcase
end
case choice
when 1
add_product
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment