import java.util.*; import java.io.*; // already written ********************** // IM( String person ): set up a new im for the person // void initialize( String person ): set up the im components for the person // ArrayList getFriends(): returns user's im friends // ArrayList whoseLoggedOn(): returns user's im online friends // String whoami(): returns the name of the person running the im // String getXimuLocation(): returns the folder containing the im info // void setXimuLocation( String s ): sets the folder containing im info // String toString(): returns a text representation of the im // void logon(): set status to here // void logout(): set status to gone and delete messages // to be written ********************** // boolean isFriend(): return whether person is a friend // boolean isLoggedOn( String person ): return whether person is an // online friend // void initializeFriends(): set up a list of all im friends // void updateWhoseOnline(): reset list of online friends // void sendMessage( String person, String contents ): if person is a // friend; sets the friend's current im message // String getMessage( String person ): returns message if person is a // friend; otherwise, returns null public class IM { String ximuFolderLocation; String user; ArrayList friends; ArrayList onlineFriends; public static void main( String[] args ) { ConsoleIM.main( null ); } // IM(): set ups a new im for the person public IM( String person ) { this.initialize( person ); } // isFriend(): return whether person is a friend ***** public boolean isFriend( String person ) { // determine whether person is a friend, if not return false return false; // replace with your own code } // isLoggedOn(): return whether person is an online friend ***** public boolean isLoggedOn( String person ) { // determine whether person is a friend, if not return false // determine friend's ximu folder // try to read status file of friend // no file indicates not logged on // empty file indicates not logged on // file contents do not start with "here" indicates not logged on // file contents start with "here" indicates logged on return false; // replace with your own code } // initializeFriends(): set up a list of all im friends ***** public void initializeFriends() { // first set up an empty list this.friends = new ArrayList(); // now add each person in the friends file to the friend list and // send them an empty message ("") this.friends.add( "cohoon" ); // replace with your own code } // updateWhoseOnline(): reset list of online friends ***** public void updateWhoseOnline() { // reset onlist to empty this.onlineFriends = new ArrayList(); // now add each online friend from the friend list to the online list this.onlineFriends.add( "cohoon" ); // replace with your own code } // sendMessage(): sets the current message of the friend to contents ***** public void sendMessage( String person, String contents ) { // determine whether person is a friend, if not return // set friend file to contents } // getMessage(): returns the current message of the friend (if the ***** // friend is not logged on or has no message for us, returns null public String getMessage( String person ) { // determine whether person is a friend, if not return null // determine friend's logon status // not logged on indicates null return value // logged on but no message indicates null return value // logged on and has message indicates message needs to be read // and returned. return "getMessage() not done"; // replace with your own code } // woe to those who change this code ********************** // initialize(): sets up the im components private void initialize( String person ) { this.user = person; this.setXimuLocation( person ); this.initializeFriends(); this.updateWhoseOnline(); this.logon(); } public String whoami() { return this.user; } // getFriends(): returns the user's im friends public ArrayList getFriends() { // hand over friend list return this.friends; } // whoseLoggedOn(): returns the user's online im friends public ArrayList whoseLoggedOn() { // hand over the logged on list return this.onlineFriends; } // logon(): set status to here public void logon() { // get the folder for the status file String folder = this.getXimuLocation(); // set up a connection to the status file Connection status = new Connection( folder, "status" ); // get a printer to the file PrintStream out = status.getWriter(); // set contents of status file to "here" out.println( "here" ); } // logout(): set contents of status file to "gone" and delete messages public void logout() { // get the folder for the status file String folder = this.getXimuLocation(); // set up a connection to the status file Connection status = new Connection( folder, "status" ); // get a printer to the file PrintStream out = status.getWriter(); // set contents of status file to "gone" out.println( "gone" ); // get the im friends ArrayList list = this.getFriends(); // for each friend, delete the current message for ( int i = 0; i < list.size(); ++i ) { String friend = list.get( i ); Connection c = new Connection( folder, friend ); c.delete(); } } // getXimuLocation(): returns the folder containing the im info public String getXimuLocation() { return this.ximuFolderLocation; } // setXimuLocation(): sets the folder containing im info public void setXimuLocation( String s ) { String os = System.getProperty( "os.name" ); boolean isMac = ( os.indexOf( "Mac" ) == 0 ); boolean isPC = ( os.indexOf( "Windows" ) == 0 ); String location; if ( isPC ) { location = "J:\\public_html\\ximu\\"; } else if ( isMac ) { location = "/Volumes/" + s + "/public_html/ximu/"; } else { location = "/home/" + s + "/public_html/ximu/"; } this.ximuFolderLocation = location; } // toString(): produce a text representation of the instant messenger public String toString() { String representation = ""; representation += "IM[ "; representation += this.whoami() + ", "; representation += this.getXimuLocation() + ", "; representation += "friends( " + this.getFriends() + " ), "; representation += "online( " + this.whoseLoggedOn() + " ), "; representation += " ]"; return representation; } // getXimuPage(): returns the name of the web page for the person's ximu // account public static String getXimuPage( String person ) { String base = "http://www.people.virginia.edu/~"; String page = base + person + "/ximu/"; return page; } }