COMP 142: Project 6 --- Hogwarts Facebook
Update, 4/25
I have added a walkthrough for getting started with the project. See the walkthrough
here and the diagram here.
Overview
Though electronic devices don't work at Hogwarts School of Witchcraft and Wizardry
(because magic), recently as part of the Muggle Studies curriculum, the professors have
bewitched some computers to run inside the Hogwarts castle, and of course the
students discovered social media. The professors have blocked access to Facebook however,
so you need to create a simplified version of the social media platform for them to access.
You will create a program that runs a simplified version of Facebook, specifically
designed for Hogwarts. The program will read in a text file giving a list of students,
which Hogwarts houses they are in, and who is friends with whom.
Then your program will allow the user to browse Facebook through a menu.
Getting started
Make a new CLion project and create a new C++ class called facebook. See the project 4
webpages if you forget how to do this. You should now have three files, called main.cpp,
facebook.h, and facebook.cpp.
Place the following code in each files: main.cpp facebook.h facebook.cpp
Program details
You should use a struct in your program to hold each Facebook user. The declaration of the struct
should be the following (note this is already defined in facebook.h for you):
struct person
{
string name;
string house;
vector<person*> friends;
};
The struct contains the following information about a person on Facebook:
- name: the name of the person.
- house: the person's Hogwarts house (Gryffindor, Ravenclaw, Hufflepuff, or Slytherin).
- friends: a vector of pointers to all the friends of this person.
You will notice that the facebook class is mostly empty --- you should add appropriate
fields (variables) and methods (functions) as you see fit. The only thing you should not change
is the all_people field --- this variable stores a vector of pointers to people: one
person for everyone on Facebook.
The entire friendship network is maintained through this vector of people. All of the
individual people in the network should be allocated on the heap (using new
),
and then you will add pointers to those people to the all_people
vector.
You should make sure to add a destructor to the facebook class so that the memory used to store
all the people will be deallocated at the end of the program; this will be done by using
delete.
Note that you will need a loop to do this, since you will
have to delete
each individual person one at a time.
Operations your program must support
- Printing all the people and friendships in the database.
- Printing all of the friendships for a specific person.
- Printing all of the cross-house friendships.
- Adding a new person to the database.
- Do this by allocating a new
person
on the heap (using new
), then use push_back
to add the
pointer to the new person to the all_people
vector. This person will initially have no friends.
- Adding a new friendship (between two people) to the database.
- Do this by searching the
all_people
vector for each person's name. This should give you two pointers (one to each person).
Then add each pointer to the other person's friends
vector.
- Removing a friendship.
- Removing a person (and all of their friendships).
How the program should work
- Your program should first prompt the user for a filename that contains all of the Facebook
users and their friendships. See below for details of how this file is arranged.
- Your program should read in the contents of this file and create a facebook object
that contains all of the people and friendships specified in the file.
- Your program will then go into a loop where a menu is printed and the user can select
from the operations listed earlier.
The text file
This file is a very simple encoding of all the people and friendships that the Hogwarts version of
Facebook should be loaded with at the beginning (other people or friendships might be added
later by the user as the program runs).
- Every line in the file will have exactly three words, separated by a space.
- The first
word will be either the string
person
or the string friend
.
- For a line that begins with
person
, the next two words will be the name of
the person and their Hogwarts house (these are each guaranteed to be one word only).
- For a line that begins with
friend
, the next two words will be two
names of people who should be added as friends in the database (friendships are reciprocal,
so if "A" is friends with "B," then "B" is also friends with "A," even though the friendship
will only be listed once in this file).
Reading and processing this file should be straightforward. It will be even easier if you
make functions for adding a new person and adding a new friendship, because you can just
call these functions while processing the file, and also use the same functions
later when the user needs those options.
Here is a sample file.
Coding style
- All the normal guidelines for coding from previous projects apply.
Do not use any global variables.
- This is the first time you are designing a class pretty much from scratch, so it is up to
you to decide how to structure the class. You get to pick what the facebook class can do, and
what should go into the class and what should be left out of it.
- Choosing what to leave out is equally
as important as choosing what goes into the class.
- When deciding what should go into the class
vs what should go outside of the class, ask yourself, "If someone else were using this Facebook class
in their C++ program, would they ever call this function?" If the answer is no, that code should
probably not go inside the class.
-
For instance, you probably do not want
the code that prints the Facebook menu or asks for the input filename to be inside the class; that should
be in main or in a function in main.cpp.
Sample input and output
Sample input/output that uses the text file from earlier.
Your program's output should match mine.
Your program should never crash, and you should print error messages if the user
selects an option that requires a name but the name given does not correspond to someone
on Facebook. For instance, if someone
chooses to print all the friends of a certain person, and they mistype the person's name,
you can just print an error message and return to the menu; you do not have to re-prompt
for their name.
How to submit
Upload your code to Moodle. Please upload all three files (main.cpp, facebook.h, and facebook.cpp) and no others.
Challenge problem
Add in the ability for people to make (text-only) posts to Facebook. Posts should consist of
a string with some text, with an optional list of Facebook users that are tagged in the post.
For instance, Harry might post "Totally retrieved the Sorcerer's stone earlier tonight --- thanks for the help
@Ron and @Hermione! Quirrell always weirded me out lol." Add a menu option to let a user make post
and tag people in it. Then add a menu option to display all posts where a certain person
is tagged.