Question: Assignment 6 Creating Lists Currently in our Facebook application, the first option in the driver program lists the users in some arbitrary order. In this

Assignment 6 Creating Lists Currently in our Facebook application, the first option in the driver program lists the users in some arbitrary order. In this assignment you will split that into two options one to list users alphabetically by username and the other to list them based on the number of friends they have (from most to least). When you list users alphabetically, list them from smallest to largest and ignore capitalization. For example, Alice should come before Bob who should come before Charlie. In addition, the Recommend friends options currently lists the recommendations in an arbitrary order. Change this so that the recommendations are listed according to the number of friends each person already has, from most to least. The menu displayed to the user will now look like the one below, and you will be implementing options 1 and 2 and tweaking the implementation of option 9. (The code for the rest of the options will stay the same as it was for previous assignments.) 1. List users alphabetically 2. List users by number of friends 3. Add a user 4. Delete a user 5. Get password hint 6. Add a friend 7. Remove a friend 8. List friends 9. Recommend friends 10.Quit There are several ways to accomplish the assignment, and you are free to use whichever approach you feel most comfortable with. If youre at a loss for ideas, consider that the Collections class has a sort method that will take any List and a Comparator and sort the objects accordingly. This is described in chapter 22 of your textbook. Comparators are also discussed in the video for this topic. 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 will list users based on their username The username listing is lexicographic order, from smallest to largest The username listing is case insensitive The program will list users based on the number of friends they have The listing based on number of friends is ordered from most to least Friend recommendations are listed in order of the number of friends each person already has, from most to least The program compiles The program runs The program is clearly written and uses standard coding conventions

I am having trouble sorting by number of friends.

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;

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();

}

package userAccount;

import java.io.Serializable;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

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){

displayNoUsers();

}

else{

try{

Collections.sort(users);

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);

/* Collections.sort(users); //, new Facebook());*/

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(){

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.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);

}

}

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!