Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bestfriendsarraylist.BestFriend;
- import java.util.Arrays;
- import java.util.Scanner;
- public class BestFriendArrayListHelper {
- // array of bestfriends
- BestFriend[] myBFFs;
- private int count;// current count of best friends
- Scanner keyboard = new Scanner(System.in);
- String firstName;
- String lastName;
- String nickName;
- String cellPhone;
- String email;
- public BestFriendArrayListHelper() //instantiates myBFFs and count. Scanner object is created too.
- {
- myBFFs = new BestFriend[10];
- count = 0;
- keyboard = new Scanner(System.in);
- Scanner keyboard = new Scanner(System.in);
- }
- public void addAFriend() //adds friend to array
- {
- System.out.println("Enter the First Name of ur friend");
- firstName = keyboard.nextLine();
- System.out.println("Enter the Last Name of ur friend");
- lastName = keyboard.nextLine();
- System.out.println("Enter the Nick Name of ur friend");
- nickName = keyboard.nextLine();
- System.out.println("Enter the Phone no of ur friend");
- cellPhone = keyboard.nextLine();
- System.out.println("Enter the email of ur friend");
- email = keyboard.nextLine();
- BestFriend bff = new BestFriend(firstName, lastName, nickName, cellPhone, email);
- if (count == myBFFs.length) { //array doubles if full
- expandArray();
- }
- // adding new best friend to the array, updating count of friends.
- myBFFs[count] = bff;
- count++;
- System.out.println("Friend has been successfully added!");
- }
- private void shrinkArray(int index) //method to shrink the array by removing element at given index.
- {
- // shifting remaining elements to the left
- for (int i = index; i < count - 1; i++) {
- myBFFs[i] = myBFFs[i + 1];
- }
- // decrementing count
- count--;
- }
- private void expandArray() //expands erray
- {
- BestFriend newArray[] = new BestFriend[myBFFs.length * 2];
- for (int i = 0; i < myBFFs.length; i++) //copies items from old array to new one
- {
- newArray[i] = myBFFs[i];
- }
- myBFFs = newArray;
- }
- public void displayFriend() {
- System.out.println("\ta. Display a specific friend: Type S ");
- System.out.println("\tb. Display all friends: Type all");
- String choice = keyboard.nextLine();
- if (choice.equalsIgnoreCase("all")) {
- for (int i = 0; i < count; i++) {
- System.out.println(myBFFs[i]);
- }
- } else {
- int i = searchAFriend();
- if (i == -1) {
- System.out.println("Not Found");
- } else {
- System.out.println(myBFFs[i]);
- }
- }
- }
- public int searchAFriend() {
- BestFriend searchObj = new BestFriend(firstName, lastName, nickName);
- System.out.println("\nWhat is their first name?");
- firstName = keyboard.nextLine();
- System.out.println("\nWhat is their last name?");
- lastName = keyboard.nextLine();
- System.out.println("\nWhat is their nick name?");
- nickName = keyboard.nextLine();
- for (int i = 0; i < count; i++) {
- if (myBFFs[i].equals(searchObj)) {
- return i;
- }
- else{
- System.out.println("User has not been found!");
- }
- } return -1;
- }
- public void deleteFriend() //deletes friend from array.
- {
- String Choice;
- int i = searchAFriend();
- if (i != -1) {
- System.out.println(myBFFs[i]);
- System.out.println("Are you sure you want to remove this friend? :");
- Choice = keyboard.nextLine();
- if (Choice.equalsIgnoreCase("yes")) {
- myBFFs[i] = null;
- shrinkArray(i);
- System.out.println("Friend has been sucessfully removed");
- }
- } else {
- System.out.println("Sorry, your friend cannot be found");
- }
- }
- public void changeFriend() {
- int i = searchAFriend();
- if (i == -1) {
- System.out.println("Not Found");
- } else {
- System.out.println("\nWhat is the new first Name of your friend?");
- firstName = keyboard.nextLine();
- System.out.println("\nWhat is the new last name of your friend?");
- lastName = keyboard.nextLine();
- System.out.println("\nWhat is the new nick name of your friend?");
- nickName = keyboard.nextLine();
- System.out.println("\nWhat is the new cell phone of your friend?");
- cellPhone = keyboard.nextLine();
- System.out.println("\nWhat is the new email of your friend?");;
- email = keyboard.nextLine();
- BestFriend bff = myBFFs[i];
- bff.setCellPhone(cellPhone);
- bff.setEmail(email);
- bff.setFirstName(firstName);
- bff.setLastName(lastName);
- bff.setNickName(nickName);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement