rriggi129
rriggi129

Reputation: 19

Java arraylist, trying to search?

These are the methods I've been given, and they are all void. I want to access the results from the displaySearchResults using an appropriate loop, only reading data.

Anyone know what I need to do to pull the results from the 3 prior search methods?

/**
*   Searches inventory by model
*   @param model is the model you'd like to find
*/
public void searchByModel(String model){
    ArrayList<Vehicle> results = new ArrayList();
    for(int i = 0; i < vehicles.size(); i++){
        if(vehicles.get(i).getModel().equalsIgnoreCase(model)){
            results.add(vehicles.get(i));
        }
    }
}  

    /**
*   Searches inventory by year
*   @param year is the year you'd like to find
*/
public void searchByYear(int year){
    ArrayList<Vehicle> results = new ArrayList();
    for(int i = 0; i < vehicles.size(); i++){
        if(vehicles.get(i).getYear() == year){
            results.add(vehicles.get(i));
        }
    }
}

/**
*   Searches inventory by price
*   @param minPrice is the lowest price you'd like to search by
*   @param maxPrice is the highest price you'd like to search by
*/
public void searchByPrice(double minPrice, double maxPrice){
    ArrayList<Vehicle> results = new ArrayList();
    for(int i = 0; i < vehicles.size(); i++){
        if(vehicles.get(i).getSellingPrice() < maxPrice &&
        vehicles.get(i).getSellingPrice() > minPrice){
            results.add(vehicles.get(i));
        }
    }

}

/**
 *  @return Displays search results, unsure of how to get this working still
 */
public void displaySearchResults(ArrayList<Vehicle> results){
    for(Vehicle vehicle : results){

    }

Upvotes: 0

Views: 1274

Answers (3)

sara
sara

Reputation: 11

public void displaySearchResults(ArrayList<Vehicle> results){
    for(Vehicle vehicle : results){
        System.out.println(vehicle.getModel()+ " of " +vehicle.getYear()+ " of " + vehicle.getSellingPrice());
    }
}

Upvotes: 1

Virmundi
Virmundi

Reputation: 2631

You could make an object that takes in its constructor the vehicle array and has a member called results.

public class WhyWouldYouDoThis {
    private List<Vehicle> results;
    public WhyWouldYouDoThis() {
    }
    public List<Vehicle> getResults() {
        return results;
    }
   /**
    *   Searches inventory by year
    *   @param year is the year you'd like to find
    */
   public void searchByYear(int year){

         results = new LinkedList<>();
      for(int i = 0; i < vehicles.size(); i++){
         if(vehicles.get(i).getYear() == year){
            results.add(vehicles.get(i));
         }
       }
   }

}

Now there are a few things to keep in mind with this. A) it's fairly crazy because your methods you be returning result. You're current code is really problematic at an API/design level. B) It is not thread safe.

Upvotes: 0

Bal&#225;zs &#201;des
Bal&#225;zs &#201;des

Reputation: 13807

Change your search methods, so that they actually return the results:

public List<Vehicle> searchByYear(int year){
    ArrayList<Vehicle> results = new ArrayList<>();
    for(int i = 0; i < vehicles.size(); i++){
        if(vehicles.get(i).getYear() == year){
            results.add(vehicles.get(i));
        }
    }
    return results;
}

Now when displaying, you can iterate the results of an actual search:

public void displaySearchResults(ArrayList<Vehicle> results){
    for(Vehicle vehicle : searchByYear(1991)){
        //display whatever you want from it
    }
    // do this with the other results
}

Also, if you are using java 8, you could replace the for loops with the usage of the much more elegant functional stuff:

public List<Vehicle> searchByPrice(double min, double max){
    return vehicles.stream()
        .filter(v -> (v.getSellingPrice() > min && v.getSellingPrice() < max))
        .collect(Collectors.toList());
}

Upvotes: 0

Related Questions