Threesuns

Email the Instructor

Contact

All Semesters

Semesters

All Courses

Courses

2015Spring

01.12.15 - 05.10.15

Ruby

CSYS2853

Tuesday Thursday
05:30 PM - 06:50 PM

Store Object

Lab04

Objective: To practice writing Ruby classes

pastdue

Submit Your Solution

Lab04

Request a Copy of your Solution

Lab04

Requirements

Store class written and store object can be created10 pts
Store object contains products hash10 pts
Products hash correctly associated products with prices10 pts
add_product method works correctly10 pts
Store objects contains cart array10 pts
add_to_cart method works correctly10 pts
Test code executes and produces specified results10 pts
cart method returns comma separated list of carted products10 pts
cart_total method correctly calculates cart total10 pts
Store object behaves in a reasonable manner with any other data10 pts
Total100 pts

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