This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Introduction to Programming with Ruby

Week 2 Exercise

Andy Lindeman's picture
Fri, 2011-05-06 05:14

Your elderly uncle owns and runs a pawn shop. However, he is finding it difficult to keep track of items and their prices in his old age. He has hired you to write a simple program for him that will help him run his store. He wants the computer to be able to track his inventory, and calculate final prices of different orders for him.

Currently, there are 8 items in the store. They are:

  • A 1977 Apple II: $80
  • A baseball signed by Babe Ruth: $50
  • A copy of the 1974 game Oregon Trail: $10
  • A Louisville Slugger owned by Babe Ruth: $500
  • Complete set of Superman comic books: $100
  • An original IBM Model M keyboard: $15
  • A paper copy of Chris Pine's Learn to Program book: $20
  • A leaked pre-release iPhone 5: $5000

When a customer first interfaces with the program, it should list which items are in stock and wait for a command. Next, the customer will issue a command to add an item to his or her virtual shopping cart. At some point, the customer will issue a command to finish the transaction; at this point, a receipt should be shown with each item the customer is purchasing, its price, and a grand total.

Exactly how the interface and commands operate is up to you, though you'll definitely want to use something like the gets method introduced in LtP Chapter 4 to receive the input from the user. 

After a customer completes a transaction, the items should be removed from inventory and the cycle should start over (i.e., a revised list of items is presented and the next customer can select which to purchase).

BONUS: Your uncle's store also runs various promotions which need to be taken into account:

  • If a customer purchases the Apple II and the Oregon Trail game, he or she receives a 10% discount on those two items.
  • If a customer buys three or more items, they will get a 5% discount off their total price.

The discounts can overlap.