Question

The Facebook Class Now we’ll continue our project by writing a Facebook class that contains an...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all user information from a given input file. The input file contains information of a user in following order: username, first name, last name, password, account number and account balance. Information is separated with ‘|’. o username is a unique information, so no two users will have same username. Sample input file: Username eaglebank has password 123456, account number of BB12 and balance of $1000....
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
Write a program in Java that: 1. will prompt user with a menu that contains options...
Write a program in Java that: 1. will prompt user with a menu that contains options to: a. Add a To-Do Item to a todo list. A To-Do Item contains: i. An arbitrary reference number (4; 44,004; etc.) ii. A text description of the item ("Pick up dry cleaning", etc.) iii. A priority level (1, 2, 3, 4, or 5) b. View an item in the list (based on its reference number) c. Delete an item from the list (based...
Here is the code I am supposed to Analyze: // If we want to use the...
Here is the code I am supposed to Analyze: // If we want to use the Scanner class (type) to get user input, we need // to import the following Java package that includes the Scanner class // definitions. We do not have to add an import statement to use the // print() or println() methods of the System object, because they // are built in. import java.util.Scanner; public class PRG420Week1_AnalyzeAssignment { /* The main() method you see below is...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It must inherit from Professor, but add a new String attribute named term. (The term must be a six digit string, with the year followed by two digits to represent the term: "01" for Winter, "05" for Spring, and "09" for Fall. Thus, 201705 represents the Spring 2017 term. Override print so that it prints CAS data like: McGarrity, Ivan Department: English Term: 201705 Write...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Objective The Final Project aims to demonstrate your ability to analyze data from a big database,...
Objective The Final Project aims to demonstrate your ability to analyze data from a big database, exercise use of arrays of objects, external classes, processing files and user interaction. For this project, you will design and implement a program that analyzes baby name popularities in data provided by the Social Security Administration. Every 10 years, the data gives the 1,000 most popular boy and girl names for kids born in the United States. The data can be boiled down to...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT