/***********************************
Program: Stack and Queue Tester
Date: February 3, 2010
***********************************/
import hsa.Console; // Holtsoft's console
import java.awt.*; // Graphics
public class StackQueueTester
{
static Console c;
public static boolean isNumeric (String number) // Checks if param is a number
{
try
{
Integer.parseInt (number); // Attempt to convert number
}
catch (Exception e)
{
return false; // Fail
}
return true; // Succeess
}
public static void main (String[] args)
{
c = new Console ();
boolean quit = false;
boolean initialized = false;
String terminal = "";
// Positions main output
int paragraphX = 30;
int paragraphY = 30;
Color bgcolor = Color.black;
Color fgcolor = Color.blue;
Color textcolor = Color.white;
String escapeChar = "q"; // Used to quit entering values
int data[] = {}; // Data to be manipulated on the stack
stack s = new stack (); // New stack instance
while (!quit)
{
c.println ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); // Moves cursor to bottom
c.setColor (bgcolor);
c.fillRect (0, 0, c.getWidth (), c.getHeight ());
c.setColor (fgcolor);
c.fillRect (paragraphX - 10, paragraphY - 10, 250, 70);
c.fillRect (paragraphX - 10, 120, 500, 30);
c.setColor (textcolor);
c.setFont (new Font ("Verdana", Font.BOLD, 12));
c.drawString ("Program: Stacks", paragraphX, paragraphY + 10);
c.drawString ("Date: February 3, 2010", paragraphX, paragraphY + 30);
c.drawString ("1 - Create New 2 - Is Empty 3 - Add to 4 - Pop Off 5 - View Top", paragraphX, paragraphY + 110);
c.drawString ("Type a number in the menu and press Enter", paragraphX, paragraphY + 150);
c.setColor (Color.green);
c.drawString (terminal, paragraphX, paragraphY + 170);
String choice = c.readString ();
if (isNumeric (choice))
{
if (Integer.parseInt (choice) >= 1 && Integer.parseInt (choice) <= 5)
{
if (Integer.parseInt (choice) == 1) // Creates a new stack
{
s.init (); // Initializes the stack
initialized = true;
terminal = "Stack is now initialized!";
}
else if (Integer.parseInt (choice) == 2)
{
if (initialized)
{
if (s.empty ())
terminal = "Stack is empty!";
else
terminal = "Stack is not empty!";
}
else
terminal = "Must initialize a stack first!";
}
else if (Integer.parseInt (choice) == 3)
{
if (initialized)
{
terminal = "";
String val = " ";
String all = " ";
while (!val.equalsIgnoreCase (escapeChar))
{
c.setColor (bgcolor);
c.fillRect (0, 0, c.getWidth (), c.getHeight ());
c.setColor (textcolor);
c.drawString ("Type a number then press ", paragraphX, paragraphY + 10);
c.drawString (" to add to the stack", paragraphX + 220, paragraphY + 10);
c.drawString ("Type ' ' then press", paragraphX, paragraphY + 30);
c.drawString ("to return to the Main Menu", paragraphX + 178, paragraphY + 30);
c.setColor (Color.pink);
c.drawString (escapeChar.toUpperCase (), paragraphX + 38, paragraphY + 30);
c.drawString ("ENTER", paragraphX + 130, paragraphY + 30);
c.drawString ("ENTER", paragraphX + 175, paragraphY + 10);
c.setColor (new Color (150, 150, 255));
c.drawString (all, paragraphX - 7, paragraphY + 80);
c.setColor (Color.green);
c.drawString (terminal, paragraphX, paragraphY + 110);
val = c.readString ();
if (isNumeric (val))
{
s.push (val);
all += " " + val;
terminal = "";
}
else if (val.equalsIgnoreCase (escapeChar))
break;
else if (!isNumeric (val))
terminal = "Please enter a numeric value";
}
}
else
terminal = "Must initialize a stack first!";
}
else if (Integer.parseInt (choice) == 4)
{
if (initialized)
{
if (!s.empty ())
{
s.pop ();
terminal = "Popped value from top of the stack";
}
else
terminal = "Stack is empty! Cannot pop";
}
else
terminal = "Must initialize a stack first!";
}
else if (Integer.parseInt (choice) == 5)
{
if (initialized)
terminal = "Top of the stack: " + s.top ();
else
terminal = "Must initialize a stack first!";
}
}
else
terminal = "Please enter a number from the menu";
}
else
terminal = "Please enter a numeric value";
}
}
}
|