package mug10; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Comparator; import rebelsky.io.pen; /* Write something that reads the user's input and parses it (figures out what they mean). You should accept reasonable synonyms and print error messages if you can't figure out what the mean. */ public class ReadAndParse { // +--------+-------------------------------------------------- // | Fields | // +--------+ String verb; String object; String modifier; Comparator metric; //String username; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /* Probably don't need but just in case public ReadAndParse(String _verb, String _object, String _modifier) { this.verb = _verb; this.object = _object; this.modifier = _modifier; } public ReadAndParse(String _verb, String _object) { this.verb = _verb; this.object = _object; } */ // +----------------+------------------------------------------ // | Public Methods | // +----------------+ public void readStuff() throws Exception { BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); String input = keyboard.readline(); String[] array1 = input.split(" "); this.verb = array1[0]; this.object = array1[1]; if (array1.length == 3) { this.modifier = array1[2]; } // check to see if the verb ie. array1[0] is in the array of valid actions String[] arrayOfActions = getArrayofActions(); int lb = 0; // The lower-bound of the portion of interest int ub = arrayOfActions.length - 1; // The upper-bound while (lb <= ub) { int mid = (lb + ub)/2; int c = metric.compare(this.verb, arrayOfActions[mid]); if (c == 0) // Found it! pen.println(interact(this.username, this.verb, this.object)); else if (c < 0) // Middle element too big! ub = mid-1; else // Middle element too small! lb = mid+1; } // while // Whoops, doesn't seem to be there throw new Exception("Invalid Action!"); } // readStuff() public void printMessage(String given) { pen.println(given); } /* Plan the user interface for world builders. What methods will you need World to provide to allow you to build or extend the world? We need a public boolean IsValid(String username, String password) method that checks if a user's password and username is valid. We need a public String[] getArrayOfActions() method which gets the array of possible actions that a user can do. We also need an public String interact(String username, String verb, String object) method. */ } // class ReadAndParse