adamjohnson adamjohnson - 1 month ago 7
Java Question

ArrayLists (Removing and Changing Elements)

Hello everyone I am an amateur in Java and had some specific questions about a program using ArrayLists. The program is made up of several classes, and its purpose is to add, change, remove, and display friends from a Phone Book. I have the add and display methods done, but I'm having trouble with the remove and change method. I saw a similar case on this site, but it did not help me solve my problems. Any help at all would be much appreciated. This is what I have so far:





package bestfriends;

import java.util.Scanner;
import java.util.ArrayList;

public class BFFHelper
{
ArrayList<BestFriends> myBFFs;
Scanner keyboard = new Scanner(System.in);

public BFFHelper()
{
myBFFs = new ArrayList<BestFriends>();
}

public void addABFF()
{
System.out.println("Enter a first name: ");
String firstName = keyboard.next();
System.out.println("Enter a last name: ");
String lastName = keyboard.next();
System.out.println("Enter a nick name: ");
String nickName = keyboard.next();
System.out.println("Enter a phone number: ");
String cellPhone = keyboard.next();
BestFriends aBFF = new BestFriends(firstName, lastName, nickName, cellPhone);
myBFFs.add(aBFF);


}

public void changeABFF()
{
System.out.println("I am in changeBFF");
}

public void displayABFF()
{
System.out.println("My Best Friends Phonebook is: ");
System.out.println(myBFFs);
}

public void removeABFF()
{
System.out.print("Enter a friend's name to be removed: ");
int i = 0;
boolean found = false;

while (i < myBFFs.size() && !found)
{
if(firstName.equalsIgnoreCase(myBFFs.get(i).getFirstName()) && lastName.equalsIgnoreCase(myBFFs.get(i).getLastName()))
{
found = true;
}
else
i++;
}
}
}


That was my Helper Class, for which I'm having trouble with the removeABFF method, and still need to create a changeABFF method from scratch. Next is my main class:





package bestfriends;
import java.util.Scanner;

public class BFFPhoneBook
{

public static void main(String args[])
{
int menuOption = 0;
Scanner keyboard = new Scanner(System.in);
BFFHelper myHelper = new BFFHelper();

do
{

System.out.println("1. Add a Friend");
System.out.println("2. Change a Friend");
System.out.println("3. Remove a Friend");
System.out.println("4. Display a Friend");
System.out.println("5. Exit");

System.out.print("Enter your selection: ");
menuOption = keyboard.nextInt();

switch (menuOption)
{
case 1:
myHelper.addABFF();
break;
case 2:
myHelper.changeABFF();
break;
case 3:
myHelper.removeABFF();
break;
case 4:
myHelper.displayABFF();
break;
case 5:
break;
default:
System.out.println("Invalid option. Enter 1 - 5");

}



} while (menuOption != 5);
}
}


This is my last class:





package bestfriends;

public class BestFriends {

private static int friendNumber = 0;
private int friendIdNumber;
String firstName;
private String lastName;
private String nickName;
private String cellPhoneNumber;


public BestFriends (String aFirstName, String aLastName, String aNickName, String aCellPhone)
{
firstName = aFirstName;
lastName = aLastName;
nickName = aNickName;
cellPhoneNumber = aCellPhone;
friendIdNumber = ++friendNumber;
// friendIdNumber = friendNumber++;


}

public boolean equals(Object aFriend)
{
if (aFriend instanceof BestFriends )
{
BestFriends myFriend = (BestFriends) aFriend;
if (lastName.equals(myFriend.lastName) && firstName.equals(myFriend.firstName))
return true;
else
return false;
}
else
return false;



}
public String getFirstName()
{
return firstName;
}

public String getLastName()
{
return lastName;
}

public String getNickName()
{
return nickName;
}

public String getCellPhone()
{
return cellPhoneNumber;
}

public int getFriendId()
{
return friendIdNumber;
}

public String toString()
{
return friendIdNumber + ". " + firstName + " (" + nickName + ") " + lastName + "\n" + cellPhoneNumber + "\n";
}
}

Answer

To explore and manipulate a arraylist an iterator is used the object lacks the Setters

declare variables

ArrayList<BestFriends> myBFFs;
Scanner keyboard = new Scanner(System.in);
BestFriends best;
public BFFHelper()
{
    myBFFs = new ArrayList<BestFriends>();
    best= new BestFriends();
}

Delete

public void removeABFF()
{
    System.out.print("Enter a friend's name to be removed: ");
    String name= keyboard.next().toLowerCase();// entry name to be removed
    Iterator<BestFriends> nameIter = myBFFs.iterator(); //manipulate ArrayList
    while (nameIter.hasNext()){
        best = nameIter.next(); // obtained object list
        if (best.getNickName().trim().toLowerCase().equals(name)){ // if equals name
            nameIter.remove(best); // remove to arraylist
        }           
    }    
} 

Update

 public void changeABFF()
{
    System.out.print("Enter a friend's name to be change: ");
    String name= keyboard.next().toLowerCase().trim();//entry name to be update
    Iterator<BestFriends> nameIter = myBFFs.iterator(); 
    while (nameIter.hasNext()){
        best = nameIter.next();
        if (best.getNickName().trim().toLowerCase().equals(name)){// if equals name
            best.setNickName("NEW DATE");//update data with new data Setters
            ....
        }           
    }
}