TQueue1.java
Select all
/* TQueue1.java David Klick CIS 260 3/23/09 This program provides an interactive interface to an underlying circular 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 TQueue1 { final static int DEFAULT_QUEUE_SIZE = 10; 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; int queueSize = DEFAULT_QUEUE_SIZE; // process command line parameters (array length) if (args.length == 1) { try { queueSize = Integer.parseInt(args[0]); if ((queueSize > 20) || (queueSize < 5)) { println("Illegal queue size specified"); queueSize = DEFAULT_QUEUE_SIZE; } } catch (NumberFormatException e) { error("Unrecognized command line argument"); System.exit(0); } } else if (args.length > 1) { error("Only one command line parameter allowed. " + args.length + " found."); System.exit(0); } else { error("No command line parameters found. Using default" + " queue size of " + DEFAULT_QUEUE_SIZE + "."); } MyCQueue q = new MyCQueue(queueSize); // 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
= arr.length) rear = 0; arr[rear] = n; numElements++; } public int remove() throws MyQueueException { int n = peek(); if (++front > arr.length-1) front = 0; numElements--; return n; } public int peek() throws MyQueueException { if (isEmpty()) throw new MyQueueException("Queue empty"); else return arr[front]; } public boolean isEmpty() { return (numElements == 0); } public boolean isFull() { return (numElements == arr.length); } public String toString() { String s = "["; int pos = front; for (int i=0; i
arr.length-1) pos = 0; } s += "]"; return s; } public void print() { System.out.println(toString()); } } class MyQueueException extends Exception { public MyQueueException() {} public MyQueueException(String msg) { super(msg); } }