Question: **IN JAVA CODE** In a BestFriendSimulation driver class, define and initialize a reference variable called myBestFriends referencing an ArrayList ofBestFriend objects. Then, create a menu

**IN JAVA CODE**

In a BestFriendSimulation driver class, define and initialize a reference variable called myBestFriends referencing an ArrayList ofBestFriend objects. Then, create a menu that will continue to display itself, and process the requests of the user, until the user requests to exit the menu (loop). The menu should have 5 menu options:

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

For each of these menu options, be sure to prompt the user for the necessary information such as BestFriend's first name, last name, nickname, and cell phone number.

Hint: In order to be able to change a BestFriend's information or remove a BestFriend from the arrayList, you will have to create a loop that will search the myBestFriends arrayList from beginning to end, and store the index position of the BestFriend that will be updated or removed. If the desired BestFriend is not found, a -1 will be returned from the search, or a not found flag will be set. If the BestFriend is found, use that index position to either change or remove that object from the myBestFriends arrayList. Make sure you test each of your menu options, and then select the option that prints the table immediately afterwards, to ensure the PhoneBook looks correctly.

1. Create the BestFriend Domain Class Define and/or instantiate the private static and non-static fields.

Create the constructor

public class BestFriend

{ private static int friendNumber = 0;

private int friendIdNumber;

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; friendNumber++;

friendIdNumber = friendNumber;

}

public String toString( )

{

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

}

Create the set methods (setters). Create the get methods (getters).

2. Create the BestFriendSimulation Driver Class: Instantiate an arrayList called myBFFs Create a Do While loop to create a menu of 5 choices (add, change, remove, display, exit) Allow the user to input the choice. Use an if statement (or switch) to execute the users choice.

There are many ways to accomplish this task:

Technique #1.) Create a Helper class that defines the arrayList of BestFriends in its constructor, and then also defines the add, change, display, remove, and error methods as instance methods. Instantiate the Helper class from the driver (main) class. Also in the driver class, create the loop to display and process the menu options. When any specific menu option is selected, call the method from the Helper class.

Technique #2.) Instantiate the driver class in the main method (instantiate its own self). Then, define instance methods in the driver class for the add, change, display, remove, and error methods. Call them from the menu loop. Technique #3.) In the driver class, create static methods for the add, change, display, remove, and error methods. Call them from the menu loop.

In any of the above 3 techniques, the arrayList of BestFriends can be defined as a global variable, so that it does not have to be passed as a parameter to each of the add, change, display, remove, and error methods. Similarly, the Scanner keyboard object should also be defined as a global variable too.

The alternative to defining the arrayList and Scanner keyboard as global variables is to pass these as parameters to each method call. For example:

addBFF(myBFFs, keyboard);

Logic for ChangeABFF method:

Ask the user for the first and last name of the BFF that they want to change.

Create aBFF other object with the first and last name.

While Loop (while notFound && index < size of arrayList): a.) get the object from the arrayList b.) check if the object from arrayList equals the other c.) if they are equal, notFound = false (this stops the loop)

If notFound == false //found it! a.) Ask user for the changes in first, last, cell, nickname b.) Set the attributes in the object c.) Set the arrayList object back

If notFound == true //never found it a.) Tell the user Sorry, I could not find your BFF to change it

Logic for RemoveABFF method:

Ask the user for the first and last name of the BFF that they want to change.

Create aBFF other object with the first and last name.

While Loop (while notFound && index < size of arrayList): a.) get the object from the arrayList b.) check if the object from arrayList equals the other c.) if they are equal, notFound = false (this stops the loop)

If notFound == false //found it! a.) Remove it from the arrayList

If notFound == true //never found it a.) Tell the user Sorry, I could not find your BFF to remove it

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!