08.22.16 - 12.18.16
Store class written and store object can be created | 10 pts |
Store object contains products hash | 10 pts |
Products hash correctly associated products with prices | 10 pts |
add_product method works correctly | 10 pts |
Store objects contains cart array | 10 pts |
add_to_cart method works correctly | 10 pts |
Test code executes and produces specified results | 10 pts |
cart method returns comma separated list of carted products | 10 pts |
cart_total method correctly calculates cart total | 10 pts |
Store object behaves in a reasonable manner with any other data | 10 pts |
Total | 100 pts |
For this assignment you will need to write a Ruby class that will allow you to create a "Store" object. The store object should be able to add products to the store and to then add any of those products to a shopping cart.
The store object should contain a hash of products that can be purchased and their associated price. The initial list of products and prices should be the following:
The store should have a add_product method that can be used to add new products to the products hash.
In addition to the products hash, the store should also contain an "cart" array used to keep track of what a person wants to buy.
The store class should contain a add_to_cart method that can be used to add items to the cart array. If a name is passed to the add_to_cart method that doesn't exist in the products hash nothing should be done.
If your store object works as planed you should be able to execute the following code with it and have it perform the actions or produce the output specified.
store = Store.new #should create a new store object
store.add_to_cart "eggs" #should add eggs to the cart
store.add_to_cart "Pie" #should add pie to the cart
store.add_to_cart "Shirt" #should do nothing
store.add_product "shirt", 15 #should add $15 shirt to products hash
store.add_to_cart "Shirt" #should add shirt to cart
store.add_to_cart "bread" #should add bread to cart
puts store.cart # should output: eggs, pie, shirt, bread
printf "$%6.2f", store.cart_total #should output: $ 24.20