Reputation: 77
I am supposed to use the method exactly(all my methods have to return void)
public void searchByPriceRange(double minPrice, double maxPrice)
The method will search the ArrayList<Property>
for properties where the asking price is between the minPrice and maxPrice and add each match to a local ArrayList<Property>
that will then be passed to another method that has to be
displaySearchResults(ArrayList<Property> searchResults)
How would I create a local ArrayList<Property>
that is accessible outside the scope of the first method?(if thats what I am supposed to do)
my instance variable and initialization of ArrayList
private ArrayList<Property> properties = new ArrayList<Property>();
my method in AgentListing 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);
}
}
}
my display method AgentListing class
public void displaySearchResults(ArrayList<Property> searchResults){
for(Property result: searchResults) {
System.out.println(result);
}
}
my driver class that I am trying to access the local arraylist with
AgentListings CharlesListings = new AgentListings();
Property one = new Property("1ListingNumber", "777 Imaginary Drive, Nowhere, British Columbia", 1.00, "Single Family", "House",
"1.1 Acre", 2007);
one.setNumberOfBedrooms(1);
one.setNumberOfBathrooms(1);
Property two = new Property("2ListingNumber", "777 Imaginary Drive, Nowhere, British Columbia", 2.00, "Single Family", "House",
"2.2 Acre", 2007);
two.setNumberOfBedrooms(2);
two.setNumberOfBathrooms(3);
CharlesListings.addProperty(one);
CharlesListings.addProperty(two);
CharlesListings.searchByPriceRange(1.0, 2.0);
CharlesListings.displaySearchResults(searchResults); //searchResults is not reachable
I added the full instructions to my assignment if I am not clear
Upvotes: 1
Views: 157
Reputation: 376
Modify searchByPrice(...) method like this:
public ArrayList<Property> 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);
}
}
return searchResults;
}
Now, when it returns searchResults array, you can use the returned value as a parameter for displaySearchResults(searchResults) method. Like this:
ArrayList<Property> searchResultsList = CharlesListings.searchByPriceRange(1.0, 2.0);
CharlesListings.displaySearchResults(searchResultsList);
Upvotes: 1
Reputation: 740
searchResult
is a local variable, so it's not visible outside the method it is declared in. A possible solution would be to change the signature of the searchByPriceRange
method from:
public void searchByPriceRange(double minPrice, double maxPrice)
to
public List<Property> searchByPriceRange(double minPrice, double maxPrice)
.
Now you can return the searchResults
list, so it can be stored in a variable or passed to another method. Since you only need to pass it to the displaySearchResults
method, you could write:
CharlesListings.displaySearchResults(CharlesListings.searchByPriceRange(1.0, 2.0));
Upvotes: 1
Reputation: 48268
How would I create a local ArrayList that is accessible outside the scope of the first method?(if thats what I am supposed to do)
here:
public void searchByPriceRange(double minPrice, double maxPrice)
return a list (searchResults) instead of void...
you can define a temp variable, or just queue the returned value as param to the display method
ArrayList<Property> x = searchByPriceRange(min, max);
displaySearchResults(x);
or
displaySearchResults(searchByPriceRange(min, max));
Upvotes: 1