Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # interface.rb
- # Pseudo-code
- # 1. Print Welcome
- # 2. Define your store (with a bunch of items)
- # 2.1 What should we use ? Array / Hash
- # 2.3 We use an Hash with the products
- # 2.4 Each produtct is itself an hash with some properties:
- # 2.5 price, description, quantity
- require_relative "cart"
- STORE = {
- #key => value
- "potatoe" => { price: 2, quantity: 20 },
- "banana" => { price: 1, quantity: 10 },
- "carrot" => { price: 3, quantity: 15 }
- }
- STORE.each do |product_name, properties|
- puts "#{product_name} has #{properties[:quantity]} items- Price: #{properties[:price]}"
- end
- CART = {
- # key => #value
- # product_name => quantity
- # product1 => 10
- }
- # 3. Get items from the user (shopping step)
- input = nil
- until input == "n"
- puts "What do you want to buy?"
- product = gets.chomp
- puts "How many?"
- quantity = gets.chomp.to_i
- # 4. Add items to the user Cart
- # 4.1 Check if the product is available on store
- if STORE.key?(product) && STORE[product][:quantity] >= quantity
- # 4.2 If it is - Subtract from the available quantity
- STORE[product][:quantity] = STORE[product][:quantity] - quantity
- # 4.3 And add to the cart
- if CART.key?(product)
- CART[product] = CART[product] + quantity
- # is the same as
- # CART[product] += quantity
- else
- CART[product] = quantity
- end
- else
- # 4.3 Otherwise lets say, sorry not enough stock
- puts "Sorry not enough #{product} available!"
- end
- # add_to_cart(STORE, CART, product, quantity)
- puts "Do you want buy more stuff?"
- input = gets.chomp
- end
- #5. Print the bill
- total = 0
- CART.each do |product, quantity|
- product_price = STORE[product][:price] * quantity
- total += product_price
- puts "You've bought #{product} for #{product_price}€."
- end
- puts "Youre total bill is #{total}€."
- # 5. Print the bill (cashier step)
- #total = 0
- #CART.each do |key, value|
- # product_price = STORE[key][:price] * value
- # total += product_price
- # puts "Your cart has #{value} items of #{key} - It costs #{product_price}€."
- #end
- #puts "Total bill is #{total}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement