Question

need help in Java Now we’ll continue our project by writing a Facebook class that contains an ArrayList of Facebook user...

need help in Java

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: list users add a user, delete a user, get password hint and 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.

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

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

0 1
Add a comment Improve this question Transcribed image text
Answer #1

//save it as FacebookTest.java


import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;

class FacebookUser implements Serializable{
   String Username;
   String Password;
   String Hint;
  
   FacebookUser(String u, String p, String h){
       Username = u;
       Password = p;
       Hint = h;              
   }  
  
   public String getUsername(){
       return Username;      
   }      
  
   public String getHint(){
       return Hint;      
   }      
  
   public String getPassword(){
       return Password;      
   }
  
}

class Facebook implements Serializable{
   ArrayList<FacebookUser> users;
   static Scanner sc = new Scanner(System.in);
  
   Facebook(){
       users = new ArrayList<FacebookUser>();              
   }
  
   public void listUsers(){
       if (users.size()>0)
       {
           System.out.println("\n---- User list ----- ");      
           for (int i = 0; i < users.size(); i++)
           {
               System.out.println( users.get(i).getUsername() );
           }                      
       }
       else
           System.out.println("No users present in list");          
   }
  
   public void addUser(){
       System.out.print("Enter username:");          
       String user = sc.nextLine();      
      
       //now check if new user is already exists in list
       for (int i = 0; i < users.size(); i++)
           {
               if (users.get(i).getUsername().equals(user))
               {
                   System.out.println( "User already exists..." );                  
                   return;   //if it is already present then return without adding
               }          
           }              
       System.out.print("Enter password:");          
           String pass = sc.nextLine();
       System.out.print("Enter hint:");          
           String hint = sc.nextLine();
          
       FacebookUser u = new FacebookUser(user, pass, hint); // create new user object
       users.add(u);   //add user to list
   }
  
   public void deleteUser(){
       System.out.print("Enter username to delete:");          
       String user = sc.nextLine();      
      
       //now check if user is exists in list
       for (int i = 0; i < users.size(); i++)
           {
               if (users.get(i).getUsername().equals(user)) // if user is found in list
               {                  
                   System.out.print("Enter password:"); //then ask password
                   String pass = sc.nextLine();
                   if ( users.get(i).getPassword().equals(pass) )   //if password matches
                   {
                       users.remove(users.get(i));   //remove user from list
                       System.out.println(user + " Deleted!");
                       return;                                  
                   }
                   else
                   {
                       //otherwise display error message
                       System.out.println("***** Wrong password, cannot delete: " + user);      
                       return;
                   }
               }          
           }                              
       System.out.println("User not found: " + user);          
   }
  
   public void getPassHint(){
       System.out.print("Enter username to get hint: ");
       String user = sc.nextLine();      
      
       //now check if user exists in list
       for (int i = 0; i < users.size(); i++)
           {
               if (users.get(i).getUsername().equals(user)) // if user is found in list
               {
                   System.out.println("Password hint is: " + users.get(i).getHint());                                  
                   return;
               }          
           }                              
       System.out.println("User not found! ");                                  
          
   }
}


public class FacebookTest {
  
   public static void main (String args[]) throws IOException{
      
       Scanner sc = new Scanner(System.in);
       Facebook fb = new Facebook();
      
       //output objects are required for serialization of object
       FileOutputStream fos = null;
       ObjectOutputStream oos = null;      
      
       //input objects are required for de-serialization of object
       FileInputStream fis = null;
       ObjectInputStream ois = null;          
      
       File f = new File("facebookData");   //facebookData is a file to store/serialize Facebook object
      
       if(f.exists()) {   // if file already exists then open it to read data
           fis = new FileInputStream("facebookData");
           ois = new ObjectInputStream(fis);          
           try
           {
               //de-serialize
               fb = (Facebook)ois.readObject();   // read stored object from file and assign to fb object
               //so that fb will contain previously saved data
               System.out.println("Users retrieved: " + fb.users.size());              
           }
           catch (ClassNotFoundException ex)
           {
               System.out.println("Exception during Deserialization: "+ex);              
           }
       }      
      
       fos = new FileOutputStream("facebookData");
       oos = new ObjectOutputStream(fos);              
       fis = new FileInputStream("facebookData");
       ois = new ObjectInputStream(fis);
      
       int choice = 0;
       while(choice!=5)   //if choice is not 5 quit then repeat loop to show menu as follows
       {
           System.out.println("\n----- Facebook -----");
           System.out.println("1. List users");
           System.out.println("2. Add users");
           System.out.println("3. Delete users");
           System.out.println("4. Get Password hint");
           System.out.println("5. Quit");
           System.out.print("Enter your choice: ");
           choice = sc.nextInt();   // get user choice
          
           switch(choice){
               case 1:
                       fb.listUsers();   // list all users
                       break;
               case 2:
                       fb.addUser();   // add user
                       System.out.println("count: " + fb.users.size());
                       break;
               case 3:
                       fb.deleteUser();   // delete user
                       break;
               case 4:
                       fb.getPassHint();   // get password hint for user
                       break;
               case 5:   // quit
                       oos.writeObject(fb); // serialize object data to file (save data to file)
                       oos.flush();   // flush actually writes data to file
                      
                       fos.close();   // close the file stream
                       oos.close();   // close the object stream
                      
                       fis.close();
                       ois.close();
                      
                       break;
               default:
                       System.out.println("*** Invalid choice, try again");
              
           }
          
       }
   }
}

/*   Output

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 1
No users present in list

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 2
Enter username:aa
Enter password:aa
Enter hint:aa
count: 1

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 2
Enter username:bb
Enter password:bb
Enter hint:bb
count: 2

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 3
Enter username to delete:cc
User not found: cc

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 1

---- User list -----
aa
bb

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 2
Enter username:cc
Enter password:cc
Enter hint:cc
count: 3

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 3
Enter username to delete:bb
Enter password:bbb
***** Wrong password, cannot delete: bb

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 3
Enter username to delete:bb
Enter password:bb
bb Deleted!

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 1

---- User list -----
aa
cc

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 4
Enter username to get hint: aa
Password hint is: aa

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 4
Enter username to get hint: bb
User not found!

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 4
Enter username to get hint: cc
Password hint is: cc

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 5

After program restart ------------------

Users retrieved: 2

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice: 1

---- User list -----
aa
cc

----- Facebook -----
1. List users
2. Add users
3. Delete users
4. Get Password hint
5. Quit
Enter your choice:


*/

Add a comment
Know the answer?
Add Answer to:
need help in Java Now we’ll continue our project by writing a Facebook class that contains an ArrayList of Facebook user...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA 04. Fourth Assignment – Facebook Friends We’ll continue with the Facebook application by enabling...

    IN JAVA 04. Fourth Assignment – Facebook Friends We’ll continue with the Facebook application by enabling the ability to friend and defriend people through our driver program. In particular, you will need to add four new menu options: friend someone, de-friend someone, list friends, and recommend new friends. Of course, you’ll need to add new methods to the Facebook class to perform these actions. The first three of these new functions (friending, de-friending, and listing friends) are similar to the...

  • Create a class called Login which contains a HashMap as a private attribute. It should also...

    Create a class called Login which contains a HashMap as a private attribute. It should also have an instance method called loadCredentials which acccepts the two arrays. It will load the Hash Map with key/value pairs based on the two arrays above. The userNames should be the keys and the passwords the values. private static String[] userNameArray = {"John", "Steve", "Bonnie", "Kylie", "Logan", "Robert"); private static String) passwordArray = {"1111", "2222", "3333", "4444", "5555", "6666"}; Create a login method in...

  • This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones...

    This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones in which one letter is substituted for another. Although their output looks impossible to read, they are easy to break because the relative frequencies of English letters are known. The Vigenere cipher improves upon this. They require a key word and the plain text to be encrypted. Create a Java application that uses: decision constructs looping constructs basic operations on an ArrayList of objects...

  • I) will create a rudimentary (and highly insecure) database for the storage of only the user...

    I) will create a rudimentary (and highly insecure) database for the storage of only the user generated identities. This program will have the following behaviors: (line break, 11 pt) ) - Prior to prompting for a new username, the existing list of usernames should be read and loaded to an array. 1 ) Read the list from "users.txt", a prompt is not necessary. Do not change this data file name "users.txt". 2 ) The existing list of usernames should be...

  • Create a python script to manage a user list that can be modified and saved to...

    Create a python script to manage a user list that can be modified and saved to a text file. Input text file consisting of pairs of usernames and passwords, separated by a colon (:) without any spaces User choice: (‘n’-new user account, ‘e’-edit existing user account, ‘d’- delete existing user account, ‘l’- list user accounts, ‘q’-quit) List of user accounts, error messages when appropriate Output text file consisting of pairs of username and passwords, separated by a colon (:) without...

  • User Profiles Write a program that reads in a series of customer information -- including name,...

    User Profiles Write a program that reads in a series of customer information -- including name, gender, phone, email and password -- from a file and stores the information in an ArrayList of User objects. Once the information has been stored, the program should welcome a user and prompt him or her for an email and password The program should then use a linearSearch method to determine whether the user whose name and password entered match those of any of...

  • Hashmap concept

    HashMap:  We can use a HashMap to store key value pairs.  Reminder that only reference types can be used as the key or value.  It you want to use a number as the key then an Integer data type could be used but not an int primitive data type.Create a class called Login which contains a HashMap as a private attribute.  It should also have an instance method called loadCredentials which accepts the two arrays.  It will load the HashMap...

  • Can anyone help me with rewriting my pseudocode? Below is the pseudocode, and after that is...

    Can anyone help me with rewriting my pseudocode? Below is the pseudocode, and after that is the completed program. Pseudocode: DISPLAY Login information PROMPT for username/password            output "Enter Username"            input userName          output "Enter password: "         input password //If successful, display information pertaining to the specific user, as well as prompt for logout    IF User type 'quit'      Exit Program    VALIDATE User Credentials   IF NOT Validated      Check number of Invalid Attempts         IF user attempts equal three THEN            DISPLAY error message...

  • 02. Second Assignment – The FacebookUser Class We’re going to make a small change to the...

    02. Second Assignment – The FacebookUser Class We’re going to make a small change to the UserAccount class from the last assignment by adding a new method: public abstract void getPasswordHelp(); This method is abstract because different types of user accounts may provide different types of help, such as providing a password hint that was entered by the user when the account was created or initiating a process to reset the password. Next we will create a subclass of UserAccount...

  • Looking for some help with this Java program I am totally lost on how to go about this. You have been approached by a local grocery store to write a program that will track the progress of their sale...

    Looking for some help with this Java program I am totally lost on how to go about this. You have been approached by a local grocery store to write a program that will track the progress of their sales people (SalesPerson.java) The information needed are: D Number integer Month 1 Sales Amount-Double Month 2 Sales Amount-Double Month 3 Sales Amount-Double Write a Java program that allows you to store the information of any salesperson up to 20 While the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT