Advertisement
apad464

3rd FRQ #3

Apr 17th, 2023 (edited)
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. class ProductReview {
  4.     private String name;
  5.     private String review;
  6.  
  7.     public ProductReview(String pName, String pReview) {
  8.         name = pName;
  9.         review = pReview;
  10.     }
  11.  
  12.     public String getName() {
  13.         return name;
  14.     }
  15.  
  16.     public String getReview() {
  17.         return review;
  18.     }
  19.  
  20.     @Override
  21.     public String toString() {
  22.         return name + " - " + review;
  23.     }
  24. }
  25.  
  26. class ReviewCollector {
  27.     private ArrayList<ProductReview> reviewList;
  28.     private ArrayList<String> productList;
  29.  
  30.     public ReviewCollector() {
  31.         reviewList = new ArrayList<ProductReview>();
  32.         productList = new ArrayList<String>();
  33.     }
  34.  
  35.     public void addReview(ProductReview prodReview) {
  36.         reviewList.add(prodReview);
  37.  
  38.         if (!(productList.contains(prodReview.getName()))) productList.add(prodReview.getName());
  39.     }
  40.  
  41.     public int getNumGoodReviews(String prodName) {
  42.         int count = 0;
  43.  
  44.         for (ProductReview review : reviewList) {
  45.             if (review.getName().equals(prodName)) {
  46.                 if (review.getReview().contains("best"))
  47.                     count++;
  48.             }
  49.         }
  50.         return count;
  51.     }
  52.  
  53.     @Override
  54.     public String toString() {
  55.         return "Review List::" + reviewList + "\n\nProduct List::" + productList + "\n\n";
  56.     }
  57. }
  58.  
  59. public class Review {
  60.     public static void main(String[] args) {
  61.         ReviewCollector test = new ReviewCollector();
  62.         test.addReview(new ProductReview("veggie taco", "best"));
  63.         test.addReview(new ProductReview("chicken taco", "best"));
  64.         test.addReview(new ProductReview("beef taco", "Best"));
  65.         test.addReview(new ProductReview("chicken taco", "bad"));
  66.         test.addReview(new ProductReview("shrimp taco", "BEST"));
  67.         test.addReview(new ProductReview("chicken taco", "best"));
  68.         test.addReview(new ProductReview("veggie taco", "asbestos"));
  69.  
  70.         System.out.print(test);
  71.  
  72.         System.out.println("chicken taco good reviews :: " + test.getNumGoodReviews("chicken taco"));
  73.         System.out.println("beef taco good reviews :: " + test.getNumGoodReviews("beef taco"));
  74.         System.out.println("shrimp taco good reviews :: " + test.getNumGoodReviews("shrimp taco"));
  75.         System.out.println("veggie taco good reviews :: " + test.getNumGoodReviews("veggie taco"));
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement