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
*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
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
public BestFriendHelper(){
best = new ArrayList
}
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());
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
