Assignment 10: Events, State, and Transitions

###
Assignment 10: Events, State, and Transitions

Before you tackle the homework, remind yourself of our General Advice, Advice on Homeworks, and Grading Guidelines. Above all, keep your work neat and honest.

1. (6pts) You are the chief programmer for J-mart's inventory database. The database consists of inventory records, which contain the name of an item, its current price, and the number of copies in stock. The placeholder `current-inventory` stands for a list of these inventory records, representing all items for sale. Choose a data representation and make up an example!

   Write the function `new-item!`, which consumes the name of an item, its price, and the number of copies that arrived from the warehouse. It extends the `current-inventory` appropriately. That is, if the item is already in the database, it modifies the corresponding record; otherwise it adds an appropriate record.

   Write the function `inflate!`, which consumes a percentage and increases the price of each item in `current-inventory` accordingly.

   Write the function `discontinued!`, which consumes the name of an item and removes the item from the list.

2. (4pts) Implement a simple three-colored traffic light. Use the library `draw-lib.ss` to draw the circles and disks that represent the three bulbs in their on and off states.

   The implementation requires two functions:

   1. `init!`, which initializes the light to `GREEN` and creates the corresponding display;
   2. `next!`, which switches the traffic light from its current state into the next one. In particular, the sequence

          (begin
            (next!)
            (next!)
            (next!))

      should cycle the traffic light through all three states back to whatever was its intial state.
      A change of state should re-paint the displayed light, not create a new display.

   Here are the three states:

   They were produced with the following constants:

         ;; ---------------------------------------------------------
         ;; Basic constants:
         (define WIDTH  50)
         (define HIGHT 160)
         (define RADIUS 20)

         (define DELTA 10)

         (define X (quotient WIDTH 2))

         ;; off-set : num -> num
         (define (off-set y) (+ y DELTA (* 2 RADIUS)))

         (define Y:RED (off-set (- RADIUS)))
         (define Y:YEL (off-set Y:RED))
         (define Y:GRN (off-set Y:YEL))

         (define-struct bulb (center color))
         ;; A bulb is (make-bulb posn color)

         (define RED:BULB (make-bulb (make-posn X Y:RED) RED))
         (define YEL:BULB (make-bulb (make-posn X Y:YEL) YELLOW))
         (define GRN:BULB (make-bulb (make-posn X Y:GRN) GREEN))

Matthias Felleisen | This page was generated on Fri Mar 5 09:05:54 CST 1999.