0

I'm trying to create a menu page that allows addition to the array, output of the array and to search by name. I'm struggling with the search part, as it is a multi-dimensional array. How do I search just the names part of each object?

I'm also not sure how to loop this so that they return to the main page after each request, and therefore the array remains updated with any new editions.

package qa.com.task;

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

import qa.com.task.Person;

public class Runner {

    public static void main(String[] args) {
        
        Person pp1 = new Person("Karen", 27, "DevOps Engineer");
        Person pp2 = new Person("Jim", 24, "Software Engineer");

        // Create array
        ArrayList<Person> people = new ArrayList<Person>();
        people.add(pp1);
        people.add(pp2);

        // Search array
        Scanner scan = new Scanner(System.in);
    
    System.out.println("---------------------MENU---------------------");
    System.out.println("------Create--------Search-------Output All---");
    System.out.println("------type c--------type s---------type o-----");
    String request = scan.nextLine();
    
    if (request.contains("c")){
        //CREATE NEW PERSON
    System.out.println("----------Create Request: Enter Name----------");
    String newname = scan.nextLine();
    System.out.println("-------------------Enter Age-------------------");
    Integer newage = scan.nextInt();
    scan.nextLine();
    System.out.println("-------------------Job Title-------------------");
    String newjobtitle = scan.nextLine();
    Person ppnew = new Person(newname, newage, newjobtitle);
    people.add(ppnew);
    System.out.println("-----Updated Array with New Creation Request----");
    System.out.println(Arrays.toString(people.toArray()));
    }
    
    if (request.contains("s")){
        //SEARCH
    System.out.println("----------Search Request: Enter Name----------");
    String searchname = scan.nextLine();
    
    }
    
    if (request.contains("o")){
    //OUTPUT DATABASE
    System.out.println("----------------Output Request:----------------");
    System.out.println(Arrays.toString(people.toArray()));
    }
    

}}

1 Answer 1

1

You can filter your people list based on a person name.

List<Person> filteredPeople = people.stream()
    .filter(person -> person.getName().contains(searchname)) // Filter condition
    .collect(Collectors.toList()); // Get the result as list

Then you can do what you want with filteredPeople.
Otherwise you can use a traditional for loop to iterate over the list and "print" Persons who match the condition, something like below.

List<Person> people...;
boolean stop = false;
while(!stop) {
    // print menu (with exit command)
    // handle commands and if exit command, set stop = true.
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.