In this project, you will continue to experiment with building your own Python classes. Many of you log into Facebook and interact with your friends. You will build classes to represent a text-version of Facebook.
This project is out of 20 points and offers up to 2 extra credit points once the project is complete. You can skip anything shown in blue and come back to finish it later for more credits.
We stongly encourage you to work on this project with a partner.
Here is an example of how all the classes come together to make our Facebook work. This output reflects implementation of all the extra credit features.
>>> TextFacebook = Facebook() >>> TextFacebook.registerUser('Bob', 'bob@gmail.com') >>> TextFacebook.registerUser('Alice', 'alice@gac.edu') >>> TextFacebook.registerUser('Charles', 'charlie@hotmail.com') >>> TextFacebook.login('Bob') >>> TextFacebook.addFriend('Alice') >>> TextFacebook.postStatus('is working on Project 10!') >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! >>> TextFacebook.logout() >>> TextFacebook.login('Charles') >>> TextFacebook.addFriend('Alice') >>> TextFacebook.postStatus('is going to watch Star Trek Into Darkness.') >>> TextFacebook.viewStatus() (1) Charles is going to watch Star Trek Into Darkness. >>> TextFacebook.logout() >>> TextFacebook.login('Alice') >>> TextFacebook.viewProfile() Alice (alice@gac.edu) Friends: Bob and Charles >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! (1) Charles is going to watch Star Trek Into Darkness. >>> TextFacebook.likeStatus(1) >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! (1) Charles is going to watch Star Trek Into Darkness. Alice likes this. >>> TextFacebook.commentOnStatus(0, 'Poor you... Have a cookie!') >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! Alice: Poor you... Have a cookie! (1) Charles is going to watch Star Trek Into Darkness. Alice likes this. >>> TextFacebook.logout() >>> TextFacebook.login('Bob') >>> TextFacebook.viewStatus() (0) Bob is working on Project 10! Alice: Poor you... Have a cookie! >>> TextFacebook.commentOnStatus(0, "It's all right. The project is a lot of fun!") >>> TextFacebook.postStatus("finished working on Project 10! Woohoo!") >>> TextFacebook.logout() >>> TextFacebook.login('Alice') >>> TextFacebook.viewStatus() (0) Bob is working on Project 10 Alice: Poor you... Have a cookie! Bob: It's all right. The project is a lot of fun! (1) Charles is going to watch Star Trek Into Darkness. Alice likes this. (2) Bob finished working on Project 10! Woohoo!
Implement the Comment
class in comment.py
such that:
Each comment consists of the name of the commentor and the content.
The Comment
class provides accessor and mutator methods for all instance variables.
The __str__
method of the Comment
class returns a string that shows the name and the content separated by a colon. For example, Inigo Montoya's comment "I do not think it means what you think it means." should return a string shown below.
'Inigo Montoya: I do not think it means what you think it means.'
Implement the Status
class in status.py
such that:
Each status consists of the name of the person who posted the status, the status message, names of those who liked the status, and comment objects associated with the status.
The Status
class provides accessor and mutator methods for all instance variables.
The Status
class provides the following methods:
A method addLike
which takes a name and adds the name to those who like the status.
A method addComment
which takes a Comment object and adds the given comment.
The __str__
method of the Status
class returns a string with the poster's name, the status message, the names of those who like the status and commments afterwards. The string should not have a newline character (\n
) at the end. If multiple users liked the status, they should be comma separated. For example, if Alice and Bob liked the status, "Alice, Bob like this."
The following string is returned by a Status
object whose poster is Ben Franklin and status message is 'signed the declaration of independence today. Booyah, England!'. France liked this Status
object, and there are four Comment
objects for the status.
'Ben Franklin signed the declaration of independence today. Booyah, England!\nFrance like this.\nJohn Adams: Bring it, bra.\nBen Franklin: You redcoats don't stand a chance.\nJohn Hancock: Does this mean we get to sign more? Cuz that was fun.\nBen Franklin: So we gathered.'
The output below shows when the above string is printed.
Ben Franklin signed the declaration of independence today. Booyah, England! France like this. John Adams: Bring it, bra. Ben Franklin: You redcoats don't stand a chance. John Hancock: Does this mean we get to sign more? Cuz that was fun. Ben Franklin: So we gathered.
For more credit, make the line with people who like the status gramatically correct. For example, "France likes this." and "France and Bob like this." and "France, Bob and Daniel like this."
Implement the User
class in user.py
such that:
Each user consists of his/her name, e-mail address and list of friends (User
objects). Only the name and e-mail address are required to create a User
object.
The User
class provides accessor and mutator methods for all instance variables.
The User
class provides the following methods:
A method addFriend
which takes a User
object and makes both users friends of each other.
A method isFriend
which takes a User
object and returns True
if the given user is a friend and False
otherwise.
The __str__
method of the User
class returns a string containg the name, e-mail address and the names of all friends. For example, if you print a user whose name is Jim Halpert, whose e-mail address is jim@office.com and whose only friend is Pam Beesley, the string should look as shown below.
'Jim Halpert (jim@office.com)\nFriends: Pam Beesley'
The output below shows when the above string is printed.
Jim Halpert (jim@office.com) Friends: Pam Beesley
If one has no friends, the output should be as shown below.
Chazz Michael Michaels (lonewolf@icerink) Friends: None
If one has more than one friend, the names of all the friends should belong to the same line as shown below.
Bob (bob@gmail.com) Friends: Alice, Charles, Daniel
For more credit, upgrade the format of the list of friends as shown below.
Bob (bob@gmail.com) Friends: Alice, Charles and Daniel Alice (alice@gmail.com) Friends: Bob and Charles
Implement the Facebook
class in facebook.py
such that:
The facebook.py
file starts with the following import
s:
from comment import * from status import * from user import *
Facebook keeps track of all the users and their statuses and the currently logged-in user. Facebook
object also keeps track of unique IDs for statuses. For the purpose of this project, only one user is allowed to use the Facebook at a time. When a Facebook object is created, it does not have any users or statuses.
Use a dict-of-string:user
, where the keys are names and the values are user objects, to keep track of users in the Facebook class.
Use a list-of-status
, where the positions in the list are status IDs and the values are status objects, to keep track of statuses in the Facebook class.
Use a string
(the name of the current user) to keep track of who's currently using your Facebook.
You do NOT have to implement any accessors and mutators for the instance variables in the Facebook
class.
You do NOT have to implement the __str__
method in the Facebook
class.
For examples of outputs from the following methods, refer the the example at the top of this page. None of the following methods returns any value; all output is printed
The Facebook
class provides a method called registerUser
which takes a name and an e-mail address. This method adds a new user to Facebook. It should print an error message if there is already a user with the same name.
The Facebook
class provides a method called login
which takes a name. This method allows a person to log into Facebook with the user associated with the given name. If another user is logged in already, it should print out an error message saying that the other user needs to log off first.
The Facebook
class provides a method called logout
. This method allows the currently logged-in user to log out from Facebook. It should print out an error message if there is no user logged in at the time.
The Facebook
class provides a method called addFriend
that takes a name. This method allows the currently logged-in user to befriend the user with the given name. This should make both users friends of each other. It should print out an error message if there is no user logged in at the time; or if there is no other user with the given name.
The Facebook
class provides a method called viewProfile
. This method allows the currently logged-in user to view his/her profile that includes his/her name and email address along with the list of his/her friends.
The Facebook
class provides a method called postStatus
. This method allows the currently logged-in user to post a new status. Facebook should add this new status. It should print out an error message if there is no user logged in at the time.
The Facebook
class provides a method called viewStatus
. This method allows the currently logged-in user to view statuses of the user and his/her friends. If there is no user logged in at the time, it should not print any statuses and print out an error message saying that a user need to be logged in. The selected statuses should be printed in order of status ID number.
The Facebook
class provides a method called likeStatus
which takes a status ID number. This method allows the currently logged-in user to like his/her status or his/her friend's status. If there is no user logged in at the time, it should print out an error message. If the status the user tries to "like" is not his/hers or his/her friend's, it should also print out an error message.
The Facebook
class provides a method called commentOnStatus
which takes a status identification number and a comment (string). This method allows the currently logged-in user to comment on his/her status or his/her friend's status. If there is no user logged in at the time, it should print out an error message. If the status the user tries to "comment" is not his/hers or his/her friend's, it should also print out an error message.
Remember not to print
anything in the __str__
method. You should accumulate ONE string that contains all the information and return it.
You will be submitting your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit the following files:
Comment
class.Status
class.User
class.Facebook
class.You will earn one point for each of the following accomplishments: (Project 10 is still out of 20 points. In other words, you may earn up to 2 points of extra credits in this project.)
You have all the instance variables necessary for the Comment
class.
You have all the accessor and mutator methods for all the instance variables in the Comment
class.
The __str__
method in the Comment
formats the code correctly.
You have all the instance variables necessary for the Status
class.
You have all the accessor and mutator methods for all the instance variables in the Status
class.
The __str__
method in the Status
class formats the code correctly.
The addLike
and addComment
methods are implemented correctly.
You have all the instance variables necessary for the User
class.
You have all the accessor and mutator methods for all the instance variables in the User
class.
The __str__
method in the User
class formats the code correctly.
The addFriend
method links two users as friends correctly in the User
class.
The isFriend
method checks if the given user is a friend or not correctly.
You have all the instance variables necessary for the Facebook
class.
The registerUser
method adds a new user to Facebook correctly.
The login
and logout
methods log in and out a user correctly and print out an error message when appropriate.
The addFriend
method correctly links two users as friends in the Facebook
class.
The viewProfile
method displays the information about the current user correctly.
The viewStatus
method displays only the status of the currently logged-in user and his/her friends, formatting
the statuses correctly and printing out an error message when appropriate.
The postStatus
method adds a new post by the user correctly. It prints out an error message when appropriate.
The likeStatus
and commentOnStatus
methods behave correctly. They print out an error message when appropriate.
The __str__
method of the status
class uses the grammatically correct format.
The __str__
method of the user
class uses the grammatically correct format.