// IMPROVED ORDER PROCESSING APPLET /* This applet will accept orders for toys. It provides for toy cost computation and enables several toys to be ordered in one order number. A maximum of 10 toys may be ordered after which an error message is displayed in the status bar. The cart like feature is implemented using several arrays with the same array pointer. Grid baglayout is used to position the GUI elements. Item listeners are used in a few combo boxes to display costs once a combo box element is clicked. */ /* To run, save the prgram as Orders.java and compile it using the command javac Orders.java. Create an html file containing the applet tag indicating "Orders.class" as code and height and width set to 200 and 500 pixels respectively. Save the html file as Orders.html and run it using the command appletviewer Orders.html. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; // Inherit JApplet properties // Let the applet implement items and actions listeners public class Orders extends JApplet implements ActionListener, ItemListener { private int arrayPointer = 0; private String toyId; private int toyQty; private double toyAmt; private double wrappingCharge; private double shippingCharge; private double toyTotal; private double totalAmount; private String [] toyIdArray; private int [] toyQtyArray; private double [] toyAmtArray, wrapChargeArray; private double [] shipChargeArray, toyTotalArray; private int nextShopperId = 111001; private int nextOrderNo = 901001; protected String [] toyIds = {"T0103", "T7352", "T6382", "T4068", "T1630"}; protected final double [] toyCosts = {10.0, 20.0, 30.0, 40.0, 50.0, 0.0}; protected final String [] giftWraps = {"Birthday", "Graduation", "Christmas", "Ordinary", "Special"}; protected final double [] wrappingCharges = {1.0, 2.0, 3.0, 4.0, 5.0, 0.0}; protected final String [] shippingModes = {"Standard", "World Mail", "UPS", "DHL", "Express Padala"}; protected final double [] shippingCharges = {2.0, 4.0, 6.0, 8.0, 10.0, 0.0}; JPanel toyPanel; JPanel buttonsPanel; GridBagLayout gbl; GridBagConstraints gbc; protected JLabel lblShopperId, lblOrderNo; protected JLabel lblToyId, lblQty, lblToyCost, lblToyAmount; protected JLabel lblGiftWrap, lblShippingMode; protected JLabel lblWrappingCharges, lblShippingCharges, lblToyTotal, lblRunningTotal; protected JTextField txtShopperId, txtOrderNo, txtQty; protected JComboBox cmbToyId, cmbGiftWrap, cmbShippingMode; protected JTextField txtToyCost, txtToyAmount; protected JTextField txtGiftWrapCharges, txtShippingCharges, txtToyTotal, txtRunningTotal; JButton btnSubmit, btnCompute, btnAddToy, btnClear, btnCancel; public Orders() { // Instantiate the arrays toyIdArray = new String [10]; toyQtyArray = new int [10]; toyAmtArray = new double [10]; wrapChargeArray = new double [10]; shipChargeArray = new double [10]; toyTotalArray = new double [10]; gbl = new GridBagLayout(); gbc = new GridBagConstraints(); toyPanel = new JPanel(); toyPanel.setLayout(gbl); buttonsPanel = new JPanel(); // Add everrything to the content pane getContentPane().setLayout(new BorderLayout()); getContentPane().add(toyPanel, BorderLayout.CENTER); getContentPane().add(buttonsPanel, BorderLayout.SOUTH); lblShopperId = new JLabel("Shopper Id: "); lblOrderNo = new JLabel("Order No: "); lblToyId = new JLabel("Toy Id: "); lblQty = new JLabel("Quantity: "); lblToyCost = new JLabel("Toy Cost: "); lblToyAmount = new JLabel("Toy Amount: "); lblGiftWrap = new JLabel("Gift Wrap: "); lblShippingMode = new JLabel("Shipping Mode: "); lblWrappingCharges = new JLabel("Wrapping Charges: "); lblShippingCharges = new JLabel("Shipping Charges: "); lblToyTotal = new JLabel("Toy Total: "); lblRunningTotal = new JLabel("Running Total: "); txtShopperId = new JTextField(10); txtOrderNo = new JTextField(10); txtQty = new JTextField(5); cmbToyId = new JComboBox(toyIds); cmbGiftWrap = new JComboBox(giftWraps); cmbShippingMode = new JComboBox(shippingModes); txtToyCost = new JTextField(10); txtToyAmount = new JTextField(10); txtGiftWrapCharges = new JTextField(10); txtShippingCharges = new JTextField(10); txtToyTotal = new JTextField(10); txtRunningTotal = new JTextField(10); // Add buttons to their own panel // Register each with action listener btnSubmit = new JButton("Submit"); btnSubmit.addActionListener(this); buttonsPanel.add(btnSubmit); btnCompute = new JButton("Compute"); btnCompute.addActionListener(this); buttonsPanel.add(btnCompute); btnAddToy = new JButton("Add Toy"); btnAddToy.addActionListener(this); buttonsPanel.add(btnAddToy); btnClear = new JButton("Clear"); btnClear.addActionListener(this); buttonsPanel.add(btnClear); btnCancel = new JButton("Cancel"); btnCancel.addActionListener(this); buttonsPanel.add(btnCancel); gbc.anchor = GridBagConstraints.NORTHEAST; // Align labels gbc.gridx = 1; gbc.gridy = 1; gbl.setConstraints(lblShopperId, gbc); toyPanel.add(lblShopperId); gbc.gridx = 1; gbc.gridy = 2; gbl.setConstraints(lblOrderNo, gbc); toyPanel.add(lblOrderNo); gbc.gridx = 1; gbc.gridy = 3; gbl.setConstraints(lblToyId, gbc); toyPanel.add(lblToyId); gbc.gridx = 1; gbc.gridy = 4; gbl.setConstraints(lblQty, gbc); toyPanel.add(lblQty); gbc.gridx = 1; gbc.gridy = 5; gbl.setConstraints(lblGiftWrap, gbc); toyPanel.add(lblGiftWrap); gbc.gridx = 1; gbc.gridy = 6; gbl.setConstraints(lblShippingMode, gbc); toyPanel.add(lblShippingMode); gbc.gridx = 3; gbc.gridy = 1; gbl.setConstraints(lblToyCost, gbc); toyPanel.add(lblToyCost); gbc.gridx = 3; gbc.gridy = 2; gbl.setConstraints(lblToyAmount, gbc); toyPanel.add(lblToyAmount); gbc.gridx = 3; gbc.gridy = 3; gbl.setConstraints(lblWrappingCharges, gbc); toyPanel.add(lblWrappingCharges); gbc.gridx = 3; gbc.gridy = 4; gbl.setConstraints(lblShippingCharges, gbc); toyPanel.add(lblShippingCharges); gbc.gridx = 3; gbc.gridy = 5; gbl.setConstraints(lblToyTotal, gbc); toyPanel.add(lblToyTotal); gbc.gridx = 3; gbc.gridy = 6; gbl.setConstraints(lblRunningTotal, gbc); toyPanel.add(lblRunningTotal); gbc.anchor = GridBagConstraints.NORTHWEST; // Align text fields gbc.gridx = 2; gbc.gridy = 1; gbl.setConstraints(txtShopperId, gbc); txtShopperId.setEditable(false); txtShopperId.setText("S" + Integer.toString(nextShopperId)); toyPanel.add(txtShopperId); gbc.gridx = 2; gbc.gridy = 2; gbl.setConstraints(txtOrderNo, gbc); txtOrderNo.setEditable(false); txtOrderNo.setText("T" + Integer.toString(nextOrderNo)); toyPanel.add(txtOrderNo); gbc.gridx = 2; gbc.gridy = 3; gbl.setConstraints(cmbToyId, gbc); cmbToyId.addItemListener(this); // Register item listener cmbToyId.setSelectedIndex(-1); toyPanel.add(cmbToyId); gbc.gridx = 2; gbc.gridy = 4; gbl.setConstraints(txtQty, gbc); toyPanel.add(txtQty); gbc.gridx = 2; gbc.gridy = 5; gbl.setConstraints(cmbGiftWrap, gbc); cmbGiftWrap.addItemListener(this); // Register item listener cmbGiftWrap.setSelectedIndex(-1); toyPanel.add(cmbGiftWrap); gbc.gridx = 2; gbc.gridy = 6; gbl.setConstraints(cmbShippingMode, gbc); cmbShippingMode.addItemListener(this); // Register item listener cmbShippingMode.setSelectedIndex(-1); toyPanel.add(cmbShippingMode); gbc.gridx = 4; gbc.gridy = 1; gbl.setConstraints(txtToyCost, gbc); txtToyCost.setEditable(false); toyPanel.add(txtToyCost); gbc.gridx = 4; gbc.gridy = 2; gbl.setConstraints(txtToyAmount, gbc); txtToyAmount.setEditable(false); toyPanel.add(txtToyAmount); gbc.gridx = 4; gbc.gridy = 3; gbl.setConstraints(txtGiftWrapCharges, gbc); txtGiftWrapCharges.setEditable(false); toyPanel.add(txtGiftWrapCharges); gbc.gridx = 4; gbc.gridy = 4; gbl.setConstraints(txtShippingCharges, gbc); txtShippingCharges.setEditable(false); toyPanel.add(txtShippingCharges); gbc.gridx = 4; gbc.gridy = 5; gbl.setConstraints(txtToyTotal, gbc); txtToyTotal.setEditable(false); toyPanel.add(txtToyTotal); gbc.gridx = 4; gbc.gridy = 6; gbl.setConstraints(txtRunningTotal, gbc); txtRunningTotal.setEditable(false); toyPanel.add(txtRunningTotal); } public void initializeToyArray() { for(int i = 0; i < 10; i++) { toyIdArray[i] = ""; toyQtyArray[i] = 0; toyAmtArray[i] = 0.0; wrapChargeArray[i] = 0.0; shipChargeArray[i] = 0.0; toyTotalArray[i] = 0.0; } } public void displayToys() { String stmp = txtShopperId.getText(); String otmp = txtOrderNo.getText(); System.out.println("\nShopper Id: " + stmp); System.out.println("Order No.: " + otmp); for(int i = 0; i < 10; i++) { if(toyTotalArray[i] == 0.0) { break; } else { System.out.println("\nToy Id: " + toyIdArray[i]); System.out.println("Qty: " + toyQtyArray[i]); System.out.println("Toy Amount: " + toyAmtArray[i]); System.out.println("Gift Wrap Charge: " + wrapChargeArray[i]); System.out.println("Shipping Charge: " + shipChargeArray[i]); System.out.println("Toy Total: " + toyTotalArray[i]); } } System.out.println("\nTotal Bill " + totalAmount); initializeToyArray(); arrayPointer = 0; totalAmount = 0.0; resetToy(); txtRunningTotal.setText(""); nextOrderNo = nextOrderNo + 1; txtOrderNo.setText("T" + Integer.toString(nextOrderNo)); } public void updateToyArray() { toyIdArray [arrayPointer] = toyId; toyQtyArray [arrayPointer] = toyQty; toyAmtArray [arrayPointer] = toyAmt; wrapChargeArray [arrayPointer] = wrappingCharge; shipChargeArray [arrayPointer] = shippingCharge; toyTotalArray [arrayPointer] = toyTotal; } public void addToy() { int cmbIndex = cmbToyId.getSelectedIndex(); toyId = toyIds[cmbIndex]; computeToyTotal(); if(arrayPointer > 9) { getAppletContext().showStatus("Only 10 toys allowed per order."); } else { updateToyArray(); } arrayPointer = arrayPointer + 1; } public void resetToy() { txtShopperId.setText("S" + Integer.toString(nextShopperId)); txtOrderNo.setText("T" + Integer.toString(nextOrderNo)); cmbToyId.setSelectedIndex(-1); txtQty.setText(""); cmbGiftWrap.setSelectedIndex(-1); cmbShippingMode.setSelectedIndex(-1); txtToyCost.setText(""); txtToyAmount.setText(""); txtGiftWrapCharges.setText(""); txtShippingCharges.setText(""); txtToyTotal.setText(""); // txtRunningTotal.setText(""); getAppletContext().showStatus(""); } public boolean isToySelected() { if(cmbToyId.getSelectedIndex() == -1) { return false; } else { return true; } } public boolean isQtyNumeric() { String tmp = txtQty.getText(); for(int i = 0; i <= tmp.length()-1; i++) { if(tmp.charAt(i) < '0' || tmp.charAt(i) > '9') { return false; } } return true; } public boolean isQtyEmpty() { String tmp = txtQty.getText(); if(tmp.length() == 0) { return true; } else { return false; } } public double computeToyAmount() { toyQty = Integer.parseInt(txtQty.getText()); int cmbIndex = cmbToyId.getSelectedIndex(); double toyAmt = toyQty * toyCosts[cmbIndex]; return toyAmt; } public double computeWrappingCharges() { int cmbIndex = cmbGiftWrap.getSelectedIndex(); if(cmbIndex == -1) { cmbIndex = 5; } double totWrap = toyQty * wrappingCharges[cmbIndex]; return totWrap; } public double computeShippingCharges() { int cmbIndex = cmbShippingMode.getSelectedIndex(); if(cmbIndex == -1) { cmbIndex = 5; } double totShip = toyQty * shippingCharges[cmbIndex]; return totShip; } public void computeToyTotal() { toyAmt = computeToyAmount(); wrappingCharge = computeWrappingCharges(); toyTotal = toyAmt + wrappingCharge; shippingCharge = computeShippingCharges(); toyTotal = toyTotal + shippingCharge; } public void computeTotalAmount() { totalAmount = totalAmount + toyTotal; } public void displayToyAmounts() { txtToyAmount.setText(Double.toString(toyAmt)); txtGiftWrapCharges.setText(Double.toString(wrappingCharge)); txtShippingCharges.setText(Double.toString(shippingCharge)); txtToyTotal.setText(Double.toString(toyTotal)); } public void displayRunningTotal() { txtRunningTotal.setText(Double.toString(totalAmount)); } public void itemStateChanged(ItemEvent ie) // Item event handling { if(ie.getSource() == cmbToyId) { int cmbIndex = cmbToyId.getSelectedIndex(); if(cmbIndex == -1) { txtToyCost.setText(""); } else { txtToyCost.setText(Double.toString(toyCosts[cmbIndex])); } } else if(ie.getSource() == cmbGiftWrap) { int cmbIndex = cmbGiftWrap.getSelectedIndex(); if(cmbIndex == -1) { txtGiftWrapCharges.setText(""); } else { txtGiftWrapCharges.setText(Double.toString(wrappingCharges[cmbIndex])); } } else if(ie.getSource() == cmbShippingMode) { int cmbIndex = cmbShippingMode.getSelectedIndex(); if(cmbIndex == -1) { txtShippingCharges.setText(""); } else { txtShippingCharges.setText(Double.toString(shippingCharges[cmbIndex])); } } } public void actionPerformed(ActionEvent ae) // Action event handling { if(ae.getSource() == btnSubmit) { if(toyTotalArray[0] == 0.0) { getAppletContext().showStatus("You haven't ordered anything."); } else { displayToys(); } } else if(ae.getSource() == btnCompute) { if(isToySelected() == false) { getAppletContext().showStatus("Please select a toy."); } else { if(isQtyEmpty() == true) { getAppletContext().showStatus("Please enter quantity."); } else { if(isQtyNumeric() == false) { getAppletContext().showStatus("Qty should be numeric."); } else { computeToyTotal(); displayToyAmounts(); } } } } else if(ae.getSource() == btnAddToy) { if(isToySelected() == false) { getAppletContext().showStatus("Please select a toy."); } else { if(isQtyEmpty() == true) { getAppletContext().showStatus("Please enter quantity."); } else { if(isQtyNumeric() == false) { getAppletContext().showStatus("Qty should be numeric."); } else { addToy(); computeTotalAmount(); displayRunningTotal(); resetToy(); } } } } else if(ae.getSource() == btnClear) { resetToy(); } else if(ae.getSource() == btnCancel) { initializeToyArray(); arrayPointer = 0; totalAmount = 0.0; resetToy(); txtRunningTotal.setText(""); } } public void init() { Orders myOrders = new Orders(); } } /* To use, select a toy id and enter quantity. Select wrapping and shipping modes if required. Click compute to find out how much the toy will cost. Click add toy to add toy to the order. Click submit to send the order to the store. Clear will erase the order fields except running total. Cancel will erase everything. Submit button also prints a bill. */