Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. # interface.rb
  2.  
  3. # Pseudo-code
  4. # 1. Print Welcome
  5. # 2. Define your store (with a bunch of items)
  6. # 2.1 What should we use ? Array / Hash
  7. # 2.3 We use an Hash with the products
  8. # 2.4 Each produtct is itself an hash with some properties:
  9. # 2.5 price, description, quantity
  10.  
  11. require_relative "cart"
  12.  
  13. STORE = {
  14. #key => value
  15. "potatoe" => { price: 2, quantity: 20 },
  16. "banana" => { price: 1, quantity: 10 },
  17. "carrot" => { price: 3, quantity: 15 }
  18. }
  19.  
  20. STORE.each do |product_name, properties|
  21. puts "#{product_name} has #{properties[:quantity]} items- Price: #{properties[:price]}"
  22. end
  23.  
  24. CART = {
  25. # key => #value
  26. # product_name => quantity
  27. # product1 => 10
  28. }
  29.  
  30. # 3. Get items from the user (shopping step)
  31.  
  32. input = nil
  33. until input == "n"
  34. puts "What do you want to buy?"
  35. product = gets.chomp
  36. puts "How many?"
  37. quantity = gets.chomp.to_i
  38.  
  39. # 4. Add items to the user Cart
  40. # 4.1 Check if the product is available on store
  41. if STORE.key?(product) && STORE[product][:quantity] >= quantity
  42. # 4.2 If it is - Subtract from the available quantity
  43. STORE[product][:quantity] = STORE[product][:quantity] - quantity
  44. # 4.3 And add to the cart
  45. if CART.key?(product)
  46. CART[product] = CART[product] + quantity
  47. # is the same as
  48. # CART[product] += quantity
  49. else
  50. CART[product] = quantity
  51. end
  52. else
  53. # 4.3 Otherwise lets say, sorry not enough stock
  54. puts "Sorry not enough #{product} available!"
  55. end
  56. # add_to_cart(STORE, CART, product, quantity)
  57.  
  58. puts "Do you want buy more stuff?"
  59. input = gets.chomp
  60. end
  61.  
  62.  
  63. #5. Print the bill
  64. total = 0
  65. CART.each do |product, quantity|
  66. product_price = STORE[product][:price] * quantity
  67. total += product_price
  68. puts "You've bought #{product} for #{product_price}€."
  69. end
  70. puts "Youre total bill is #{total}€."
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. # 5. Print the bill (cashier step)
  86. #total = 0
  87. #CART.each do |key, value|
  88. # product_price = STORE[key][:price] * value
  89. # total += product_price
  90. # puts "Your cart has #{value} items of #{key} - It costs #{product_price}€."
  91. #end
  92.  
  93. #puts "Total bill is #{total}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement