CS 535: Object-Oriented Programming & Design |
---|
class Driver { public static void main( String args[] ) { System.out.println( "Welcome to Room Scheduler"); System.out.println( "-------------------------"); System.out.println( "Please select one of the following:"); System.out.println( ""); System.out.println( "1. Find an event"); System.out.println( "2. Schedule an event"); System.out.println( "3. Quit"); int request = Console.readInt( "Your Selection: " ); // etc } }
class Driver { public static void main( String args[] ) { int userSelection = showMainMenu(); // etc } public static int showMainMenu() { Console.println( "Welcome to Room Scheduler"); Console.println( "-------------------------"); Console.println( "Please select one of the following:"); Console.println( ""); Console.println( "1. Find an event"); Console.println( "2. Schedule an event"); Console.println( "3. Quit"); int request = Console.readInt( "Your Selection: " ); while ( (request >3) || (request < 1 )) { Console.println( "Your selection must be 1, 2, 3"); request = Console.readInt( "Your Selection: " ); } return request; } }
import java.util.Vector; import sdsu.io.Console; class Menu { Vector menuItems = new Vector(); String menuMessage; public void add( String newMenuItem ) { menuItems.addElement( newMenuItem ); } public void remove( int menuItemIndex ) { menuItems.removeElementAt( menuItemIndex ); } public void setMessage( String message ) { menuMessage = message; } private void displayMenuItems() { for ( int index = 1; index <= menuItems.size();index++) { Console.println( "" + index + ". " + menuItems.elementAt( index - 1 )); } }
public int show() { Console.println( menuMessage ); Console.println( ); displayMenuItems(); int request = Console.readInt( "Your Selection: " ); while ( (request > menuItems.size() ) || (request < 1 )) { Console.println( "Your selection must be between 1 and " + (menuItems.size() - 1) ); request = Console.readInt( "Your Selection: " ); } return request; } }
class Test { public static void main( String args[] ) { Menu counterMenu = new Menu(); counterMenu.add( "Increase Counter" ); counterMenu.add( "Decrease Counter" ); counterMenu.add( "Display Counter" ); counterMenu.add( "Quit" ); Counter testCount = new Counter(); int userSelection; do { userSelection = counterMenu.show(); switch (userSelection) { case 1: testCount.increase(); break; case 2: testCount.decrease(); break; case 3: Console.println( "Counter value = " + testCount); break; } } while ( userSelection != 4 ); } }
import java.lang.reflect.Method; class Foo { public void bar() { System.out.println( "Hi Mom" ); } } class TestMethod { public static void main( String[] arg) throws Exception { Foo test = new Foo(); Class classOfTest = test.getClass(); Class[] classOfBarsParameters = new Class[0]; Method bar = classOfTest.getMethod( "bar", classOfBarsParameters); bar.invoke( test, new Class[0]); } }
public MenuItem( String menuText, Object callBackObject, String callBackMethod)
public void add( MenuItem newMenuItem )
public void show()
class SampleMainProgram { public static void main( String args[] ) throws Exception { Counter count = new Counter(); count.addObserver( new ASCIICounterView( ) ); Menu counterMenu = new Menu(); // Note menu automatically adds a Quit Menu Item counterMenu.add( new MenuItem("Increase Counter", count, "increase" )); counterMenu.add( new MenuItem("Decrease Counter", count, "decrease" )); counterMenu.add( new MenuItem("Reset Counter", count, "reset" )); counterMenu.show(); } }
import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; class MenuItem { String text; Object callBackObject; Method callBackMethod; public MenuItem( String menuText, Object callBackObject, String callBackMethod) throws NoSuchMethodException { text = menuText; this.callBackObject = callBackObject; Class callBackClass = callBackObject.getClass(); Class[] parameters = new Class[0]; this.callBackMethod = callBackClass.getMethod( callBackMethod, parameters); } public String getText() { return text; } public String toString() { return text; }
public void invokeCallBack() { try { callBackMethod.invoke( callBackObject, new Class[0] ); } catch ( IllegalAccessException violatedMethodProtection ) { System.err.println( "MenuTime: " + violatedMethodProtection ); } catch ( InvocationTargetException callBackException ) { System.err.println( "MenuTime: " + callBackException ); } } }
class Menu { Vector menuItems = new Vector(); boolean repeatMenu = true; public Menu() { try { menuItems.addElement( "Null Object for 0'th index" ); menuItems.addElement( new MenuItem("Exit", this, "hide")); } catch ( NoSuchMethodException canNotHappen) { System.err.println( "Exception in Menu: " + canNotHappen); } } public void add( MenuItem newMenuItem ) { menuItems.addElement( newMenuItem ); } public void hide() { repeatMenu = false; }
public void show() { do { MenuItem selectedMenuItem = getUsersMenuSelection(); selectedMenuItem.invokeCallBack(); } while ( repeatMenu ); } private MenuItem getUsersMenuSelection() { Console.println( "\n\n---------------------------"); int request = displayMenuItems(); while ( (request >= menuItems.size() ) || (request < 1 )) { Console.println( "Your selection must be between 1 and " + (menuItems.size() - 1)); request = displayMenuItems(); } return (MenuItem) menuItems.elementAt( request); } private int displayMenuItems() { for ( int index = 1; index < menuItems.size();index++) { Console.println( "" + index + ". " + menuItems.elementAt( index )); } return Console.readInt( "Your Selection: " ); } }
class Counter extends Observable { private int count = 0; protected void changeNotify() { setChanged(); notifyObservers( ); } public void increase() { count++; changeNotify(); } public void decrease() { count--; changeNotify(); } public void reset() { count = 0; changeNotify(); } public int value() { return count; } public String toString() { return String.valueOf( count ); } }
class ASCIICounterView implements Observer { public void update( Observable sender, Object message ) { System.out.println( "\nThe current count is: " + ((Counter) sender) ); } }