Question
* Add data into arraylist in alphabetical order + Javadoc . in the BestFriendSimulation driver class, instantiate a Helper class that will contain an arrayList
* Add data into arraylist in alphabetical order + Javadoc .
in the BestFriendSimulation driver class, instantiate a Helper class that will contain an arrayList of BestFriends, and will have all the functionality of updating the arrayList of BestFriend objects.
Then, create a menu that will continue to display until the user requests to exit the menu (loop). That menu will ask the user what they wish to do:
1. Add a BestFriend object to their BFF arrayList 2. Change info of a BestFriend object in their BFF arrayList 3. Remove a BestFriend object from their BFF arrayList 4. Display a. a specific BestFriend's info (using their last name and first name to search) b. all the BFF objects in the arrayList 5. Exit
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.
Create a BestFriend domain class with the following attributes: a. first name b. last name c. nick name d. cell phone number e. email address
Code:
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
public static void main(String arg[]) {
Scanner scanner = new Scanner(System.in);
best = new ArrayList
int Option = 0;
String first, last, nick, number;
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 " + " ";
int AS = 0;
while (Option != 5) { System.out.print(MainMenu);
Option = scanner.nextInt();
switch (Option) { case 1: { System.out.println("Please Enter First Name"); first = scanner.next();
System.out.println("Please Enter Last Name"); last = scanner.next();
System.out.println("Please Enter Nick Name"); nick = scanner.next();
System.out.println("Please Enter Cell Phone Number"); number = scanner.next();
best.add(new BestFriend(first, last, nick, number)); break; } case 2: { System.out.println("Enter First Name Of Friend You Want To Change"); first = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To Change"); last = scanner.next();
BestFriend BF = new BestFriend(first, last, "", ""); AS = best.size();
if (AS == 0) { System.out.print("List is Empty"); break; }
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"); nick = scanner.next();
System.out.println("Please Enter New Cell Phone Number"); 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.print("No Such Friend Exists"); break;
} }
break; }
case 3: {
System.out.println("Enter First Name Of Friend You Want To Remove"); first = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To Remove"); last = scanner.next();
BestFriend BF = new BestFriend(first, last, "", "");
AS = best.size();
if (AS == 0) { System.out.print("List is Empty"); break;
} for (int i = 0; i < AS; i++) { if (best.get(i).equals(BF)) { best.remove(i); break; } else if (i == AS - 1) { System.out.print("No Such Friend Exists"); break; } } break; } case 4: { AS = best.size(); if (AS == 0) { System.out.print("List is Empty"); break; } for (int i = 0; i < AS; i++) {
System.out.print(best.get(i).toString()); break; } break; } case 5: { break; } } } } }
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