简体   繁体   中英

How to store search results and display data JAVA

I am stuck

I have two methods that NEEDS to be

public void searchByPriceRange(double minPrice, double maxPrice)

and

public void displaySearchResults(ArrayList<Property> searchResults)

both need to return void.

The only instance variable I am allowed to have is (not sure which one)

private ArrayList<Property> properties = new ArrayList<Property>();

or

private ArrayList<Property> properties;

I need to search an array list called properties and store the properties that fit within the price range in a LOCAL ArrayList that will then be passed to the displaySearchResults(ArrayList searchResults) for display.

How would I create a local arraylist? How would I access the data in the local array list when calling the displaySearchReults method? How would I call the display search results method?

Right now, i think my searchByPriceRange method stores the data in a local variable called searchResults. But I dont understand how to call the displaySearchResults method and display the data in the local arraylist created in the searchByPriceRange method.

My full method in agentListings class

public void searchByPriceRange(double minPrice, double maxPrice){

    ArrayList<Property> searchResults;
    searchResults = new ArrayList<Property>();
    for (Property property : properties){
        if (property.getAskingPrice() > minPrice &&
                property.getAskingPrice() < maxPrice){

            searchResults.add(property);
        }
    }
}

public void displaySearchResults(ArrayList<Property> searchResults){


    for(Property result: searchResults) {

        System.out.println(result.getListingNumber());
    }
}

My driver class

AgentListings CharlesListings = new AgentListings();

CharlesListings.displaySearchResults(CharlesListings.searchByPriceRange(1, 2));

this line doesnt work. Not sure what to put in the param if the method param has to be

public void displaySearchResults(ArrayList<Property> searchResults)

Instead of declaring arrayList inside function, declare it in class like this

class{
    ArrayList<Property> searchResults = new ArrayList<Property>();

public void searchByPriceRange(double minPrice, double maxPrice){

   
    for (Property property : properties){
        if (property.getAskingPrice() > minPrice &&property.getAskingPrice() < maxPrice){
            this.searchResults.add(property); \\You can remove "this." as it may work without that
        }
    }
}

public void displaySearchResults(ArrayList<Property> searchResults){


    for(Property result: this.searchResults) { \\You can remove "this." as it may work without that

        System.out.println(result);
        System.out.println(searchResults.size());
    }
}
}

I just found the part that we overlooked in the instructions.

public void searchByPriceRange(double minPrice, double maxPrice)

Will search the ArrayList for properties where the asking price is between the minPrice and maxPrice, and add each match to a local ArrayList that will then be passed to the displaySearchResults(ArrayList searchResults) for display

You did mention it in your question, but with everything else you wrote it kind of got me thinking in the wrong direction;)

Which means:

public class AgentListings {
    private ArrayList<Property> properties = new ArrayList<Property>();     
    
    public void searchByPriceRange(double minPrice, double maxPrice) {
        ArrayList<Property> searchResults = new ArrayList<Property>(); // <- This is your local ArrayList
        
        for (Property property : properties){
            if (property.getAskingPrice() >= minPrice && property.getAskingPrice() <= maxPrice) {
                searchResults.add(property);
            }
        }

        displaySearchResults(searchResults); // <- And here we go calling the method with the filtered list
    }

    public void displaySearchResults(ArrayList<Property> searchResults){
        System.out.println(searchResults.size());
        for(Property result: searchResults) { 
            System.out.println(result);
        }
    }
}

The important part here is that we DO NOT change the properties our class remembers. We create a new local ArrayList and immediately display it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM