Goal: To complete the term project by coding Domain Logic to handle events in the recipe application.
Tutorial
In Ex7c, the Model-View-Controller ex7c.pdf showed Domain Logic and View as part of the Document (Delegate) View. The View is the presentation layer and display fields, buttons, etc. Domain Logic is the the business code that knows how to process the events (e.g. the "pushing" of a button) that occur in the View. Both interact with Domain Objects. The View mainly displays information about Domain Objects and the Domain Logic mainly manipulates Domain Objects.
In Ex7c, we saw planner/gui/PlannerApplet.java had 3 main ways of introducing Domain Logic into the code:
// this is an inner class, specific for one type of action // the code would need one inner class per each possible action // this shows the Domain Logic and GUI as tightly "coupled" // the advantage to any of these approaches is that Domain Logic // can "see" the widgets and operate on them, e.g. "ta", the TextArea class MenuBookListener implements ActionListener { // knows how to respond public void actionPerformed(ActionEvent e) { // must have this method ta.append(mb.toString()); // append the full MenuBook to the ta } } MenuBookListener mbl = new MenuBookListener(); // instantiate this inner class showMenuBook.addActionListener(mbl); // identify this instance as the listener for the button showMenuBook.addActionListener(new MenuBookListener()); // instead: just new it right here showMenuBook.addActionListener( // new way: anonymous inner class new ActionListener() { // new one without a name public void actionPerformed(ActionEvent e) { // and give the same code as above ta.append(mb.toString()); } } ); ... // PlannerApplet implements ActionListener // Here is the required method public void actionPerformed(ActionEvent e) { String cmd = (String)e.getActionCommand(); if (cmd.equals("Show Book")) // what event just occurred? ta.append(mb.toString()); // display entire MenuBook to ta }
Your application will follow the last way: code Domain Logic to process the events within the context of PlannerApplet's actionPerformed().
Before coding, you should review the following classes and understand these methods in detail:
Class | Method | API |
JTextArea | append() | API |
JTextField | getText() | API |
JRadioButton | isSelected() | API |
JComboBox | getSelectedItem() | API |
setSelectedItem() | ||
insertItemAt() | ||
removeItem() | ||
HashMap | get() | API |
remove() | ||
FoodComposite | add () | ex7b.pdf |
remove() |
Assignment
Recall from Ex7c, the 10 Buttons and associated Actions:
Button | Action |
Show Book | Display the entire MenuBook to the TextArea |
New Menu | TextField specifies new menu name, create Menu, add to ComboBox |
RadioButton specifies BREAKFAST, LUNCH, DINNER | |
Remove Menu | ComboBox specifies old menu name, remove Menu from MenuBook, HashMap, ComboBox |
Show Menu | ComboBox specifies menu name, get from HashMap, display to TextArea |
New Recipe | similar to New Menu |
Show Recipe | similar to Show Menu |
Add Recipe | ComboBox specifies recipe name, get from HashMap |
ComboBox specifies menu name, get from HashMap, add Recipe to Menu | |
Remove Recipe | similar to Remove Menu but remove from specified Menu |
Add Ingredient | ComboBoxes specify Recipe, Item, Unit, get from respective HashMaps |
TextField specifies quantity of Unit, convert to Double | |
add new Ingredient to Recipe | |
Add Step | ComboBox specifies recipe name, get from HashMap |
TextField specifies Step text, add new Step to Recipe |
Implement these Actions inside of ActionPerformed(). To help you get started, here is pseudocode for New Menu:
Get the name of the new menu, and insert it at the front of the JComboBox. Force the front of the JComboBox to be displayed. Based on the JRadioButtons, determine which MenuType is selected. Create a new FoodMenu and add it to the MenuBook.
and for Remove Menu:
Get the name of the menu from the JComboBox. Get the menu object from the HashMap using the name as the key. Remove the menu object from the MenuBook. Remove the name from the HashMap. Remove the name from the JComboBox.
Code Domain Logic for all of the Buttons. You might want to do the Show buttons first, and then perhaps followed one-by-one according to the Validation Test plan below. This is an incremental code-and-test strategy.
Compile, test, and javadoc all packages:
cd ex7 rm planner/*.class rm planner/gui/*.class rm planner/gui/domain/*.class javac planner/Planner.java java planner/Planner javadoc *.java gui/*.java gui/domain/*.java
Here is the current javadoc: planner/allclasses-noframe.html
Validation Test
Run again and do the following Validation Test exactly. Before starting, be sure that your planner/gui/domain/MenuBook.java has only the original Birthday Dinner of 950 calories. Also be sure that your MenuBook's toString() includes getCalories(). Then do the math: does your Validation Test come up with the correct number of total calories in the final Menu Book?
Afterwards, copy from your TextArea and paste the result into your planner/Planner.java (and comment out).
Test # | Button | Data |
1 | New Menu | M1 [Lunch] |
2 | New Menu | M2 [Breakfast] |
3 | Remove Menu | M2 from Book [and from HashMap, ComboBox] |
4 | New Recipe | R1 |
5 | New Recipe | R2 |
6 | Add Recipe | R1 to M1 |
7 | Add Recipe | R2 to M1 |
8 | Remove Recipe | R2 from M1 [and from HashMap, ComboBox] |
9 | Add Step | S1 to R1 |
10 | Add Ingredient | American Cheese to R1 [4 slices] |
11 | Show Recipe | R1 |
12 | Show Menu | M1 |
13 | Show Book |
Turn-In: Entire directory: planner