package LinkedLists; /** * This class uses the QueueAsArray class to demonstrate * the use of a queue. A service queue is modelled. * * Author: Richard Jones, UWCSEA, Singapore * Date: May 2001 * Hardware: PII 333, 128 Mb RAM * Software: Visual Age for Java V2 * */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class SingtelQueue extends Applet implements ActionListener { // interface objects Label greetLabel = new Label("Serving You Better - That's Your Singtel!"); Label nextLabel = new Label("Next in queue:", Label.RIGHT); Label lengthLabel = new Label("Queue length: ", Label.RIGHT); Label nameLabel = new Label("Name: ", Label.RIGHT); Label phoneLabel = new Label("Phone: ", Label.RIGHT); Label messageLabel = new Label("System messages will appear here"); TextField next = new TextField("details of next person"); TextField length = new TextField("q length"); TextField name = new TextField(20); TextField phone = new TextField(12); Button dispenseButton = new Button("Dispense ticket"); Button nextButton = new Button("Next customer"); Panel infoPanel = new Panel(); Panel inputPanel = new Panel(); TextArea display = new TextArea( 10, 40); // the queue CustomerQueue q = new CustomerQueue(6); /** * SingtelQueue constructor comment. */ public SingtelQueue() { super(); } // // Responds to button presses... // public void actionPerformed(ActionEvent e) { // Add and remove items from the customer queue if (e.getSource() == dispenseButton) { dispense(); } else { serve(); } updateDisplay(); } /** * Method to add a name and phone to the queue * */ public void dispense() { CustomerNode theCustomer; // new customer node String nm, ph; // Get the text (and check if there is any) nm = name.getText(); ph = phone.getText(); if ( (nm.length() == 0) || (ph.length() == 0) ) { messageLabel.setText("Enter name and phone details"); } else { try // try to enqueue the details { theCustomer = new CustomerNode(nm, ph, null); q.enqueue(theCustomer); length.setText("" + q.getSize()); messageLabel.setText("Customer added"); } catch (QueueFullException qfe) { messageLabel.setText(qfe.getMessage()); } } // clear the input boxes name.setText(""); phone.setText(""); } /** * Method to set up the display */ public void init() { add(greetLabel); infoPanel.setLayout( new GridLayout( 2, 2, 10, 10 ) ); infoPanel.add(nextLabel); infoPanel.add(next); infoPanel.add(lengthLabel); infoPanel.add(length); add(infoPanel); add(dispenseButton); add(nextButton); dispenseButton.addActionListener(this); nextButton.addActionListener(this); inputPanel.setLayout( new GridLayout( 2, 2, 10, 10 ) ); inputPanel.add(nameLabel); inputPanel.add(name); inputPanel.add(phoneLabel); inputPanel.add(phone); add(inputPanel); add(messageLabel); add(display); } /** * Method to dequeue users as they are served */ public void serve() { CustomerNode temp; // next customer button was pushed try // to get the front item { temp = q.dequeue(); next.setText(temp.getName()); length.setText("" + q.getSize()); messageLabel.setText("OK"); } catch (QueueEmptyException qee) { messageLabel.setText(qee.getMessage()); // no queue next.setText(""); phone.setText(""); } } /** * Method to display the queue */ void updateDisplay() { CustomerNode temp = q.getFront(); int i = 0; display.setText(""); if (q.isEmpty()) { display.setText("Nobody waiting...\n"); } else { do { display.append("" + ++i + ": " + temp.getName() + " " + temp.getPhone() + "\n"); temp = q.getNext(temp); } while (temp != null); } } }