0

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){

    }
2
  • as is you can't use results from outside each searchByXX method, it's a local variable Commented Dec 1, 2014 at 5:56
  • Tell whoever gave you this to learn programming. These methods effectively do nothing. They are useless. They do a bunch of work then throw it away. Commented Dec 1, 2014 at 6:34

3 Answers 3

1
public void displaySearchResults(ArrayList<Vehicle> results){
    for(Vehicle vehicle : results){
        System.out.println(vehicle.getModel()+ " of " +vehicle.getYear()+ " of " + vehicle.getSellingPrice());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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());
}

Comments

0

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.

2 Comments

Its what I've been given and I'm not allowed to change it around, I've just gotta some how make it work. Not Ideal I know..
The example from above it the only way to make it work. If you're on a project and this is what the architect/team lead delivered, go to management and say it is poor work. Otherwise you can't do it.

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.