TQueue0.java
Select all
/* TQueue0.java David Klick CIS 260 3/23/09 This program provides an interactive interface to an underlying queue. Items may be added and removed from the queue, and the user may also peek at the next item set to be removed. */ import java.util.*; import java.io.*; public class TQueue0 { private static BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); public enum Command { QUIT, PRINT, STATUS, HELP, PEEK, ADD, REMOVE, CLEAR } private static Hashtable
initCommandTable() { Hashtable
hash = new Hashtable
(); hash.put("QUIT", Command.QUIT); hash.put("Q", Command.QUIT); hash.put("EXIT", Command.QUIT); hash.put("X", Command.QUIT); hash.put("PRINT", Command.PRINT); hash.put("P", Command.PRINT); hash.put("STATUS", Command.STATUS); hash.put("STAT", Command.STATUS); hash.put("S", Command.STATUS); hash.put("HELP", Command.HELP); hash.put("H", Command.HELP); hash.put("PEEK", Command.PEEK); hash.put("?", Command.PEEK); hash.put("ADD", Command.ADD); hash.put("A", Command.ADD); hash.put("+", Command.ADD); hash.put("REMOVE", Command.REMOVE); hash.put("R", Command.REMOVE); hash.put("-", Command.REMOVE); hash.put("CLEAR", Command.CLEAR); hash.put("C", Command.CLEAR); return hash; } private static void error(String msg) { System.out.println(msg); } private static boolean isInteger(String s) { return s.matches("[+-]?[0-9]+"); } private static void println(String msg) { System.out.println(msg); } public static void main(String[] args) throws IOException { boolean done = false; MyQueue q = new MyQueue(); // intialize command lookup table Hashtable
commands = initCommandTable(); // process commands displayHelp(); System.out.println("\nProgram ready for processing"); while (!done) { int position, value; String[] cmd = getCommand(); int cmdLength = cmd.length; Command command = commands.get(cmd[0].toUpperCase()); if (command == null) error("Unrecognized command"); else { switch (command) { case QUIT: if (cmdLength > 1) error("QUIT command takes no arguments"); else done = true; break; case PRINT: if (cmdLength > 1) error("PRINT command takes no arguments"); else q.print(); break; case STATUS: if (cmdLength > 1) error("STATUS command takes no arguments"); else displayStatus(q); break; case HELP: if (cmdLength > 1) error("HELP command takes no arguments"); else displayHelp(); break; case PEEK: if (cmdLength > 1) error("PEEK command takes no arguments"); else { try { println("" + q.peek()); } catch (MyQueueException e) { println(e.getMessage()); } } break; case ADD: if (cmdLength != 2) error("ADD command takes one argument"); else if (!isInteger(cmd[1])) error("ADD command argument must be an integer"); else { try { q.add(Integer.parseInt(cmd[1])); q.print(); } catch (MyQueueException e) { error(e.getMessage()); } } break; case REMOVE: if (cmdLength > 1) error("REMOVE command takes no arguments"); else { try { println("Removed from queue: " + q.remove()); q.print(); } catch (MyQueueException e) { println(e.getMessage()); } } break; case CLEAR: if (cmdLength > 1) error("CLEAR command takes no arguments"); else { try { while (!q.isEmpty()) { println("Removed from queue: " + q.remove()); } } catch (MyQueueException e) { println(e.getMessage()); } } break; default: error("Unrecognized command"); } } } // end of command processing loop println("End of processing"); } public static String[] getCommand() { String s = new String(); System.out.print("\n> "); try { while ((s = in.readLine()).length() == 0); } catch (IOException e) { System.out.println("I/O Exception"); } s = s.toUpperCase(); StringTokenizer p = new StringTokenizer(s); String[] cmds = new String[p.countTokens()]; try { int i = 0; while (p.hasMoreTokens()) cmds[i++] = p.nextToken(); } catch (NoSuchElementException e) { System.out.println(e); } if (cmds.length > 0) { // This handles the special case where the first character // is a plus (insert) or minus (delete) and is followed by // a number without any delimiter in between. This code // breaks the single "word" into two separate "words" char c = cmds[0].charAt(0); if ((cmds[0].length() > 1) && ((c == '+') || (c == '-'))) { String[] newCmds = new String[cmds.length + 1]; for (int i=1; i
v; public MyQueue() { v = new Vector
(); } public void clear() { v.clear(); } public void add(int n) throws MyQueueException { v.addElement(new Integer(n)); } public int remove() throws MyQueueException { int n = peek(); v.removeElementAt(0); return n; } public int peek() throws MyQueueException { if (isEmpty()) throw new MyQueueException("Queue empty"); else return ((Integer) (v.firstElement())).intValue(); } public int length() { return v.size(); } public boolean isEmpty() { return v.isEmpty(); } public boolean isFull() { return false; } public void print() { System.out.println(v.toString()); } public String toString() { return v.toString(); } } class MyQueueException extends Exception { public MyQueueException() {} public MyQueueException(String msg) { super(msg); } }