/***************************************************************************** * ExampleClient: example client for TigerBook and Person (prog. midterm 2) * author: Princeton COS 126 course staff, Spring 2013 * dependencies: Person, TigerBook, StdIn, StdOut * * This is a sample test client for TigerBook and Person. It reads commands * from standard input one at a time. The accepted command formats are: * * person1 registers : construct/register a person with name/id person1 * person1 posts : calls post() * person1 reads : calls listMessages(), which prints output * person1 meets person2 : calls meet() * person1 queries person2 : calls knows() and prints output * // : comment line * * The or can have spaces, but "person1" and "person2" * MUST NOT have spaces in their names. * * This program is very basic. So use caution if you write your own inputs: * for example, putting extra words or not enough words on one line can * cause errors that may seem unusual at first, and typos can be disastrous. * ****************************************************************************/ public class ExampleClient { public static void main(String[] args) { TigerBook tb = new TigerBook(); while (!StdIn.isEmpty()) { String first = StdIn.readString(); // first word on line if (first.startsWith("//")) { // is this line a comment? StdIn.readLine(); // if so, skip the rest of the line continue; // and don't try to read an action } String name = first; // the first word is a person String action = StdIn.readString(); // now, read the action // now translate the action to method calls if (action.equals("registers")) { Person newUser = new Person(name); tb.register(name, newUser); } else if (action.equals("meets")) { String name2 = StdIn.readString(); Person user1 = tb.lookup(name); Person user2 = tb.lookup(name2); user1.meet(user2); } else if (action.equals("posts")) { String message = StdIn.readLine(); // read rest of line message = message.substring(1); // get rid of space at start Person user = tb.lookup(name); user.post(message); } else if (action.equals("queries")) { String name2 = StdIn.readString(); Person user1 = tb.lookup(name); Person user2 = tb.lookup(name2); StdOut.print("Are "+name+" and "+name2+" friends? "); boolean isFriend = user1.knows(user2); StdOut.println(isFriend); } else if (action.equals("reads")) { Person user = tb.lookup(name); user.listMessages(); } else { // this case would be triggered by a line like "person1 coughs" // but could also be caused by typos, or putting too many words // on one line, or not enough words on one line, etc String msg = "ExampleClient doesn't know how to perform action"; msg += " \""+action+"\"\n Your input is misformatted, "; msg += "see the ExampleClient header comment for help"; throw new RuntimeException(msg); } } } }