In: Computer Science
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)
public boolean equals(Object another)
{
if (another instanceof BestFriend
)
{
BestFriend anotherBFF = (BestFriend)
another;
if
(firstName.equalsIgnoreCase(anotherBFF.firstName) &&
lastName.equalsIgnoreCase(anotherBFF.lastName)
// &&
nickname.equalsIgnoreCase(anotherBFF.nickName) &&
//
cellphone.equalsIgnoreCase(anotherBFF.cellPhone))
return true;
}
return false;
}
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 user’s 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);
/* setFirstName(String) , setLastName(String) , setNickName(String)
, setCellPhoneNumber(String)*/
/* BestFriendSimulation contains the main function *
/
public import java.util.*;
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)
void setFirstName(String FN)
{
firstName = FN;
}
void setLastName(String LN)
{
lastName = LN;
}
void setNickName(String NN)
{
nickName = NN;
}
void setCellPhoneNumber(String CPN)
{
cellPhoneNumber = CPN;
}
public boolean equals(Object another)
{
if (another instanceof BestFriend )
{
BestFriend anotherBFF = (BestFriend) another;
if (firstName.equalsIgnoreCase(anotherBFF.firstName)
&&
lastName.equalsIgnoreCase(anotherBFF.lastName))
// && nickname.equalsIgnoreCase(anotherBFF.nickName)
&&
// cellphone.equalsIgnoreCase(anotherBFF.cellPhone))
return true;
}
return false;
}
}
class BestFriendSimulation
{
private static ArrayList<BestFriend> ABF ;
public static void main(String arg[])
{
Scanner scanner = new Scanner(System.in);
ABF = new ArrayList<BestFriend>();
int Option = 0 ;
String FN , LN , NN ,CPN ;
String MainMenu = "Please Enter Option Number Of your
Choice"+"\n"+"1.) Add a BestFriend to the arrayList called
myBestFriends; " +"\n"+"2.) Change a BestFriend in the
arrayList;"+"\n"+ "3.) Remove a BestFriend from the
arrayList;"+"\n"+"4.) Display all the objects in the myBestFriends
arrayList."+"\n"+"5.) Exit "+"\n";
int AS = 0;
while(Option!=5)
{
System.out.print(MainMenu);
Option = scanner.nextInt();
switch(Option)
{
case 1 : {
System.out.println("Please Enter First Name");
FN = scanner.next();
System.out.println("Please Enter Last Name");
LN = scanner.next();
System.out.println("Please Enter Nick Name");
NN = scanner.next();
System.out.println("Please Enter Cell Phone Number");
CPN = scanner.next();
ABF.add(new BestFriend(FN,LN,NN,CPN));
break;
}
case 2 : {
System.out.println("Enter First Name Of Friend You Want To
Change");
FN = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To
Change");
LN = scanner.next();
BestFriend BF = new BestFriend(FN,LN,"","");
AS = ABF.size();
if(AS==0)
{
System.out.print("List is Empty");
break;
}
for(int i=0;i<AS;i++)
{
if(ABF.get(i).equals(BF))
{
System.out.println("Please Enter New First Name");
FN = scanner.next();
System.out.println("Please Enter New Last Name");
LN = scanner.next();
System.out.println("Please Enter New Nick Name");
NN = scanner.next();
System.out.println("Please Enter New Cell Phone Number");
CPN = scanner.next();
ABF.get(i).setFirstName(FN);
ABF.get(i).setLastName(LN);
ABF.get(i).setNickName(NN);
ABF.get(i).setCellPhoneNumber(CPN);
break;
}
else if(i==AS-1)
{
System.out.print("No Such Friend Exists");
break;
}
}
break;
}
case 3 : {
System.out.println("Enter First Name Of Friend You Want To
Remove");
FN = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To
Remove");
LN = scanner.next();
BestFriend BF = new BestFriend(FN,LN,"","");
AS = ABF.size();
if(AS==0)
{
System.out.print("List is Empty");
break;
}
for(int i=0;i<AS;i++)
{
if(ABF.get(i).equals(BF))
{
ABF.remove(i);
break;
}
else if(i==AS-1)
{
System.out.print("No Such Friend Exists");
break;
}
}
break;
}
case 4 : {
AS = ABF.size();
if(AS==0)
{
System.out.print("List is Empty");
break;
}
for(int i=0;i<AS;i++)
{
System.out.print(ABF.get(i).toString());
break;
}
break;
}
case 5 : {
break;
}
}
}
}
}