Write the getNumGoodReviews method, which returns the number of good reviews for a given product name. A review is considered good if it contains the string "best" (all lowercase). If there are no reviews with a matching product name, the method returns 0. Note that a review that contains "BEST" or "Best" is not considered a good review (since not all the letters of "best" are lowercase), but a review that contains "asbestos" is considered a good review (since all the letters of "best" are lowercase). Complete method getNumGoodReviews. /** Returns the number of good reviews for a given product name, as described in part (b). */ public int getNumGoodReviews(String prodName)

Respuesta :

Answer:

Check the explanation

Explanation:

ProductReview.java

public class ProductReview {

  private String name;

  private String review;

 

  public ProductReview(String name, String review) {

      super();

      this.name = name;

      this.review = review;

  }

  public String getName() {

      return name;

  }

  public String getReview() {

      return review;

  }

 

 

}

ReviewCollector.java

import java.util.ArrayList;

public class ReviewCollector {

  private ArrayList<ProductReview> reviewList;

  private ArrayList<String> productList;

  public ReviewCollector(ArrayList<ProductReview> reviewList, ArrayList<String> productList) {

      super();

      this.reviewList = reviewList;

      this.productList = productList;

  }

  public void addReview(ProductReview prodReview) {

      this.reviewList.add(prodReview);

  }

  public int getNumGoodReviews(String prodName) {

      int count = 0;

      ArrayList<ProductReview> set = new ArrayList<>();

      for (int i = 0; i < reviewList.size(); i++) {

          if (reviewList.get(i).getName().compareToIgnoreCase(prodName) == 0) {

              set.add(reviewList.get(i));

          }

      }

      if(set.size()==0)

          return count;

     

      for (int i = 0; i < set.size(); i++) {

          if (set.get(i).getReview().contains("best")) {

              count++;

          }

      }

      return count;

  }

}