package hw5; public class Person { /** The person's full name */ private String name; public enum Status {MARRIED, SINGLE, UNKNOWN, ITS_COMPLICATED}; private Status status; public String getName() { return name; } public void setName(String name) { this.name = name; } private FriendsList friends; private Wall wall; public Person() { this("",Status.UNKNOWN); } public Person (String name, Status status) { this.name = name; this.status = status; friends = new FriendsList(); wall = new Wall(); } public String toString() { return name + " " + status + "\n" + friends.getNum() + " Friends:\n" + friends + "Wall:\n" + wall; } public void addFriend(Person p) { friends.addFriend(p); p.friends.addFriend(this); //p.addFriend would create an infinite loop } public void removeFriend(Person p) { friends.removeFriend(p); p.friends.removeFriend(this); } public void addWallMessage(String msg, Person author) { if (friends.isFriend(author)) { wall.addPost(msg, author); } else { System.out.println("ERROR: " + author.getName() + " is not a friend of " + name); } } /** * Two persons are the same if they have the same name. * @param other * @return true if both have the same name. */ public boolean equals(Person other) { return name.equalsIgnoreCase(other.name); } /** * @param args */ public static void main(String[] args) { Person mark = new Person("Mark Zuckerberg",Status.SINGLE); Person cam = new Person("Cameron Winklevoss",Status.ITS_COMPLICATED); Person tyler = new Person("Tyler Winklevoss",Status.MARRIED); Person tyler2 = new Person("tyler winklevoss",Status.UNKNOWN); Person peter = new Person("Peter Thiel",Status.UNKNOWN); System.out.println(mark); mark.addFriend(cam); mark.addFriend(tyler); mark.addFriend(tyler); //adding it for a SECOND time, so it does not get added mark.addFriend(tyler2); //tyler2 does not get added because he has the same name as tyler System.out.println(mark); //mark has 2 friends System.out.println(tyler); //tyler has 1 friend mark.addWallMessage("Hello there! We have a project for you!", tyler); mark.addWallMessage("Yeah, its a Harvard-only friendster", cam); tyler.addWallMessage("Cool, I'll work on it", mark); System.out.println(mark); System.out.println(tyler); mark.addWallMessage("Hey, how is our social website coming?", cam); cam.addWallMessage("Its going to take some time, I got finals", mark); peter.addFriend(mark); peter.addWallMessage("I made this cool website, want to fund me?", mark); mark.addWallMessage("Sure", peter); mark.addWallMessage("Wait a minute! We paid you to build that!", cam); cam.addWallMessage("Nah hah", mark); mark.addWallMessage("Yeah ha", cam); cam.addWallMessage("I'm unfriending you and your twin", mark); mark.removeFriend(cam); mark.removeFriend(tyler); System.out.println(mark); System.out.println(tyler); System.out.println(cam); System.out.println(peter); mark.addWallMessage("I'm going to sue you in federal court!", cam); System.out.println(mark); } } package hw5; public class FriendsList { private static final int MAX_FRIENDS = 1024; private Person[] friend; private int numFriends; public int getNum() { return numFriends; } public FriendsList() { friend = new Person[MAX_FRIENDS]; numFriends = 0; } /** * Find out if p is in here. * @param p * @return true if p is in this friendlist */ public boolean isFriend(Person p) { for (int i=0; i < numFriends; i++) { if (friend[i].equals(p)) { return true; } } return false; } /** * Add p to list but only if it is not in here already. * @param p */ public void addFriend(Person p) { if (!isFriend(p)) { friend[numFriends++] = p; } } public String toString() { String result = ""; for (int i=0; i < numFriends; i++) { result += friend[i].getName() + "\n"; } return result; } public void removeFriend(Person p) { int i; for (i=0; i < numFriends; i++) { if (friend[i].equals(p)) break; } if (i < numFriends) {//p is a friend, at index i //overwrite friend[i] with friend[i+1], and so on till the end of the list for (int j = i; j < numFriends - 1; j++) { friend[j] = friend[j+1]; } numFriends--; } } } package hw5; public class Post { public String message; public Person author; public Post(String message, Person author) { this.message = message; this.author = author; } public String getMessage() { return message; } public String getAuthorName() { return author.getName(); } } package hw5; public class Wall { private Post[] posting; private int numPosts; private static final int MAX_WALL_POSTS = 1024; public Wall() { posting = new Post[MAX_WALL_POSTS]; numPosts = 0; } public void addPost(String msg, Person author) { Post post = new Post(msg, author); posting[numPosts++] = post; } public String toString() { String result = ""; for (int i=numPosts-1; i >= 0; i--) { result += posting[i].getAuthorName() + " writes:\n" + posting[i].getMessage() + "\n"; } return result + "\n"; } }
Tuesday, October 19, 2010
HW5 Solution
Below is my solution to this homework:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment