Last active
August 29, 2015 14:17
-
-
Save olistik/cd3673ee0901593f1d96 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
require 'json' | |
UNITS = { | |
'unit_a' => 'kg.25', | |
'unit_b' => 'kg.15', | |
# ... | |
'unit_c' => 'kg.5', | |
} | |
STORE = { | |
'department_a' => { | |
'cemento' => { | |
price: 4.0, | |
unit: UNITS['unit_a'] | |
}, | |
'cemento_bianco' => { | |
price: 12.0 | |
unit: UNITS['unit_a'] | |
} | |
}, | |
# ... | |
'department_b' => { | |
# ... | |
} | |
} | |
def load_or_create_database(filename:) | |
if File.exists?(filename) | |
JSON.parse(File.read(filename)) | |
else | |
quantities = {} | |
STORE.each do |department, products| | |
quantities[department] ||= {} | |
products.each do |product_name| | |
quantities[department][product_name] = 100 | |
end | |
end | |
File.write(filename, quantities.to_json) | |
quantities | |
end | |
end | |
quantities = load_or_create_database(filename: 'store.json') | |
def product_price(department:, product_name:, requested_quantity:) | |
amount = STORE[department][product_name][:price] * requested_quantity | |
"$ #{amount}" | |
end | |
product_price(department: 'department_a', product_name: 'cemento_bianco', requested_quantity: 23) # => "$ 276" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment