/* * SampleApp.java - Sample Application to run. * * Copyright (c) 1996 Chuck McManis, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. * * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.util.Random; import java.io.DataInputStream; import java.io.IOException; /** * This class implements a "magic 8-ball" type game where questions * asked are answered by randomly selecting an answer. */ public class SampleApp { public static void main(String Args[]) { run_app(System.in, System.out); } public static void run_app(java.io.InputStream in, java.io.PrintStream out) { Random r = new Random(); DataInputStream dis = new DataInputStream(in); out.println("Welcome to the Java Oracle, ask me any question"); out.println("that has a yes or no answer."); out.println(); while (true) { String answer = null; out.println("What question do you have today?"); out.print("> "); out.flush(); try { answer = dis.readLine(); } catch (IOException e) { break; } if (answer.startsWith("repeat")) { int iterations = 0; try { iterations = Integer.parseInt(answer.substring(answer.indexOf(':')+1).trim()); } catch (NumberFormatException e) { } iterations = Math.abs(iterations); while (iterations > 0) { out.print("Random Spooge "); int zz = (Math.abs(r.nextInt()) % 10) + 1; while (zz-- > 0) out.print("*"); out.println(" : "+iterations); iterations--; } } if (answer.startsWith("quit")) break; int z = Math.abs(r.nextInt()) % 5; switch (z) { case 0: out.println("Yes, definitely."); break; case 1: out.println("No, absolutely not."); break; case 2: out.println("Perhaps, if you are lucky."); break; case 3: out.println("Unlikely, this is not recommended."); break; case 4: out.println("My vision is unclear, ask later."); break; } } out.println("Thank you for consulting the Oracle, good day."); } }