The Facebook Class Now we’ll continue our project by writing a Facebook class that contains an ArrayList of Facebook user objects. The Facebook class should have methods to list all of the users (i.e. print out their usernames), add a user, delete a user, and get the password hint for a user. You will also need to create a driver program. The driver should create an instance of the Facebook class and display a menu containing five options: Menu 1. List Users, 2. Add a User, 3. Delete a User, 4. Get Password Hint 5. Quit Then it should read in the user’s choice and call the appropriate method on the Facebook object. This should continue until the user chooses to quit. You don’t need to worry about adding and removing “friends” for this assignment – we’ll do that later in the project. Here’s the catch: we want the contents of our Facebook object to persist between program executions. To do this, serialize your Facebook object before the program terminates. When the program starts up again, de-serialize the Facebook object rather than creating a new one. Here are some details: When adding a new FacebookUser, prompt the user for the username and check to see that if the users ArrayList already contains a FacebookUser object with that username. If it does, display an error message. If the username is unique, prompt for a password and password hint and create a new FacebookUser object with those values. Add this new user to the users ArrayList. When deleting a FacebookUser, prompt for the username and check to see that the users ArrayList contains a FacebookUser object with that username. If it doesn’t display an error message. If it does, prompt for the password and check that the FacebookUser object with this username has the same password as the one that was entered. If it doesn’t, display an error message. If the passwords match, delete this FacebookUser object from the users ArrayList When retrieving the password hint for a FacebookUser, ask for the username and display an error message if there is no user with that username. Obviously, if the user does exist you should display the password hint without requiring the user’s password. You will be graded according to the following rubric (each item is worth one point): The Facebook class has the appropriate fields and methods Copyright © 2017, 2018 Sinclair Community College. All Rights Reserved. The Facebook class can list the FacebookUser objects (i.e. their usernames) It is possible to add a user at runtime It is possible to delete a user at runtime It is possible to get the password hint for a user at runtime The driver program creates a Facebook object that is serialized before the program ends The driver program de-serializes the Facebook object when it starts executing, if a serialized version exists. If a serialized version of the Facebook object does not exist, the driver program creates a new Facebook object. The program compiles The program runs The program is clearly written and follows standard coding conventions Note: If your program does not compile, you will receive a score of 0 on the entire assignment Note: If you program compiles but does not run, you will receive a score of 0 on the entire assignment Note: If your Eclipse project is not exported and uploaded to the eLearn drop box correctly, you will receive a score of 0 on the entire assignment Note: If you do not submit code that solves the problem for this particular assignment, you will not receive any points for the program’s compiling, the program’s running, or following standard coding conventions.
package Array;
import java.io.*;
import java.util.*;
// Defines class user
class User
{
// Instance variable to store user information
String userName;
String password;
String hint;
// Default constructor to assign default values
User()
{
userName = password = hint = "";
}// End of default constructor
// Parameterized constructor to assign parameter values
User(String un, String ps, String hi)
{
userName = un;
password = ps;
hint = hi;
}// End of parameterized constructor
// Method to set user name
void setUserName(String un)
{
userName = un;
}// End of method
// Method to set password
void setPassword(String pa)
{
password = pa;
}// End of method
// Method to set hint
void setHint(String hi)
{
hint = hi;
}// End of method
// Method to return user name
String getUserName()
{
return userName;
}// End of method
// Method to return password
String getPassword()
{
return password;
}// End of method
// Method to return hint
String getHint()
{
return hint;
}// End of method
// Overrides toString() method to return user information
public String toString()
{
return "\n User Name: " + userName +
"\n Passowrd: " + password;
}// End of method
}// End of class User
// Defines a class Facebook implements Serializable interface
class Facebook implements Serializable
{
// Declares an array list to store User class Object
ArrayList <User>facebookUser;
// Creates scanner class object
Scanner sc = new Scanner(System.in);
// Default constructor to create array list
Facebook()
{
facebookUser = new ArrayList<User>();
}// End of default constructor
// Method to search user name if found returns the
// found index position otherwise returns -1
int matchUserName(String un)
{
// Loops till number of users
for(int x = 0; x < facebookUser.size(); x++)
// Checks if current user user name
// is equals to parameter user name
// then returns the found index position as x
if(facebookUser.get(x).getUserName().
compareTo(un) == 0)
return x;
// Otherwise returns -1 for not found
return -1;
}// End of method
// Method to search user password if found returns the
// found index position otherwise returns -1
int matchPassword(String ps)
{
// Loops till number of users
for(int x = 0; x < facebookUser.size(); x++)
// Checks if current user password
// is equals to parameter password
// then returns the found index position as x
if(facebookUser.get(x).getPassword().
compareTo(ps) == 0)
return x;
// Otherwise returns -1 for not found
return -1;
}// End of method
// Method to add a user account to facebook
void add()
{
String un, ps, hi;
// Accepts user name
System.out.print("\n Enter user name: ");
un = sc.next();
// Calls the method to check the user name availability
// -1 for not found
if(matchUserName(un) == -1)
{
// Accepts password
System.out.print("\n Enter password: ");
ps = sc.next();
// Accepts password hint
System.out.print("\n Enter password hint: ");
hi = sc.next();
// Creates a user
User u = new User(un, ps, hi);
// Adds the user to array list
facebookUser.add(u);
}// End of if condition
// Otherwise displays error message
else
System.out.println("User name " + un +
"already exist.");
}// End of method
// Method to remove a user account from facebook
void remove()
{
String un, ps;
// Accepts user name
System.out.print("\n Enter user name: ");
un = sc.next();
// Calls the method to check the user name availability
int pos = matchUserName(un);
// Checks if found position is -1 then not found
if(pos == -1)
{
// Displays error message
System.out.println("Invalid User Name " + un);
return;
}// End of if condition
// Accepts user password
System.out.print("\n Enter password: ");
ps = sc.next();
// Calls the method to check the password availability
pos = matchPassword(ps);
// Checks if found position is -1 then not found
if(pos == -1)
{
// Displays error message
System.out.println("Invalid Password. " + ps);
return;
}// End of if condition
// Otherwise displays error message
else
facebookUser.remove(pos);
}// End of method
// Method to display password hint
void getHint()
{
String un;
// Accepts user name
System.out.print("\n Enter user name: ");
un = sc.next();
// Calls the method to check the user name availability
int pos = matchUserName(un);
// Checks if found position is -1 then not found
if(pos == -1)
// Displays error message
System.out.println("Invalid User Name " + un);
// Otherwise found and displays the hint
else
System.out.println("Password hint: " +
facebookUser.get(pos).getHint());
}// End of method
// Method to list all user account from facebook
void listUser()
{
// Checks if array list size is 0 then display error message
if(facebookUser.size() == 0)
System.out.println("\n Empty list.");
// Otherwise not empty
else
{
System.out.print("\n\n ********** Facebook Users **********");
// Loops till number of users
for(int c = 0; c < facebookUser.size(); c++)
// Displays each object using toString() method
System.out.println(facebookUser.get(c));
}// End of else
}// End of method
// Method to writ all user account to file
void writeUser()
{
String filename = "facebookUser.txt";
// Serialization
try
{
// Saving of object in a file
FileOutputStream fileWrite = new FileOutputStream
(filename);
ObjectOutputStream outStream = new ObjectOutputStream
(fileWrite);
// Loops till number of users
for(int c = 0; c < facebookUser.size(); c++)
// Method for serialization of object
outStream.writeObject(facebookUser.get(c));
outStream.close();
fileWrite.close();
} // End of try block
catch (IOException ex)
{
System.out.println("Write error.");
} // End of catch block
}// End of method
// Method to read all user account from file
void readUser()
{
// Deserialization
try
{
String filename = "facebookUser.txt";
// Reading the object from a file
FileInputStream fileRead = new FileInputStream
(filename);
ObjectInputStream inStream = new ObjectInputStream
(fileRead);
User object = new User();
facebookUser.clear();
// Method for deserialization of object
for(int c = 0; c < facebookUser.size(); c++)
{
object = (User)inStream.readObject();
facebookUser.add(object);
}// End of for loop
inStream.close();
fileRead.close();
} // End of try block
catch (IOException ex)
{
System.out.println("Read error.");
} // End of catch block
catch (ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException" +
" is caught");
} // End of catch block
}// End of method
}// End of class Facebook
// Driver class definition
public class FacebookDriver
{
// Creates a Scanner class object
static Scanner sc = new Scanner(System.in);
// Method to display menu, accept user choice and returns it
static int menu()
{
// Displays menu
System.out.print("\n\n ********** Facebook Menu **********");
System.out.print("\n 1. List Users \n 2. Add a User" +
"\n 3. Delete a User \n 4. Get Password Hint " +
"\n 5. Quit");
System.out.print("\n What is your choice? ");
// Accepts and returns user choice
return sc.nextInt();
}// End of method
// main method definition
public static void main(String ss[])
{
// Creates an object of class Facebook
Facebook fUser = new Facebook();
// Checks if arraylist is empty then calls the
// method to read data from file
if(fUser.facebookUser.size() != 0)
fUser.readUser();
// Loops till user choice is not 5
do
{
// Calls the method to accept user choice
// Checks the returned user choice
// and calls the appropriate method
switch(menu())
{
case 1:
fUser.listUser();
break;
case 2:
fUser.add();
break;
case 3:
fUser.remove();
break;
case 4:
fUser.getHint();
break;
case 5:
fUser.writeUser();
System.exit(0);
default:
System.out.println("Invalid choice.");
}// End of switch case
}while(true); // End of do - while loop
}// End of main method
}// End of drive class
Sample Output:
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 1
Empty list.
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 2
Enter user name: Mohan
Enter password: myMohan
Enter password hint: Myname
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 2
Enter user name: Pyari
Enter password: myPyari
Enter password hint: Myname
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 1
********** Facebook Users **********
User Name: Mohan
Passowrd: myMohan
User Name: Pyari
Passowrd: myPyari
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 4
Enter user name: sunita
Invalid User Name sunita
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 4
Enter user name: Mohan
Password hint: Myname
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 2
Enter user name: Mohan
User name Mohanalready exist.
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 3
Enter user name: Ram
Invalid User Name Ram
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 3
Enter user name: Mohan
Enter password: MyName
Invalid Password. MyName
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 3
Enter user name: Mohan
Enter password: myMohan
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 1
********** Facebook Users **********
User Name: Pyari
Passowrd: myPyari
********** Facebook Menu **********
1. List Users
2. Add a User
3. Delete a User
4. Get Password Hint
5. Quit
What is your choice? 5
Get Answers For Free
Most questions answered within 1 hours.