Assignment 7c
Home ] Up ] Assignment 7a ] Assignment 7b ] [ Assignment 7c ]

 

 

A Virtual Supermarket

Now write a Java program that uses several of these Shuttle components -- a "Virtual Supermarket".

Here is how it should look:

As you can see, it contains two shuttle controls, labeled "Fresh Vegetables and Fruits" and "Groceries", plus a "Shopping Cart" list on the right.

Hint: In order to get the alignment right, you will most likely have to use various components nested within panels, within panels, each using an appropriate choice of layout manager. Experiment!  I expect this learning process to take some time, but I think you'll find it worth the time.

As you move items in each shuttle control to the right, the item should be replicated in the shopping cart list, so you get a "roll-up" of all the items selected from each shuttle. Similarly, if you move items back to the left in each shuttle control, the corresponding entry should be removed from the shopping cart list.

 

Once you have the layout done, you've done most of the work! All you have to do, then, is supply the appropriate list of items to be added to the left hand side of each shuttle control, and then connect the shuttle control behavior up to the main class, so that you can "intercept" when items are added to, or removed from, the right list of each shuttle control. That, given the functionality you've added to the shuttle class as specified above, I think you'll find it pretty easy to hook things up!

The virtual supermarket should use the shuttle events that are emitted by the shuttles as a result of user actions on those shuttles, in order to populate or to depopulate the Shopping Cart.

Here's what to do for the main class for this application, SuperMarket:

  • Make it extend JFrame.
  • Choose a class which will implement ShuttleListener which will listen for ShuttleEvents emitted from each Shuttle.
  • SuperMarket will have a main entry point for the class, so you can run it as an application, or you can choose to make it an applet instead (or both).
  • The default constructor, SuperMarket(), should do most of the work in terms of setting up the layout for the appropriate set of components. 
    It will likely take some time to get the layout correct, and you will probably use a bunch of different layout managers in nested containers. I found that it simplified my code to use a number of inner classes, one for each panel in the interface;  that made it easy to isolate the code for a panel, and know where things were happening.  Check that your frame behaves reasonably when you resize it.
    In addition, you'll need to populate the left lists of the two shuttle components with the appropriate sets of vegetable/fruit or grocery items. Supply the two sets of items in the SuperMarket class.  
    Finally, it should register a ShuttleListener for both shuttle components.
  • In your shuttle listener class, if you are implementing the ShuttleListener interface directly, you must implement all of the four methods specified in that interface. Two of them will do nothing; the two that you truly implement will cause any items added to or removed from the right lists of the two shuttle controls to cause the same items to be entered into, or removed from, the "shopping cart" list.  
  • Consider using a class that extends ShuttleAdapter for this purpose.  You can't do this if your JFrame or JPanel class is the listener, but If you use a separate class that extends ShuttleAdapter class, then you only have to implement two methods; ShuttleAdapter will already implement the remaining methods (which do nothing).

Once you've implemented the basic stuff, like the layout of the above window, the work of connecting things together should be quite simple.

Note: If you implemented Part B correctly, you should need to make zero changes to your Shuttle classes.  You should only be writing the necessary class(es) to use those Shuttle classes.  I don't want to see any new versions of the Shuttle classes supplied with Part C.  If you can't get your Part B classes to work in part C, go back to Part B and fix them, and only then return to working on Part C.

 

This page was last modified on 02 October, 2007