/* Terabite.java 1 a. Why does ControlPanel require the parameter? b. What do the setLayout methods do, and why are the new GridLayout() calls different? c. Write a doc-comment, /** ... , above each class explaining its purpose. 2 Compile, run, and critique all the problems you see. List the top four problems below: i. ii. iii. iv. 3 Fix each of the four errors you identified in Q2. 4 Creating a local JPanel, put the "Place order" button inside of it, and add the panel to the ControlPanel. I.e., the button is in a panel is in a panel. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.TreeMap; public class Terabite { public static void main(String args[]) { BusinessLogic bl = new BusinessLogic(); ControlPanel gb = new ControlPanel(bl); // Q1a JFrame jf = new JFrame("Tera-Bite Order Entry"); jf.add(gb); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(400,400); jf.setVisible(true); } } //--------------------------------------------------------- class BusinessLogic { String sandwichNames[] = {"Hamburg", "Cheeseburg", "1/4-pound", "1/2-pound","Side O' Beef"}; double sandwichPrice[] = { 0.99, 1.09, 1.19, 1.49, 2.99 }; String sideNames[] = {"Fries", "Chips", "Gummy Bears"}; double sidePrice[] = { 0.59, 0.89, 0.49 }; String drinkNames[] = {"Cola", "Root Beer", "Coffee", "Milk", "Water"}; double drinkPrice[] = { 0.99, 0.99, 1.29, 1.99, 0.49 }; double computePrice(int sandwichIdx, int sideIdx,int drinkIdx, boolean superSz) { double p = (sandwichIdx<0? 0.00: sandwichPrice[sandwichIdx]); p += (sideIdx<0? 0.00: sidePrice[sideIdx]); p += (drinkIdx<0? 0.00: drinkPrice[drinkIdx]); return (superSz? 2*p: p); } } //--------------------------------------------------------- class ControlPanel extends JPanel { JTextArea jta; ChoicesPanel choices; BusinessLogic bl; public ControlPanel(BusinessLogic bl) // Q1a { this.bl = bl; setBackground(Color.black); setLayout(new GridLayout(4,1, 3, 2)); // Q1b add( new LabelPanel() ); choices = new ChoicesPanel(bl); add( choices ); JButton order = new JButton("Place order"); order.addActionListener( new OrderListener() ); add(order); jta = new JTextArea(3,20); // row, col jta.setFont(new Font("Helvetica",Font.BOLD,16)); jta.setText("Your total today is:"); add( jta ); } class OrderListener implements ActionListener { // Place order button has been pressed public void actionPerformed(ActionEvent e) { double total = bl.computePrice( choices.getSandwichChoice(), choices.getSideChoice(), choices.getDrinkChoice(), choices.getSuper() ); jta.append("\n$ " + total ); } } } //--------------------------------------------------------- class LabelPanel extends JPanel { public LabelPanel() { JLabel title = new JLabel("Joe's Gigo BITES"); title.setFont( new Font("Helvetica",Font.BOLD,36) ); title.setForeground( Color.MAGENTA ); add(title); } } //--------------------------------------------------------- class ChoicesPanel extends JPanel { JComboBox sandwiches, sides, drinks; JCheckBox superSize = new JCheckBox("Super Size?"); String labelNames[] = {"Sandwiches","Drinks","Side Orders"}; JLabel labels[]; Font myFont = new Font("Helvetica",Font.BOLD,16); public ChoicesPanel(BusinessLogic bl) { sandwiches = new JComboBox(bl.sandwichNames); sides = new JComboBox(bl.sideNames); drinks = new JComboBox(bl.drinkNames); labels = new JLabel[ labelNames.length ]; for(int i=0; i