Question: The Facebook Class Create a class called, Facebook, and define a field called users. Define the users field as a List and instantiate the list

The Facebook Class

Create a class called, Facebook, and define a field called users. Define the users field as a

List and instantiate the list as an ArrayList in the constructor.

public Facebook() {

users = new ArrayList<>();

}

The Facebook class should have methods to list all the users (i.e. print out their usernames),

add a user, delete a user, and get and display the password hint for a user.

Create a public static void main method which prints the following menu to the console and

awaits user input.

Menu

1. List Users,

2. Add a User,

3. Delete a User,

4. Get Password Hint

5. Quit

Read the users choice and call the appropriate method on the Facebook object. This should

continue until the user chooses to quit. (See the below sections for the details on each menu

choice)

Serializing and De-serializing

We want the contents of our Facebook object to persist between program executions. To do

this, serialize your FacebookUser object before the program terminates (see the Quit section

below for the details on this). When the program starts up again, de-serialize the Facebook

object rather than creating a new one.

Remember: to serialize and de-serialize the FacebookUser class, it (along with its super class)

must implement Serializable.

Menu Options

List Users

Loop through all the Facebook users and print their user name to the console.

Add User

Prompt the user for the user name to add. Check if the supplied user name already

exists in the user list. If it does, display an error message to the console and re-display

the menu.

If the supplied user name is unique, do the following

o prompt the user to enter a password and a password hint

o construct a FacebookUser instance and add the instance to the list of users on

the Facebook class.

o Afterwards, re-display the menu.

Delete User

Prompt for the user name to delete. Check if the supplied user name exists in the user

list.

If the user does NOT exist, display an error message to the console and re-display the

menu.

If the user does exist, prompt for the password of the user being deleted.

If the password is not correct, display an error message to the console and re-display

the menu.

If the password is correct, remove the user from the list of users on the Facebook class

and re-display the menu.

Retrieve the Password Hint

Prompt for the user name. Check if the supplied user name exists in the user list.

If the user does NOT exist, display an error message to the console and re-display the

menu.

If the user does exist, display the password hint to the console and re-display the menu.

Quit

private static final String PATH =

System.getProperty("user.home") +

File.separator + "FolderName" + File.separator;

private static final String FILE_NAME = "FacebookUsers.dat";

Place the above static final fields in your Facebook class.

Using the above static final fields, serialize the users list to the PATH +

FILE_NAME location.

o new FileOutputStream(PATH + FILE_NAME);

o The PATH constant will point to your computers home directory.

o The File.separator constant will contain the directory separator character

for your operating system.

/ for linux or macs

\ for windows

If any of the folders in the PATH do not exist, create them.

o new File(PATH).mkdirs();

If an existing FacebookUsers.dat file already exists, delete it and recreate it upon exiting

the application.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!