Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. # Requirements
  2. #
  3. # Create a new list
  4. # Add an item with a quantity to the list
  5. # Remove an item from the list
  6. # Update quantities for items in your list
  7. # Print the list (Consider how to make it look nice!)
  8.  
  9.  
  10. # Pseudocode
  11. #
  12. # DEFINE 'new_list' which takes no arguments:
  13. # CREATE and RETURN an empty hash
  14. #
  15. # DEFINE 'add', which takes three arguments, item (a string) and quantity (a string), and list (a hash):
  16. # IF item is on list
  17. # PRINT message that item is already on list
  18. # ELSE
  19. # ADD key item to list with value qantity
  20. #
  21. # DEFINE 'remove', which takes two arguments, an item (string), and a list (hash):
  22. # IF item is on list
  23. # DELETE the element of list with key of item
  24. # ELSE
  25. # PRINT message that item isn't actually on the list
  26. #
  27. # DEFINE 'update', which takes three arguments, an item (string), a quantity (a string) and a list(hash):
  28. # IF item is on list
  29. # SET list at item's value to quantity
  30. # ELSE
  31. # PRINT message that the item isn't there to update
  32. #
  33. # DEFINE print_list, which takes one argument, a list (hash)
  34. # ITERATE over each element of list, for each item, quantity
  35. # PRINT item in uppercase, hyphen, quantity
  36.  
  37.  
  38.  
  39. # Initial solution
  40. #
  41. def new_list
  42. Hash.new
  43. end
  44.  
  45. def add(item, quantity, list)
  46. if list[item]
  47. puts "#{item.upcase} is already on your list! Use command `update` to change the quantity."
  48. else
  49. list[item] = quantity
  50. end
  51. end
  52.  
  53. def remove(item, list)
  54. if list[item]
  55. list.delete(item)
  56. else
  57. puts "Whoops! #{item.upcase} wasn't even on your list!"
  58. end
  59. end
  60.  
  61. def update(item, quantity, list)
  62. if list[item]
  63. list[item] = quantity
  64. else
  65. puts "Whoops! #{item.upcase} wasn't on your list! Use `add` to add your item."
  66. end
  67. end
  68.  
  69. def print(list)
  70. list.each do |item, quantity|
  71. puts "#{item.upcase} - #{quantity}"
  72. end
  73. end
  74.  
  75.  
  76. # Tests
  77. my_list = new_list
  78. p my_list == {}
  79.  
  80. add("Carrots", "10", my_list)
  81. p my_list == {"Carrots" => "10"}
  82. add("Carrots", "20", my_list)
  83. p my_list == {"Carrots" => "10"}
  84. add("Apples", "15", my_list)
  85. p my_list == {"Carrots" => "10", "Apples" => "15"}
  86.  
  87. remove("Oranges", my_list)
  88. p my_list == {"Carrots" => "10", "Apples" => "15"}
  89. remove("Apples", my_list)
  90. p my_list == {"Carrots" => "10"}
  91.  
  92. update("Carrots", "20", my_list)
  93. p my_list == {"Carrots" => "20"}
  94. update("Oranges", "20", my_list)
  95. p my_list == {"Carrots" => "20"}
  96.  
  97.  
  98. # Refactored solution w/ commandline input
  99. #
  100. def new_list
  101. {}
  102. end
  103.  
  104. def add(item, quantity, list)
  105. return list[item] = quantity unless list[item]
  106. puts "#{item.upcase} is already on your list! Use command `update` to change the quantity."
  107. end
  108.  
  109. def remove(item, list)
  110. list.delete(item) { |item| puts "Whoops! #{item.upcase} wasn't on your list!" }
  111. end
  112.  
  113. def update(item, quantity, list)
  114. return list[item] = quantity if list[item]
  115. puts "Whoops! #{item.upcase} wasn't on your list! Use `add` to add your item."
  116. end
  117.  
  118. def print(list)
  119. list.each { |item, quantity| puts "#{item.upcase} - #{quantity}" }
  120. end
  121.  
  122. def help
  123. puts <<-helpdocs
  124. The following commands are available. Separate input with a single space, e.g.:
  125. add 10 chips
  126.  
  127. add <number> <item> ADD an item to your list:
  128. remove <item> REMOVE an item from your list
  129. update <item> <quantity> UPDATE an item's quantity on your list
  130.  
  131. help show HELP documentation
  132. quit exit the program
  133. helpdocs
  134. end
  135.  
  136. def run
  137. puts "Hello! Lets get ready to shop!"
  138. list = new_list
  139. puts "Type 'help' to see a list of available commands."
  140. input = ""
  141.  
  142. until input[0] == "quit"
  143. puts
  144. puts "What would you like to do?"
  145. input = gets.chomp.split(" ")
  146. puts
  147.  
  148. case input[0]
  149. when 'add' then add(input[2], input[1], list)
  150. when 'remove' then remove(input[1], list)
  151. when 'update' then update(input[1], input[2], list)
  152. when 'help' then help; next
  153. else puts "Try again..."
  154. end
  155.  
  156. puts; puts "*******************"
  157. puts "This is your list:"
  158. print(list)
  159. puts
  160. end
  161. end
  162.  
  163. run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement