-
-
Save mattetti/121875 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
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