Skip to content

Instantly share code, notes, and snippets.

@jraregris
Created February 10, 2010 14:51
Show Gist options
  • Select an option

  • Save jraregris/300386 to your computer and use it in GitHub Desktop.

Select an option

Save jraregris/300386 to your computer and use it in GitHub Desktop.
class Friend {
private Person self;
private Friend next;
Friend (Person self) {
this.self = self;
}
/**
* Add a friend to the list
* @param buddy person to gain as friend
*/
public void addFriend(Person buddy) {
if (hasNext()) {
next.addFriend(buddy);
}
else {
next = new Friend(buddy);
}
}
/**
* Remove a friend from the list
* @param buddy person to lose as friend
*/
public void removeFriend(Friend buddy) {
if (getNext() == buddy) {
if (next.hasNext()) {
next = next.getNext();
}
else {
next = null;
}
}
else {
next.removeFriend(buddy);
}
}
/**
* Check for subsequent friend in list
* @return false if this is last in line
* true if someone follows
*/
public boolean hasNext() {
if (next != null) {
return true;
}
else {
return false;
}
}
/**
* Get person this friend-object represents
* @return this person
*/
public Person getSelf() {
return self;
}
/**
* Get next friend in list
* @return next friend in line
*/
public Friend getNext() {
return next;
}
/**
* Check if said person is a friend
* @param person person to check
* @return true if person is in list
* false if not friends
*/
public boolean hasFriend(Person person) {
if (self == person) {
return true;
}
else if (hasNext()) {
return next.hasFriend(person);
}
else {
return false;
}
}
/**
* Get said friend from list
* @param buddy friend to get
* @return passes the friend
*/
public Friend getFriend(Person buddy) {
if (self == buddy) {
return this;
}
else if (hasNext()) {
return next.getFriend(buddy);
}
else {
return null;
}
}
/**
* Counts all friends
* @return amount of friends in list
*/
public int getSize() {
if (hasNext()) {
return 1 + next.getSize();
}
else {
return 1;
}
}
/**
* Put all friends into an array - naive:
* Expects parameters to be checked somewhere else
* @param p array to fill
* @param count scorekeeper
*/
public void getFriends(Person[] p, int count) {
p[count] = this.self;
if (hasNext()) {
next.getFriends(p, ++count);
}
}
}
class FriendList {
private Friend first;
/**
* Add a new friend to the list
* @param buddy person to gain as friend
*/
public void addFriend(Person buddy) {
if (hasFirst()) {
first.addFriend(buddy);
}
else {
first = new Friend(buddy);
}
}
/**
* Check if there is anyone in the list
* @return true if list is populated
* false if not
*/
public boolean hasFirst() {
if (first != null) {
return true;
}
else {
return false;
}
}
/**
* Get the current number of friends in list
* @return number of friends
*/
public int getSize() {
if (!hasFirst()) {
return 0;
}
else {
return first.getSize();
}
}
/**
* Get specific friend
* @param buddy person to match friend
*/
public Friend getFriend(Person buddy) {
return first.getFriend(buddy);
}
/**
* Remove said friend from list
* @param buddy person to lose as friend
*/
public void removeFriend(Friend buddy) {
if (hasFirst() && first == buddy) {
if (first.hasNext()) {
first = first.getNext();
}
else {
first = null;
}
}
else if (hasFirst() ){
first.removeFriend(buddy);
}
}
/**
* Check if said person is in list
* @param person person to check for
* @return true if person is a friend
* false if not
*/
public boolean hasFriend(Person person) {
if (hasFirst()) {
return first.hasFriend(person);
}
else {
return false;
}
}
/**
* Get all the persons in the persons friend list
* @param p array to keep persons in
*/
public void getFriends(Person[] p) {
if (hasFirst()) {
first.getFriends(p, 0);
}
}
}
class Person {
private String name;
private String phone;
private FriendList friends;
private Person next;
Person (String name, String phone) {
this.name = name;
this.phone = phone;
friends = new FriendList();
}
/**
* Get the name of the person
* @return name of the person
*/
public String getName() {
return name;
}
/**
* Get the phone number of the person
* @return phone number to the person
*/
public String getPhone() {
return phone;
}
/**
* Check for subsequent person in list
* @return false if this is last in line
* true if someone follows
*/
public boolean hasNext() {
if (next != null) {
return true;
}
else {
return false;
}
}
/**
* Get a person.
* @param name name of the person to get
* @return person that was requested
*/
public Person getPerson(String name) {
if (this.name.equals(name)) {
return this;
}
else if (hasNext()) {
return next.getPerson(name);
}
else {
return null;
}
}
/**
* Get next person in list
* @return person next in line
*/
public Person getNext() {
if (hasNext()) {
return next;
}
else {
return null;
}
}
/**
* Add a new person to the list
* @param name name of the person
* @param phone phone number of the person
*/
public void addPerson(String name, String phone) {
if (hasNext()) {
next.addPerson(name, phone);
}
else {
next = new Person(name, phone);
}
}
/**
* Removes this person from list
* @param p person to remove
*/
public void removePerson(Person p) {
if (getNext() == p) {
next.removeAllFriends();
if (hasNext()) {
next = next.getNext();
}
else {
next = null;
}
}
else {
next.removePerson(p);
}
}
/**
* Counts all persons
* @return amount of persons in list
*/
public int getSize() {
if (hasNext()) {
return 1 + next.getSize();
}
else {
return 1;
}
}
/**
* Puts all persons in an array
* @param persons array to store them in
* @param count position to place them
*/
public void getPersons(Person[] persons, int count) {
persons[count] = this;
if (hasNext()) {
next.getPersons(persons, ++count);
}
}
/**
* Adds a friend to persons friend list
* @param buddy person to gain as friend
*/
public void addFriend(Person buddy) {
friends.addFriend(buddy);
}
/**
* Checks if a person is in friend list
* @param person person to look for
* @return false if not friend
* true if friend
*/
public boolean hasFriend(Person person) {
return friends.hasFriend(person);
}
/**
* Removes a friend from persons friend list
* @param buddy person to lose as friend
*/
public void removeFriend(Person buddy) {
Friend f = friends.getFriend(buddy);
friends.removeFriend(f);
}
/**
* Removes all friends from friendlist
*/
public void removeAllFriends() {
for (Person p : getFriends()) {
p.removeFriend(this);
removeFriend(p);
}
}
/**
* Get all the persons in the persons friend list.
* @return friends of this person in array
*/
public Person [] getFriends() {
Person[] p = new Person[friends.getSize()];
friends.getFriends(p);
return p;
}
}
class PersonList {
private Person first;
/**
* Add a new person to the list
* @param name name of the person
* @param phone phone number of the person
* @return true if person was added successfully.
* false if the person is already in the list
*/
public boolean addPerson(String name, String phone) {
if (getPerson(name) != null) {
return false;
}
else if (hasFirst()) {
first.addPerson(name, phone);
return true;
}
else {
first = new Person(name, phone);
return true;
}
}
/**
* Check if there is anyone in the list
* @return true if list is populated
* false if not
*/
private boolean hasFirst() {
if (first != null) {
return true;
}
else {
return false;
}
}
/**
* Remove a person from the list.
* @param name name of the person
* @return true if person was successfully removed
* false if the person doesn't exist.
*/
public boolean removePerson(String name) {
Person p = getPerson(name);
if (p == null) {
return false;
}
else if (hasFirst() && first == p) {
first.removeAllFriends();
if (first.hasNext()) {
first = first.getNext();
}
else {
first = null;
}
return true;
}
else {
first.removePerson(p);
return true;
}
}
/**
* Add a person as a friend to another person
* @param name name of the person to gain a friend
* @param friendName name of the new friend
* @return true if successfully added person as friend
* false if friendName is already a friend
*/
public boolean addFriend(String name, String friendName) {
Person p = getPerson(name);
Person f = getPerson(friendName);
if ((p == null) || (f == null)) {
return false;
}
else if (p.hasFriend(f) || f.hasFriend(p)) {
return false;
}
else {
p.addFriend(f);
f.addFriend(p);
return true;
}
}
/**
* Remove a friend from a person
* @param name name of the person to lose a friend
* @param friendName name of the friend to be removed
* @return true if successfully removd person as friend
* false if friendName isn't a friend
*/
public boolean removeFriend(String name, String friendName) {
Person p = getPerson(name);
Person f = getPerson(friendName);
if ((p == null) || (f == null)) {
return false;
}
else if (!p.hasFriend(f) || !f.hasFriend(p)) {
return false;
}
else {
p.removeFriend(f);
f.removeFriend(p);
return true;
}
}
/**
* Get the current number of persons in the list.
* @return number of persons
*/
public int getSize() {
if (!hasFirst()) {
return 0;
}
else {
return first.getSize();
}
}
/**
* Get an array of persons currently in the list.
* @return array of persons
*/
public Person [] getPersons() {
Person[] persons = new Person [getSize()];
if (!hasFirst()) {
return persons;
}
else {
first.getPersons(persons, 0);
return persons;
}
}
/**
* Get a person.
* @param name name of the person to get
* @return person that was requested
*/
public Person getPerson(String name) {
if (hasFirst()) {
return first.getPerson(name);
}
else {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment