/* This is a simple restaurant Menu * */ import javax.swing.*; import java.awt.event.*; import java.awt.*; import javax.swing.tree.*; import javax.swing.event.*; import java.text.DecimalFormat; import java.text.NumberFormat; public class RestaurantMenu extends JFrame { private JPanel mainPanel; private JPanel textPanel; private JPanel treePanel; private JPanel buttonPanel; private JButton calculate; private JButton select; private JButton remove; private JButton removeall; private JButton exit; private JButton fivediscount; private JButton tendiscount; private JTree tree; private DefaultTreeModel model; private JTextArea textArea; private double sum = 0.0; private double taxsum = 0.0; private double discount = 0.0; public RestaurantMenu() { // Sets the title of the Frame setTitle("\u6B22 " + "\u8FCE" + "Dream Island Menu"); // Sets the size of the Frame setSize(1300, 780); // Sets the start location of the window setLocationRelativeTo(null); // Sets the close action of the frame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create the main panel with a border layout mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); // the JTree panel treePanel = new JPanel(); // Create empty tree nodes DefaultMutableTreeNode root, Appetizer, Soup, Chef_Rec, Chop_Suey; // Creating the root directory root = new DefaultMutableTreeNode("Dream Island Menu"); // Creating child nodes Appetizer = new DefaultMutableTreeNode("Appetizer"); Soup = new DefaultMutableTreeNode("Soup"); Chef_Rec = new DefaultMutableTreeNode("Chef's Recommendation"); Chop_Suey = new DefaultMutableTreeNode("Chop Suey"); // Adding child nodes to the parent node root.add(Appetizer); root.add(Soup); root.add(Chef_Rec); root.add(Chop_Suey); // Creating child nodes for Appetizer DefaultMutableTreeNode eggroll = new DefaultMutableTreeNode("Egg Rolls (4).........................$4.25"); DefaultMutableTreeNode fwonton = new DefaultMutableTreeNode("Fried Wonton (10)......................$3.50"); DefaultMutableTreeNode fcheese = new DefaultMutableTreeNode("Fried Cream Cheese Wonton (10) ......$4.25"); DefaultMutableTreeNode fshrimp = new DefaultMutableTreeNode("Fried Shrimp (6).......................$5.50"); DefaultMutableTreeNode paperchick = new DefaultMutableTreeNode("Paper Wrapped Chicken.................$4.95"); DefaultMutableTreeNode spdumpling = new DefaultMutableTreeNode("Steamed/Pan Fried Dumping (8).........$4.50"); DefaultMutableTreeNode chickwings = new DefaultMutableTreeNode("Chicken Wings in spicy Salt* .........$5.95"); DefaultMutableTreeNode hffries = new DefaultMutableTreeNode("House French Fries.......................$3.25"); // Add the nodes to the Appetize Appetizer.add(eggroll); Appetizer.add(fwonton); Appetizer.add(fcheese); Appetizer.add(fshrimp); Appetizer.add(paperchick); Appetizer.add(spdumpling); Appetizer.add(chickwings); Appetizer.add(hffries); // Creating child nodes for Soup DefaultMutableTreeNode eggDsoup = new DefaultMutableTreeNode("Egg Drop Soup...............$4.95"); DefaultMutableTreeNode hsoursoup = new DefaultMutableTreeNode("Hot & Sour Soup* ...............$5.25"); DefaultMutableTreeNode sizzlingsoup = new DefaultMutableTreeNode("Sizzling Rice Soup..........$6.95"); DefaultMutableTreeNode cCornsoup = new DefaultMutableTreeNode("Chicken Corn Soup................$5.50"); DefaultMutableTreeNode seaFoodsoup = new DefaultMutableTreeNode("Seafood Soup..................$6.95"); // Add the ndoes to the Soup Soup.add(eggDsoup); Soup.add(hsoursoup); Soup.add(sizzlingsoup); Soup.add(cCornsoup); Soup.add(seaFoodsoup); // Creating child nodes for chef's recommendation DefaultMutableTreeNode lotus = new DefaultMutableTreeNode("Lotus Root in Fu-Chung Bean Curd Sauce...........$7.95"); DefaultMutableTreeNode squid = new DefaultMutableTreeNode("Squid with Sweet & Sour Mustard Greens*...........$8.95"); DefaultMutableTreeNode chickFrice = new DefaultMutableTreeNode("Chicken Fried Rice with Ginger...............$7.25"); DefaultMutableTreeNode homerib = new DefaultMutableTreeNode("Home Style Cut Spare Ribs.......................$8.25"); DefaultMutableTreeNode fishfillet = new DefaultMutableTreeNode("Fish Fillet with Assorted Sweet Vegetables...$8.95"); DefaultMutableTreeNode bakepork = new DefaultMutableTreeNode("Baked Pork Chop Pasta or Rice.................$6.95"); DefaultMutableTreeNode cWing = new DefaultMutableTreeNode("Chicken Wing / Ribs in Honey Garlic Sauce........$7.95"); DefaultMutableTreeNode fishFlemon = new DefaultMutableTreeNode("Fish Fillet / Chicken in Spicy Lemon Sauce....$8.25"); DefaultMutableTreeNode cFchick = new DefaultMutableTreeNode("Crispy Fried chicken (Half)....................$7.95"); DefaultMutableTreeNode sSeafood = new DefaultMutableTreeNode("Sauteed Seadfood with Macadamia Nut...........$10.25"); DefaultMutableTreeNode sSeaTofu = new DefaultMutableTreeNode("Sauteed Seafood with Tofu....................$9.95"); DefaultMutableTreeNode houseFish = new DefaultMutableTreeNode("House Special Fish Fillet* .................$8.50"); DefaultMutableTreeNode sPineNuts = new DefaultMutableTreeNode("Shrimp with Pine Nuts.......................$9.95"); // Add the nodes to the chef's recommendation Chef_Rec.add(lotus); Chef_Rec.add(squid); Chef_Rec.add(chickFrice); Chef_Rec.add(homerib); Chef_Rec.add(fishfillet); Chef_Rec.add(bakepork); Chef_Rec.add(cWing); Chef_Rec.add(fishFlemon); Chef_Rec.add(cFchick); Chef_Rec.add(sSeafood); Chef_Rec.add(sSeaTofu); Chef_Rec.add(houseFish); Chef_Rec.add(sPineNuts); // Creating child nodes for Chop Suey DefaultMutableTreeNode porkSuey = new DefaultMutableTreeNode("Pork Chop Suey..............$7.50"); DefaultMutableTreeNode beefSuey = new DefaultMutableTreeNode("Beef Chop Suey..............$7.50"); DefaultMutableTreeNode chickSuey = new DefaultMutableTreeNode("Chicken Chop Suey..............$7.50"); DefaultMutableTreeNode shrimpSuey = new DefaultMutableTreeNode("Shrimp Chop Suey..............$9.95"); DefaultMutableTreeNode combSuey = new DefaultMutableTreeNode("Combination Chop Suey..............$8.95"); // Add the nodes to the Chop Suey Chop_Suey.add(porkSuey); Chop_Suey.add(beefSuey); Chop_Suey.add(chickSuey); Chop_Suey.add(shrimpSuey); Chop_Suey.add(combSuey); // Creates a tree that display the started node tree = new JTree(root); // Create the tree selection model and set the tree model tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); // Sets the number of rows visible in the display tree.setVisibleRowCount(30); // Add the scroll pane to the tree JScrollPane sp = new JScrollPane(tree); // Sets the preferred size for scroll pane sp.setPreferredSize(new Dimension(350, 500)); // Add the scroll panel to the panel treePanel.add(sp); // Add the treePanel to the main panel mainPanel.add(treePanel, BorderLayout.WEST); // Create a text area panel textPanel = new JPanel(); // Creates a JText area textArea = new JTextArea("", 20, 50); // Sets the editability of the text area textArea.setEditable(false); // Add the text area to the scroll pane JScrollPane scroll = new JScrollPane(textArea); // Add the scroll pane to the panel textPanel.add(scroll); // Add the text panel to the main panel mainPanel.add(textPanel, BorderLayout.EAST); // Create a horizontal box Box box = Box.createHorizontalBox(); // Create a button panel buttonPanel = new JPanel(); // Create buttons removeall = new JButton("Remove All"); select = new JButton("Select"); calculate = new JButton("Calculate"); fivediscount = new JButton("5% discount"); tendiscount = new JButton("10% discount"); remove = new JButton("Remove"); exit = new JButton("Exit"); // Add the buttons to the box box.add(removeall); box.add(Box.createHorizontalStrut(60)); box.add(select); box.add(calculate); box.add(fivediscount); box.add(tendiscount); box.add(remove); box.add(Box.createHorizontalStrut(60)); box.add(exit); // Add the box to the button panel buttonPanel.add(box); // Create the event listener for the buttons calculate.addActionListener(new ButtonListener()); removeall.addActionListener(new ButtonListener()); select.addActionListener(new ButtonListener()); remove.addActionListener(new ButtonListener()); exit.addActionListener(new ButtonListener()); fivediscount.addActionListener(new ButtonListener()); tendiscount.addActionListener(new ButtonListener()); // Add the button panel to the main panel mainPanel.add(buttonPanel, BorderLayout.SOUTH); // Add the main panel to the content pane add(mainPanel); // Set the window to be visible setVisible(true); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { NumberFormat formatter = new DecimalFormat("#0.00"); if(e.getSource()==select) { Object getSelect = tree.getLastSelectedPathComponent(); DefaultMutableTreeNode creating = (DefaultMutableTreeNode) getSelect; String title = (String)creating.getUserObject(); textArea.append(title + "\n"); if(title.equals("Egg Rolls (4).........................$4.25")) { sum += 4.25; } else if(title.equals("Fried Wonton (10)......................$3.50")) { sum += 3.50; } else if(title.equals("Fried Cream Cheese Wonton (10) ......$4.25")) { sum += 4.25; } else if(title.equals("Fried Shrimp (6).......................$5.50")) { sum += 5.50; } else if(title.equals("Paper Wrapped Chicken.................$4.95")) { sum += 4.95; } else if(title.equals("Steamed/Pan Fried Dumping (8).........$4.50")) { sum += 4.50; } else if(title.equals("Chicken Wings in spicy Salt* .........$5.95")) { sum += 5.95; } else if(title.equals("House French Fries.......................$3.25")) { sum += 3.25; } else if(title.equals("Egg Drop Soup...............$4.95")) { sum += 4.95; } else if(title.equals("Hot & Sour Soup* ...............$5.25")) { sum += 5.25; } else if(title.equals("Sizzling Rice Soup..........$6.95")) { sum += 6.95; } else if(title.equals("Chicken Corn Soup................$5.50")) { sum += 5.50; } else if(title.equals("Seafood Soup..................$6.95")) { sum += 6.95; } else if(title.equals("Lotus Root in Fu-Chung Bean Curd Sauce...........$7.95")) { sum += 7.95; } else if(title.equals("Squid with Sweet & Sour Mustard Greens*...........$8.95")) { sum += 8.95; } else if(title.equals("Chicken Fried Rice with Ginger...............$7.25")) { sum += 7.25; } else if(title.equals("Home Style Cut Spare Ribs.......................$8.25")) { sum += 8.25; } else if(title.equals("Fish Fillet with Assorted Sweet Vegetables...$8.95")) { sum += 8.95; } else if(title.equals("Baked Pork Chop Pasta or Rice.................$6.95")) { sum += 6.95; } else if(title.equals("Chicken Wing / Ribs in Honey Garlic Sauce........$7.95")) { sum += 7.95; } else if(title.equals("Fish Fillet / Chicken in Spicy Lemon Sauce....$8.25")) { sum += 8.25; } else if(title.equals("Crispy Fried chicken (Half)....................$7.95")) { sum += 7.95; } else if(title.equals("Sauteed Seadfood with Macadamia Nut...........$10.25")) { sum += 10.25; } else if(title.equals("Sauteed Seafood with Tofu....................$9.95")) { sum += 9.95; } else if(title.equals("House Special Fish Fillet* .................$8.50")) { sum += 8.50; } else if(title.equals("Shrimp with Pine Nuts.......................$9.95")) { sum += 9.95; } else if(title.equals("Pork Chop Suey..............$7.50")) { sum += 7.50; } else if(title.equals("Beef Chop Suey..............$7.50")) { sum += 7.50; } else if(title.equals("Chicken Chop Suey..............$7.50")) { sum += 7.50; } else if(title.equals("Shrimp Chop Suey..............$9.95")) { sum += 9.95; } else if(title.equals("Combination Chop Suey..............$8.95")) { sum += 8.95; } } if(e.getSource()==calculate) { double tax = 0.0; tax = sum * 0.0978; sum -= discount; taxsum = tax + sum; JOptionPane.showMessageDialog(null, "The total is " + sum + "\n Tax rate is 9.75% " + "\nTotal with tax is "+ formatter.format(taxsum)); } if(e.getSource()==remove) { String title = textArea.getSelectedText(); textArea.replaceSelection(""); if(title.equals("Egg Rolls (4).........................$4.25")) { sum -= 4.25; } else if(title.equals("Fried Wonton (10)......................$3.50")) { sum -= 3.50; } else if(title.equals("Fried Cream Cheese Wonton (10) ......$4.25")) { sum -= 4.25; } else if(title.equals("Fried Shrimp (6).......................$5.50")) { sum -= 5.50; } else if(title.equals("Paper Wrapped Chicken.................$4.95")) { sum -= 4.95; } else if(title.equals("Steamed/Pan Fried Dumping (8).........$4.50")) { sum -= 4.50; } else if(title.equals("Chicken Wings in spicy Salt* .........$5.95")) { sum -= 5.95; } else if(title.equals("House French Fries.......................$3.25")) { sum -= 3.25; } else if(title.equals("Egg Drop Soup...............$4.95")) { sum -= 4.95; } else if(title.equals("Hot & Sour Soup* ...............$5.25")) { sum -= 5.25; } else if(title.equals("Sizzling Rice Soup..........$6.95")) { sum -= 6.95; } else if(title.equals("Chicken Corn Soup................$5.50")) { sum -= 5.50; } else if(title.equals("Seafood Soup..................$6.95")) { sum -= 6.95; } else if(title.equals("Lotus Root in Fu-Chung Bean Curd Sauce...........$7.95")) { sum -= 7.95; } else if(title.equals("Squid with Sweet & Sour Mustard Greens*...........$8.95")) { sum -= 8.95; } else if(title.equals("Chicken Fried Rice with Ginger...............$7.25")) { sum -= 7.25; } else if(title.equals("Home Style Cut Spare Ribs.......................$8.25")) { sum -= 8.25; } else if(title.equals("Fish Fillet with Assorted Sweet Vegetables...$8.95")) { sum -= 8.95; } else if(title.equals("Baked Pork Chop Pasta or Rice.................$6.95")) { sum -= 6.95; } else if(title.equals("Chicken Wing / Ribs in Honey Garlic Sauce........$7.95")) { sum -= 7.95; } else if(title.equals("Fish Fillet / Chicken in Spicy Lemon Sauce....$8.25")) { sum -= 8.25; } else if(title.equals("Crispy Fried chicken (Half)....................$7.95")) { sum -= 7.95; } else if(title.equals("Sauteed Seadfood with Macadamia Nut...........$10.25")) { sum -= 10.25; } else if(title.equals("Sauteed Seafood with Tofu....................$9.95")) { sum -= 9.95; } else if(title.equals("House Special Fish Fillet* .................$8.50")) { sum -= 8.50; } else if(title.equals("Shrimp with Pine Nuts.......................$9.95")) { sum -= 9.95; } else if(title.equals("Pork Chop Suey..............$7.50")) { sum -= 7.50; } else if(title.equals("Beef Chop Suey..............$7.50")) { sum -= 7.50; } else if(title.equals("Chicken Chop Suey..............$7.50")) { sum -= 7.50; } else if(title.equals("Shrimp Chop Suey..............$9.95")) { sum -= 9.95; } else if(title.equals("Combination Chop Suey..............$8.95")) { sum -= 8.95; } } if(e.getSource()==exit) { System.exit(0); } if(e.getSource()==removeall) { textArea.setText(""); sum = 0.0; } if(e.getSource()==fivediscount) { discount = sum * 0.05; } if(e.getSource()==tendiscount) { discount = sum * 0.1; } } catch(NullPointerException ae) { } catch(NumberFormatException ne) { } } } public static void main(String[] args) { new RestaurantMenu(); } }