Question
The assignment Assignment 7 Liking Things In this assignment you will add the ability to like things to your Facebook application. Specifically, there should now
The assignment Assignment 7 Liking Things In this assignment you will add the ability to like things to your Facebook application. Specifically, there should now be two new menu options: one for a user to indicate that they like something (any String), and another to list all of the liked things in alphabetical order. When a user chooses the like menu option, they should be prompted for their username and password. If these are valid, the program should prompt for the String indicating what the user likes. Update the data structure and then provide a response to the user indicating that their like has been noted (e.g. CommanderCool likes puppies.) If a user has already liked something once, liking it again should have no effect. Now that we have learned about a variety of data structures available in Java, you need to become comfortable with choosing the appropriate data structure for a particular task. With this in mind, you are required to have a comment somewhere in your Facebook class that indicates which data structure you have used to store the likes information and explains why you have chosen it. Begin this comment with the word EXPLANATION (in all caps) so that I can search for it when grading your work. You will be graded according to the following rubric (each item is worth one point): The driver menu contains the two new options The program prompts for username, password, and String to like The number of likes is updated when username and password are valid Liking something more than once has no effect The choice of data structure to store the likes information is logical and well-defended in a comment beginning with EXPLANATION The program can list the likes (String and number) Likes are listed in alphabetical order The program compiles The program runs The program is clearly written and uses standard coding My code package userAccount; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Scanner; public class Driver implements Serializable{ public static void main(String[] args) throws IOException, ClassNotFoundException{ Facebook facebook = new Facebook(); //read facebook.users from file and add to accounts File file = new File("FacebookUsers.dat"); if (file.exists()){ ObjectInputStream ois = new ObjectInputStream( new BufferedInputStream(new FileInputStream(file))); try { facebook = (Facebook) ois.readObject(); } catch (FileNotFoundException e) { System.out.println("Could not find " + "'" + file + "'"); } catch (IOException e) { System.out.println("There was an IOException error."); } ois.close(); } else{ facebook = new Facebook(); } Scanner input = new Scanner(System.in); boolean valid = false; int option = 0; boolean quit = false; do{ do { valid = true; //display menu options System.out.println("Menu:"); System.out.println("1. List users alphabetically"); System.out.println("2. List users by number of friends"); System.out.println("3. Add a user"); System.out.println("4. Delete a user"); System.out.println("5. Get password hint"); System.out.println("6. Add friends"); System.out.println("7. Remove friends"); System.out.println("8. List friends"); System.out.println("9. Recommended friends"); System.out.println("10. Quit"); System.out.print ("Please choose one of the options: "); //read in option option = input.nextInt(); //invalid check if (option <= 10 && option >= 1){ valid = true; } else{ System.out.println("\tPlease select a valid option."); System.out.println(""); valid = false; } } while (!valid); switch (option){ case 1://List users alphabetically facebook.getUsersAtoZ(); break; case 2://List users by number of friends facebook.getUsersBynumberofFriends(); break; case 3://Add a user facebook.addUser(); break; case 4://Delete a user facebook.deleteUser(); break; case 5://Get password hint facebook.getUsersPasswordHelp(); break; case 6://Add friends facebook.friend(); break; case 7://Remove friends facebook.defriend(); break; case 8://List friends facebook.listFriends(); break; case 9://Recommended friends facebook.getRecommendedFriends(); break; case 10://Quit System.out.println("Goodbye."); quit = true; break; } System.out.println(""); } while (quit == false); input.close(); ObjectOutputStream oos = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("Users.dat"))); try { oos.writeObject(facebook); } catch (FileNotFoundException e) { System.out.println("Could not find " + file + "."); } catch (IOException e) { System.out.println("There was an IOException error."); } oos.close(); } package userAccount; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; import javax.swing.JList; public class Facebook implements Comparable, Cloneable, Serializable{ private static final long serialVersionUID = 409677907447106034L; transient Scanner input; ArrayList users = new ArrayList<>(); ArrayList friends = new ArrayList<>(); ArrayList mostFriends = new ArrayList<>(); ArrayList recommendedFriends; void getUsersAtoZ(){ System.out.println(""); if (users.size() == 0){ System.out.println("User does not have any friends"); } else{ try{ Collections.sort(users, new FacebookComparator()); ArrayList displayUsers = new ArrayList<>();; for (FacebookUser user: users){ displayUsers.add(user); } System.out.println("\t" + displayUsers); } catch(Exception e){ System.out.println("\tThere was an error." ); } } } void getUsersBynumberofFriends() { System.out.println(""); if (users.size() == 0){ System.out.println("User does not have any friends"); } else{ try{ Collections.sort(users, new FacebookComparator()); ArrayList displayUsers = new ArrayList<>();; for (FacebookUser user: users){ displayUsers.add(user); } System.out.println("\t" + displayUsers); } catch(Exception e){ System.out.println("\tThere was an error." ); } } } void addUser(){ input = new Scanner(System.in); String newUser; boolean breakLoop = false; boolean makeUser = false; do{ System.out.println(""); displayInstructions("Enter a username"); System.out.print("\tUsername: "); newUser = input.nextLine(); int newUserIndex = findUser(newUser); if (newUserIndex >= 0){//username already exists System.out.println("\t" + newUser + " already exists."); breakLoop = false; } else if (newUserIndex == -1){//username doesn't exist breakLoop = true; makeUser = true; } else if (newUserIndex == -2){//quit displayQuit(); breakLoop = true; } else if (newUserIndex == -3){// no Facebook users makeUser = true; breakLoop = true; } else if (newUserIndex == -4){// not valid username displayEnterAValidName(); breakLoop = false; } } while (breakLoop == false);//exits when newUserName is a correct username if (makeUser == true){ System.out.print("\tPassword: "); String PW = input.nextLine(); System.out.print("\tPassword hint: "); String PWHint = input.nextLine(); FacebookUser createdUser = new FacebookUser(newUser, PW, PWHint);//creates the user users.add(createdUser);//adds user to users list } } void deleteUser(){ if (users.size() == 0){ System.out.println(""); displayNoUsers(); } else{ int login = login(); if (login >= 0){ users.remove(login); System.out.println("\tYour user has successfully been deleted."); } else if (login == -2){ displayQuit(); } } } void getUsersPasswordHelp(){ if(users.size() == 0){//no users have been created System.out.println(""); displayNoUsers(); } else{ input = new Scanner(System.in); String helpUser; boolean breakLoop = false; do{ System.out.println(""); displayInstructions("Enter Username to get password help"); System.out.print("\tUsername: "); helpUser = input.nextLine(); int helpUserIndex = findUser(helpUser); if (helpUserIndex >= 0){//username exists users.get(helpUserIndex).getPasswordHelp(); breakLoop = true; } else if (helpUserIndex == -1){//username doesn't exist displayUserNotFound(helpUser); breakLoop = false; } else if (helpUserIndex == -2){//quit displayQuit(); breakLoop = true; } else if (helpUserIndex == -4){ displayEnterAValidName(); } } while (breakLoop == false);//exits when helpUserName is a correct username } } void friend(){ if (users.size() == 0){ System.out.println(""); displayNoUsers(); } else{ int userIndex = login(); if (userIndex >= 0){ String newFriend; int newFriendUserIndex; boolean breakLoop = false; do{ System.out.println(""); displayInstructions("Enter friend to be added"); System.out.print("\tUsername: "); newFriend = input.nextLine(); newFriendUserIndex = findUser(newFriend); if (newFriendUserIndex >= 0){//username exists users.get(userIndex).friend(users.get(newFriendUserIndex)); breakLoop = true; } else if (newFriendUserIndex == -1){//username doesn't exist displayUserNotFound(newFriend); breakLoop = false; } else if (newFriendUserIndex == -2){//quit displayQuit(); breakLoop = true; } else if (newFriendUserIndex == -4){ displayEnterAValidName(); breakLoop = false; } } while (breakLoop == false);//exits when newUserName is a correct username } else if (userIndex == -2){ displayQuit(); } } } void defriend(){ if (users.size() == 0){ System.out.println(""); displayNoUsers(); } else{ int userIndex = login(); if (userIndex >= 0){ String formerFriend; int formerFriendUserIndex; boolean breakLoop = false; do{ System.out.println(""); displayInstructions("Enter friend to be removed"); System.out.print("\tUsername: "); formerFriend = input.nextLine(); formerFriendUserIndex = findUser(formerFriend); if (formerFriendUserIndex >= 0){//username exists users.get(userIndex).defriend(users.get(formerFriendUserIndex)); breakLoop = true; } else if (formerFriendUserIndex == -1){//username doesn't exist displayUserNotFound(formerFriend); breakLoop = false; } else if (formerFriendUserIndex == -2){//quit displayQuit(); breakLoop = true; } else if (formerFriendUserIndex == -4){ displayEnterAValidName(); breakLoop = false; } } while (breakLoop == false);//exits when newUserName is a correct username } else if (userIndex == -2){ displayQuit(); } } } void listFriends(){ if (users.size() == 0){ System.out.println(""); displayNoUsers(); } else{ input = new Scanner(System.in); int userIndex = login(); if (userIndex >= 0){//successful login ArrayList friends = users.get(userIndex).getFriends(); if (friends != null){// you do have friends System.out.println("\t" + users.get(userIndex).getFriends()); } else{//no friends System.out.println("\tYou don't have any friends."); } } else if (userIndex == -2){//quit displayQuit(); } } } public int findUser(String username){ //returns i if user exists, -1 if user doesn't exist, -2 if quit was chosen, -4 if "" int searchedUsers = 0; if (username.equals("q")){ return -2; } else if (username.equals("")){ return -4; } for (int i = 0; i < users.size(); i++){ if (username.equals(users.get(i).toString())){ return i; } else{ searchedUsers++; } if (searchedUsers == users.size() && users.size() != 0){ return -1; } } return -3; } public int login(){ //returns i if correct username & pw, -2 if quit input = new Scanner(System.in); String username; boolean breakLoop = false; if(users.size() == 0){//no users have been created System.out.println(""); displayNoUsers(); return -2; } else{ do{ int searchedUsers = 0; System.out.println(""); displayInstructions("Login"); System.out.print("\tUsername: "); username = input.nextLine(); if (username.equals("")){//No name was entered displayEnterAValidName(); breakLoop = false; } else if (username.equals("q")){//quit option was chosen return -2; } else{//there are existing users for (int i = 0; i < users.size(); i++){ if (username.equals(users.get(i).toString())){//username exists System.out.print("\tPassword: "); String password = input.nextLine(); if (users.get(i).checkPassword(password)){ return i; } else{ System.out.println("\tPassword incorrect."); breakLoop = false; } } else{//username does not equal users.get(j)'s username searchedUsers++; breakLoop = false; } if (searchedUsers == users.size()){ breakLoop = false; System.out.println("\t" + username + " is not a Facebook user."); } } } } while (breakLoop == false);//exits when username is a user } return -1;//Should never get here } public void getRecommendedFriends(){ boolean firsttime = true; if (firsttime) { System.out.println(""); if (users.size() == 0){ System.out.println("User does not have any friends"); } else{ try{ Collections.sort(users, new FacebookComparator()); ArrayList displayUsers = new ArrayList<>();; for (FacebookUser user: users){ displayUsers.add(user); } System.out.println("\t" + displayUsers); } catch(Exception e){ System.out.println("\tThere was an error." ); } } } firsttime = false; int userIndex = login(); if (userIndex >= 0){ recommendedFriends = new ArrayList<>(); System.out.println(""); getRecommendedFriends(users.get(userIndex)); if (recommendedFriends.size() == 0){ System.out.println("\t" + users.get(userIndex).toString() + " does not recommend any friends."); } else{ Collections.sort(recommendedFriends); ArrayList displayrecommendedFriends = new ArrayList<>();; for (FacebookUser friend: recommendedFriends){ displayrecommendedFriends.add(friend); } System.out.println("\t" + displayrecommendedFriends); } } } public void getRecommendedFriends(FacebookUser user){ if (user.friends.size() == 0){ return; } else{ for (int i = 0; i < user.friends.size(); i++){ if (!recommendedFriends.contains(user.friends.get(i))){ recommendedFriends.add(user.friends.get(i)); getRecommendedFriends(user.friends.get(i)); } } } } @Override public int compareTo(FacebookUser o) { if (toString().compareToIgnoreCase(o.toString()) != 0) { return toString().compareToIgnoreCase(o.toString()); } return 0; } void displayQuit(){ System.out.println("\tReturning to menu..."); } void displayNoUsers(){ System.out.println("\tThere are no Facebook users."); } void displayUserNotFound(String username){ System.out.println("\t" + username + " was not found."); } void displayEnterAValidName(){ System.out.println("\tPlease enter a valid username."); } void displayInstructions(String prompt){ System.out.println("\t" + prompt + ", or enter 'q' to return to menu."); } } package userAccount; import java.util.*; //Create class public class FacebookComparator implements Comparator { //Define method to compare two users public int compare(FacebookUser user1, FacebookUser user2) { //Declare variables int ka, kb; ka = 0; kb = 0; //Check condition if (user1.getFriends() != null) { //Get number of friends for user1 ka = user1.getFriends().size(); } //Check condition if (user2.getFriends() != null) { //Get number of friends for user1 kb = user2.getFriends().size(); } //Return value return kb - ka; } } package userAccount; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; public class FacebookUser extends UserAccount implements Comparable, Cloneable, Serializable{ private static final long serialVersionUID = -3401368044041692793L; private String passwordHint; ArrayList friends = new ArrayList<>(); public FacebookUser(String username, String password, String passwordHint) { super(username, password); this.passwordHint = passwordHint; } void setPasswordHint(String hint){ this.passwordHint = hint; } void friend(FacebookUser newFriend){ if (friends.size() == 0){//no friends friends.add(newFriend); System.out.println("\t" + newFriend.toString() + " is now your friend.");//add the friend to newUser's friend list } else{ int j = 0; int friendsSearched = 0; boolean endFriendSearch = false; do{//friends do-while if (newFriend.equals(friends.get(j))){//already your friend System.out.println("\t" + newFriend.toString() + " is already your friend."); endFriendSearch = true;//tells friends do-while to end } else{//searched user was not friends.get(j) friendsSearched++;//add to the total # of friends searched j++;//increase j to search next friend } if (friendsSearched == friends.size()){//searched through all friends and the user was not in friends list friends.add(newFriend);//so we add to friends list System.out.println("\t" + newFriend.toString() + " is now your friend."); endFriendSearch = true;//tells friend's do-while to end } } while(endFriendSearch == false);//will end when endFriendSearch is true } } void defriend(FacebookUser formerFriend){ if (friends.size() == 0){//no friends System.out.println("\tYou have no friends."); } else{ int j = 0; int friendsSearched = 0; boolean endFriendSearch = false; do{//friends do-while if (formerFriend.equals(friends.get(j))){//already your friend friends.remove(formerFriend);//removes friends.get(j) from newUser's friend list System.out.println("\t" + formerFriend.toString() + " has been removed from your friends."); endFriendSearch = true;//tells friends do-while to end } else{//searched user was not friends.get(j) friendsSearched++;//add to the total # of friends searched j++;//increase j to search next friend } if (friendsSearched == friends.size()){//searched through all friends and the user was not in friends list System.out.println("\t" + formerFriend.toString() + " is not your friend."); endFriendSearch = true;//tells friend's do-while to end } } while(endFriendSearch == false);//will end when endFriendSearch is true } } ArrayList getFriends(){ if (friends.size() == 0){ return null; } else{ Collections.sort(friends); ArrayList displayFriends = new ArrayList<>();; for (FacebookUser friend: friends){ displayFriends.add(friend); } return displayFriends; } } @Override public int compareTo(FacebookUser o) { if (toString().compareToIgnoreCase(o.toString()) != 0) { return toString().compareToIgnoreCase(o.toString()); } return 0; } @Override public void getPasswordHelp() { System.out.println("\tHint: " + passwordHint); } } package userAccount; import java.io.Serializable; public abstract class UserAccount implements Serializable{ private String username; private String password; private boolean active; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; UserAccount other = (UserAccount) obj; if (username == null) { if (other.username != null) return false; } else if (!username.equals(other.username)) return false; return true; } @Override public String toString() { return username; } UserAccount(String username, String password){ this.username = username; this.password = password; this.active = true; } public boolean checkPassword(String password){ if (password.equals(this.password)) return true; else return false; } public void deactivateAccount(){ active = false; } public abstract void getPasswordHelp(); } What is the best solution?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started