Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*WHen adding BFF objects to arrayList, add them in alpha order, by first name. p

ID: 3741936 • Letter: #

Question

*WHen adding BFF objects to arrayList, add them in alpha order, by first name.

package bestfriend;

import java.util.*;

public class BestFriend {

private static int number = 0;

  

private int iD;

  

private String firstName;

  

private String lastName;

  

private String nickName;

  

private String cellPhoneNumber;

public BestFriend(String aFirstName, String aLastName, String aNickName, String aCellPhoneNumber) {

  

firstName = aFirstName;

  

lastName = aLastName;

  

nickName = aNickName;

  

cellPhoneNumber = aCellPhoneNumber;

  

number++;

  

iD = number;

}

public String toString() {

  

return iD + ". " + nickName + " " + firstName + " " + lastName + " " + cellPhoneNumber + " ";

  

}

//Create the set methods (setters)

//Create the get methods (getters)

  

void setFirstName(String first) {

  

firstName = first;

  

}

void setLastName(String last) {

lastName = last;

}

void setNickName(String nick) {

nickName = nick;

}

void setCellPhoneNumber(String phone) {

cellPhoneNumber = phone;

}

public boolean equals(Object another) {

  

if (another instanceof BestFriend) {

BestFriend anotherBFF = (BestFriend) another;

if (firstName.equalsIgnoreCase(anotherBFF.firstName)

&& lastName.equalsIgnoreCase(anotherBFF.lastName))

{

return true;

}

}

return false;

}

}

package bestfriend;

import java.util.*;

class BestFriendSimulation {

private static ArrayList<BestFriend> best;

public static void displayMenu(){

String MainMenu = "Please Enter Option Number Of your Choice" + " " + "1.) Add a BestFriend to the arrayList called myBestFriends; " + " " + "2.) Change a BestFriend in the arrayList;" + " " + "3.) Remove a BestFriend from the arrayList;" + " " + "4.) Display all the objects in the myBestFriends arrayList." + " " + "5.) Exit " + " ";

System.out.println(MainMenu);

}

public static void main(String arg[]) {

Scanner scanner = new Scanner(System.in);

BestFriendHelper helper = new BestFriendHelper();

int option = 0;

while(true){

displayMenu();

option = scanner.nextInt();

if(option <=0 || option >5){

System.out.println("Invalid Input! Enter input between 1 to 5");

continue;

}

if(option == 1){

helper.add();

}

else if(option == 2){

helper.change();

}else if(option == 3){

helper.remove();

}else if(option == 4){

helper.display();

}else if(option == 5){

System.out.println("Exiting");

break;

}

}

}

}

package bestfriend;

import java.util.ArrayList;

import java.util.Scanner;

public class BestFriendHelper {

private ArrayList<BestFriend> best;

public BestFriendHelper(){

best = new ArrayList<BestFriend>();

}

public void add(){

Scanner scanner = new Scanner(System.in);

System.out.println("Please Enter First Name");

String first = scanner.next();

System.out.println("Please Enter Last Name");

String last = scanner.next();

System.out.println("Please Enter Nick Name");

String nick = scanner.next();

System.out.println("Please Enter Cell Phone Number");

String number = scanner.next();

best.add(new BestFriend(first, last, nick, number));

}

public void change(){

Scanner scanner = new Scanner(System.in);

int AS = 0;

System.out.println("Enter First Name Of Friend You Want To Change");

String first = scanner.next();

System.out.println("Enter Last Name Of Friend You Want To Change");

String last = scanner.next();

BestFriend BF = new BestFriend(first, last, "", "");

AS = best.size();

if (AS == 0) {

System.out.print("List is Empty");

return;

}

for (int i = 0; i < AS; i++) {

if (best.get(i).equals(BF)) {

System.out.println("Please Enter New First Name");

first = scanner.next();

System.out.println("Please Enter New Last Name");

last = scanner.next();

System.out.println("Please Enter New Nick Name");

String nick = scanner.next();

System.out.println("Please Enter New Cell Phone Number");

String number = scanner.next();

best.get(i).setFirstName(first);

best.get(i).setLastName(last);

best.get(i).setNickName(nick);

best.get(i).setCellPhoneNumber(number);

break;

} else if (i == AS - 1) {

System.out.println("No Such Friend Exists");

break;

}

}

}

public void remove(){

int AS = 0;

Scanner scanner = new Scanner(System.in);

System.out.println("Enter First Name Of Friend You Want To Remove");

String first = scanner.next();

System.out.println("Enter Last Name Of Friend You Want To Remove");

String last = scanner.next();

BestFriend BF = new BestFriend(first, last, "", "");

AS = best.size();

if (AS == 0) {

System.out.println("List is Empty");

return;

}

for (int i = 0; i < AS; i++) {

if (best.get(i).equals(BF)) {

best.remove(i);

break;

} else if (i == AS - 1) {

System.out.println("No Such Friend Exists");

break;

}

}

}

public void display(){

int AS = best.size();

if (AS == 0) {

System.out.println("List is Empty");

return;

}

for (int i = 0; i < AS; i++) {

System.out.println(best.get(i).toString());

}

}

}

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

Note: I have updated BestFriendHelper.java & BestFriend.java (added getter methods) only, no change is made to BestFriendSimulation.java. Also if you want to keep the array list in sorted order always, I suggest you to implement a sorting function, so that it can be called after adding & updating objects . The below code works well, but if you update (change) someone's firstname, then the order will be lost.

// BestFriendHelper.java

import java.util.ArrayList;

import java.util.Scanner;

public class BestFriendHelper {

      private ArrayList<BestFriend> best;

      public BestFriendHelper() {

            best = new ArrayList<BestFriend>();

      }

      public void add() {

            Scanner scanner = new Scanner(System.in);

            System.out.println("Please Enter First Name");

            String first = scanner.next();

            System.out.println("Please Enter Last Name");

            String last = scanner.next();

            System.out.println("Please Enter Nick Name");

            String nick = scanner.next();

            System.out.println("Please Enter Cell Phone Number");

            String number = scanner.next();

            /**

            * creating a BestFriend object

            */

            BestFriend b = new BestFriend(first, last, nick, number);

            /**

            * Adding best friend in alphabetical order of first name. Note that if

            * you want to keep the array list in sorted order always, I suggest you

            * to implement a sorting function, so that it can be called after

            * adding & updating objects . The below code works well, but if you

            * update (change) someone's firstname, then the order will be lost.

            */

            if (best.size() == 0) {

                  // adding as the first object

                  best.add(b);

            } else if (b.getFirstName().compareTo(best.get(0).getFirstName()) < 0) {

                  /**

                  * Adding before current first object

                  */

                  best.add(0, b);

            } else {

                  // a flag to denote if the object is added or not

                  boolean added = false;

                  // looping through all objects

                  for (int i = 0; i < best.size() - 1; i++) {

                        /**

                        * checking if the object can be added to the middle of current

                        * pair of elements (between positions i and i+1)

                        */

                        if (b.getFirstName().compareTo(best.get(i).getFirstName()) >= 0

                                    && b.getFirstName().compareTo(

                                                best.get(i + 1).getFirstName()) < 0) {

                              best.add(i + 1, b);

                              // added to the middle

                              added = true;

                              break;

                        }

                  }

                  // finally, if not added, adding to end

                  if (!added) {

                        best.add(b);

                  }

            }

      }

      public void change() {

            Scanner scanner = new Scanner(System.in);

            int AS = 0;

            System.out.println("Enter First Name Of Friend You Want To Change");

            String first = scanner.next();

            System.out.println("Enter Last Name Of Friend You Want To Change");

            String last = scanner.next();

            BestFriend BF = new BestFriend(first, last, "", "");

            AS = best.size();

            if (AS == 0) {

                  System.out.print("List is Empty");

                  return;

            }

            for (int i = 0; i < AS; i++) {

                  if (best.get(i).equals(BF)) {

                        System.out.println("Please Enter New First Name");

                        first = scanner.next();

                        System.out.println("Please Enter New Last Name");

                        last = scanner.next();

                        System.out.println("Please Enter New Nick Name");

                        String nick = scanner.next();

                        System.out.println("Please Enter New Cell Phone Number");

                        String number = scanner.next();

                        best.get(i).setFirstName(first);

                        best.get(i).setLastName(last);

                        best.get(i).setNickName(nick);

                        best.get(i).setCellPhoneNumber(number);

                        break;

                  } else if (i == AS - 1) {

                        System.out.println("No Such Friend Exists");

                        break;

                  }

            }

      }

      public void remove() {

            int AS = 0;

            Scanner scanner = new Scanner(System.in);

            System.out.println("Enter First Name Of Friend You Want To Remove");

            String first = scanner.next();

            System.out.println("Enter Last Name Of Friend You Want To Remove");

            String last = scanner.next();

            BestFriend BF = new BestFriend(first, last, "", "");

            AS = best.size();

            if (AS == 0) {

                  System.out.println("List is Empty");

                  return;

            }

            for (int i = 0; i < AS; i++) {

                  if (best.get(i).equals(BF)) {

                        best.remove(i);

                        break;

                  } else if (i == AS - 1) {

                        System.out.println("No Such Friend Exists");

                        break;

                  }

            }

      }

      public void display() {

            int AS = best.size();

            if (AS == 0) {

                  System.out.println("List is Empty");

                  return;

            }

            for (int i = 0; i < AS; i++) {

                  System.out.println(best.get(i).toString());

            }

      }

}

// BestFriend.java

import java.util.*;

public class BestFriend {

      private static int number = 0;

      private int iD;

      private String firstName;

      private String lastName;

      private String nickName;

      private String cellPhoneNumber;

      public BestFriend(String aFirstName, String aLastName, String aNickName,

                  String aCellPhoneNumber) {

            firstName = aFirstName;

            lastName = aLastName;

            nickName = aNickName;

            cellPhoneNumber = aCellPhoneNumber;

            number++;

            iD = number;

      }

      public String toString() {

            return iD + ". " + nickName + " " + firstName + " " + lastName + " "

                        + cellPhoneNumber + " ";

      }

      // Create the set methods (setters)

      // Create the get methods (getters)

      void setFirstName(String first) {

            firstName = first;

      }

      void setLastName(String last) {

            lastName = last;

      }

      void setNickName(String nick) {

            nickName = nick;

      }

      void setCellPhoneNumber(String phone) {

            cellPhoneNumber = phone;

      }

      public boolean equals(Object another) {

            if (another instanceof BestFriend) {

                  BestFriend anotherBFF = (BestFriend) another;

                  if (firstName.equalsIgnoreCase(anotherBFF.firstName)

                  && lastName.equalsIgnoreCase(anotherBFF.lastName))

                  {

                        return true;

                  }

            }

            return false;

      }

     

      //getter methods

     

      public static int getNumber() {

            return number;

      }

      public int getiD() {

            return iD;

      }

      public String getFirstName() {

            return firstName;

      }

      public String getLastName() {

            return lastName;

      }

      public String getNickName() {

            return nickName;

      }

      public String getCellPhoneNumber() {

            return cellPhoneNumber;

      }

     

     

}

/*OUTPUT*/

Please Enter Option Number Of your Choice

1.) Add a BestFriend to the arrayList called myBestFriends;

2.) Change a BestFriend in the arrayList;

3.) Remove a BestFriend from the arrayList;

4.) Display all the objects in the myBestFriends arrayList.

5.) Exit

1

Please Enter First Name

Zayn

Please Enter Last Name

Onio

Please Enter Nick Name

Zee

Please Enter Cell Phone Number

112233

Please Enter Option Number Of your Choice

1.) Add a BestFriend to the arrayList called myBestFriends;

2.) Change a BestFriend in the arrayList;

3.) Remove a BestFriend from the arrayList;

4.) Display all the objects in the myBestFriends arrayList.

5.) Exit

4

1. Zee Zayn Onio 112233

Please Enter Option Number Of your Choice

1.) Add a BestFriend to the arrayList called myBestFriends;

2.) Change a BestFriend in the arrayList;

3.) Remove a BestFriend from the arrayList;

4.) Display all the objects in the myBestFriends arrayList.

5.) Exit

1

Please Enter First Name

Abraham

Please Enter Last Name

Johnson

Please Enter Nick Name

Abe

Please Enter Cell Phone Number

12345

Please Enter Option Number Of your Choice

1.) Add a BestFriend to the arrayList called myBestFriends;

2.) Change a BestFriend in the arrayList;

3.) Remove a BestFriend from the arrayList;

4.) Display all the objects in the myBestFriends arrayList.

5.) Exit

4

2. Abe Abraham Johnson 12345

1. Zee Zayn Onio 112233

Please Enter Option Number Of your Choice

1.) Add a BestFriend to the arrayList called myBestFriends;

2.) Change a BestFriend in the arrayList;

3.) Remove a BestFriend from the arrayList;

4.) Display all the objects in the myBestFriends arrayList.

5.) Exit

1

Please Enter First Name

Cooligan

Please Enter Last Name

Michael

Please Enter Nick Name

Coo

Please Enter Cell Phone Number

101010

Please Enter Option Number Of your Choice

1.) Add a BestFriend to the arrayList called myBestFriends;

2.) Change a BestFriend in the arrayList;

3.) Remove a BestFriend from the arrayList;

4.) Display all the objects in the myBestFriends arrayList.

5.) Exit

4

2. Abe Abraham Johnson 12345

3. Coo Cooligan Michael 101010

1. Zee Zayn Onio 112233

Please Enter Option Number Of your Choice

1.) Add a BestFriend to the arrayList called myBestFriends;

2.) Change a BestFriend in the arrayList;

3.) Remove a BestFriend from the arrayList;

4.) Display all the objects in the myBestFriends arrayList.

5.) Exit

1

Please Enter First Name

David

Please Enter Last Name

Fincher

Please Enter Nick Name

Dave

Please Enter Cell Phone Number

102030

Please Enter Option Number Of your Choice

1.) Add a BestFriend to the arrayList called myBestFriends;

2.) Change a BestFriend in the arrayList;

3.) Remove a BestFriend from the arrayList;

4.) Display all the objects in the myBestFriends arrayList.

5.) Exit

4

2. Abe Abraham Johnson 12345

3. Coo Cooligan Michael 101010

4. Dave David Fincher 102030

1. Zee Zayn Onio 112233

Please Enter Option Number Of your Choice

1.) Add a BestFriend to the arrayList called myBestFriends;

2.) Change a BestFriend in the arrayList;

3.) Remove a BestFriend from the arrayList;

4.) Display all the objects in the myBestFriends arrayList.

5.) Exit

5

Exiting