To practice writing Ruby classes
Instructions
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:
- "eggs" = 1.5
- "bread" = 3.0
- "granola cereal" = 3.4
- "coffee" = 2.3
- "pie" = 4.7
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