Question
JAVA PROGRAMMING - I need help with the extra credit part of this assignment. I provided my code below. There are 3 classes. Extra Credit
JAVA PROGRAMMING - I need help with the extra credit part of this assignment. I provided my code below. There are 3 classes.
Extra Credit" 50 points - WHen adding BFF objects to arrayList, add them in alpha order, by first name.
Use logic to determine where the BFF object belongs in the arrayList, and then use add(location, object) method.
BEST FRIEND CLASS ------------------------------------------------------------------------------------------------------------------------------------
package skeletonhw;
import java.util.Objects;
public class BestFriend {
private String firstName;
private String lastName;
private String nickName;
private String cellPhone;
private String email;
public BestFriend(String firstName, String lastName, String nickName, String cellPhone, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.nickName = nickName;
this.cellPhone = cellPhone;
this.email = email;
}
public BestFriend(String firstName, String lastName, String nickName) {
this.firstName = firstName;
this.lastName = lastName;
this.nickName = nickName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getCellPhone() {
return cellPhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "BestFriend{" + "firstName=" + firstName + ", lastName=" + lastName + ", nickName=" + nickName + ", cellPhone=" + cellPhone + ", email=" + email + '}';
}
@Override
public boolean equals (Object obj ) {
BestFriend other;
if (this == obj){
return true;
}
if (obj == null){
return false;
}
if (obj instanceof BestFriend)
{
other = (BestFriend) obj;
}
else
{
return false;
}
if (firstName.equals(other.firstName) &&
lastName.equals(other.lastName) &&
nickName.equals(other.nickName))
return true;
else
return false;
}
}
HELPER CLASS------------------------------------------------------------------------------------------------------------------------------------------
package skeletonhw;
import java.util.ArrayList;
import java.util.Scanner;
public class Helper {
ArrayList myBFFs;
Scanner scnr;
public Helper()
{
myBFFs = new ArrayList<>();
scnr = new Scanner(System.in);
}
public void add()
{
String firstName;
String lastName;
String nickName;
String cellPhone;
String email;
System.out.println("Enter the first name of the friend: ");
firstName = scnr.nextLine();
System.out.println("Enter the last name of the friend: ");
lastName = scnr.nextLine();
System.out.println("Enter the nick-name of the friend: ");
nickName = scnr.nextLine();
System.out.println("Enter the cell-phone "
+ "number of the friend: ");
cellPhone = scnr.nextLine();
System.out.println("Enter the email address of "
+ "the friend: ");
email = scnr.nextLine();
obj.setEmail(email);
BestFriend aFriend; aFriend = new BestFriend (firstName, lastName, nickName, cellPhone, email);
if(myBFFs.isEmpty())
{
myBFFs.add(aFriend);
}
else
{
for(int i=0; i
{
if(aFriend.getFirstName().compareTo
(myBFFs.get(i).getFirstName())
<0)
{
myBFFs.add(i, aFriend);
break;
}
}
}
}
public void change()
{
int returnCode = search();
if(returnCode == -1)
{
System.out.println("NOT FOUND");
}
else
{
String firstName;
String lastName;
String nickName;
String cellPhone;
String email;
BestFriend obj = myBFFs.get(returnCode);
System.out.println(obj);
System.out.println("Enter the new information:");
System.out.println("Enter the first "
+ "name of the friend: ");
firstName = scnr.nextLine();
obj.setFirstName(firstName);
System.out.println("Enter the last "
+ "name of the friend: ");
lastName = scnr.nextLine();
obj.setLastName(lastName);
System.out.println("Enter the nick-name "
+ "of the friend: ");
nickName = scnr.nextLine();
obj.setNickName(nickName);
System.out.println("Enter the cell-phone "
+ "number of the friend: ");
cellPhone = scnr.nextLine();
obj.setCellPhone(cellPhone);
System.out.println("Enter the email "
+ "address of the friend: ");
email = scnr.nextLine();
myBFFs.set(returnCode, obj);
}
}
public void remove()
{
int returnCode = search();
if(returnCode == -1)
{
System.out.println("NOT FOUND");
}
else
{
BestFriend obj = myBFFs.get(returnCode);
System.out.println(obj);
System.out.println("Are you sure you"
+ " want to remove that friend?");
String choice = scnr.nextLine().toUpperCase();
if(choice.compareTo("YES")==0)
{
myBFFs.remove(returnCode);
System.out.println("BFF removed");
}
else
{
System.out.println("BFF remove CANCELLED");
}
}
}
public void display()
{
System.out.println("Choose from one of "
+ "the following options:");
System.out.println("1.Display a specific friend");
System.out.println("2.Display All the friends");
System.out.print("(1/2)?: ");
int choice = scnr.nextInt();
scnr.nextLine();
if(choice == 2)
{
for(int i=0; i< myBFFs.size(); i++)
{
System.out.println(myBFFs.get(i));
}
}
else
{
int returnCode = search();
if(returnCode == -1)
{
System.out.println("NOT FOUND");
}
else
{
BestFriend obj = myBFFs.get(returnCode);
System.out.println(obj);
}
}
}
public int search()
{
String firstName;
String lastName;
String nickName;
System.out.println("Enter the first name of the friend: ");
firstName = scnr.nextLine();
System.out.println("Enter the last name of the friend: ");
lastName = scnr.nextLine();
System.out.println("Enter the nick-name of the friend: ");
nickName = scnr.nextLine();
BestFriend searchObj; searchObj = new BestFriend(firstName, lastName, nickName);
int i=0;
while(i < myBFFs.size())
{
if(searchObj.equals(myBFFs.get(i)))
{
return i;
}
i++;
}
return -1;
}
}
MAIN CLASS---------------------------------------------------------------------------------------------------------------------------------------
package skeletonhw;
import java.util.Scanner;
public class SkeletonHW {
public static void main(String[] args) {
System.out.println("Welcome to the Best Friends' Contacts!");
Helper myHelper = new Helper();
int menuSelection;
do
{
menuSelection = displayMenu();
switch (menuSelection)
{
case 1:
myHelper.add();
System.out.println("new BFF added");
break;
case 2:
myHelper.change();
System.out.println("BFF changed");
break;
case 3:
myHelper.remove();
System.out.println("");
break;
case 4:
System.out.println("calling display method");
myHelper.display();
break;
case 5:
System.out.println("Thanks for using BFF Contacts.");
break;
default:
System.out.println("Sorry, you entered an invalid option. Try Again.");
}
}while (menuSelection != 5);
}
public static int displayMenu()
{
int menuSelection;
Scanner sc = new Scanner(System.in);
System.out.println("1.Add a BestFriend object to their BFF arrayList");
System.out.println("2.Change info of a BestFriend object in their BFF arrayList");
System.out.println("3.Remove a BestFriend object from their BFF arrayList");
System.out.println("4.Display");
System.out.println("5.Exit");
menuSelection = sc.nextInt();
return menuSelection;
}
}
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