Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEED HELP WITH THIS QUESTION PROGRAMMING LANGUAGE : JAVA The goal of this Progra

ID: 3817524 • Letter: N

Question

NEED HELP WITH THIS QUESTION

PROGRAMMING LANGUAGE : JAVA

The goal of this Programming Project is to extend Programming Project 1 to find the customer from the CSV file who made ratings that are most similar to ratings input from the keyboard. First, do Programming Project 1. Then modify your solution so that it asks the user to input ratings for the first four products before the program reads from the file. The program should then predict whether or not the user will like the final product by outputting the rating made by the most similar customer. Use the formula IAu Acl I Bu Bol +ICu Ccl I Du Dcl to compute the similarity, where Au is the rating for product A made by the user at the keyboard and Ac is the rating for product A made by a customer from the file. A lower total indicates greater similarity. For example, if the user inputs 1 for product A, 1 for product B, 3 for product C, and 2 for product D, then with the values from Programming Project 1, the similarity to the customer in the first row is 11 3l 11 0| 13 5l 12 1 6, while the similarity to the customer in the second row is 11 1 11 1 13 4l 12- 2l J 1. The customer in the second row has the greatest similarity, so the program would output that the prediction for product E is 1, which is the second customer's rating for product E

Explanation / Answer


ProductRating.java
--------------------------

package chegg.customer.product;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class ProductRating {

   static List<CustomerAndFeedback> customerList = new ArrayList<CustomerAndFeedback>();
   static String[] products = null;
   static int customerId = 0;

   public static void main(String[] args) {
       List<String> list = ProductRating.readFile("product-rating");
       products = list.get(0).split(",");
       for (int indx = 1; indx < list.size() - 1; indx++) {
           String[] ratings = list.get(indx).split(",");
           Map<String, Integer> prductAndRatingMap = new HashMap<String, Integer>();
           if (ratings != null) {
               for (int indx2 = 0; indx2 <= ratings.length - 1; indx2++) {
                   prductAndRatingMap.put(products[indx2],
                           Integer.parseInt(ratings[indx2]));
               }
               customerList.add(new CustomerAndFeedback(ProductRating
                       .generateUniqueCustomerID(), prductAndRatingMap));
           }
       }

       for (CustomerAndFeedback object : customerList) {
           System.out.println(object);
       }

       Map<String, Double> averageFeedbackMap = ProductRating
               .findAverageRatingforProduct();
       System.out.println("average calculated rating customr wise : "+averageFeedbackMap);

       ProductRating.getSimilarityWithinput();

   }

   public static void getSimilarityWithinput() {
       String[] rating = new String[ProductRating.products.length];
       Scanner scanner = new Scanner(System.in);

       for (int indx = 0; indx < ProductRating.products.length; indx++) {
           System.out.println("Enter rating for Product : "
                   + ProductRating.products[indx]);
           rating[indx] = scanner.next();
       }

       if (scanner != null) {
           scanner.close();
       }

       List<Double> mostSimiliarRatingList = new ArrayList<Double>();
       List<UserCustomerRating> list = new ArrayList<UserCustomerRating>();

       for (CustomerAndFeedback customerAndFeedback : customerList) {
           double ratingDifference = 0.0;
           for (int indx = 0; indx <= ProductRating.products.length - 1; indx++) {
               double c = customerAndFeedback.getProductFeedBackMap().get(
                       ProductRating.products[indx]);
               double u = Double.parseDouble(rating[indx]);
               ratingDifference = ratingDifference + u - c;
           }
           mostSimiliarRatingList.add(ratingDifference);
       }

       for (int indx3 = 0; indx3 < customerList.size(); indx3++) {
           UserCustomerRating userCustomerRating = new UserCustomerRating(
                   ProductRating.products[indx3],
                   mostSimiliarRatingList.get(indx3));
           list.add(userCustomerRating);
       }

       Collections.sort(list);

       System.out.println("Most similar rating for product "
               + list.get(0).getProduct() + " and calcuated rating is : "
               + list.get(0).getRatingDifference());

   }

   public static Map<String, Double> findAverageRatingforProduct() {
       double totalRating = 0;
       int noOfCustomer = 0;
       Map<String, Double> averageFeedbackMap = new HashMap<String, Double>();
       List<CustomerAndFeedback> list = ProductRating.customerList;
       for (String string : ProductRating.products) {
           totalRating = 0;
           noOfCustomer = 0;
           for (CustomerAndFeedback customerAndFeedback : list) {
               Integer rating = customerAndFeedback.getProductFeedBackMap()
                       .get(string);
               if (rating != 0) {
                   totalRating = totalRating
                           + customerAndFeedback.getProductFeedBackMap().get(
                                   string);
                   noOfCustomer++;
               }

           }
           averageFeedbackMap.put(string, totalRating / noOfCustomer);
       }
       return averageFeedbackMap;

   }

   public static int generateUniqueCustomerID() {
       return customerId++;
   }

   /**
   *
   * @param fileNameWithPath
   * @return
   */
   public static List<String> readFile(String fileNameWithPath) {
       List<String> list = new ArrayList<String>();
       try (BufferedReader br = new BufferedReader(new FileReader("d:\"
               + fileNameWithPath + ".txt"))) {
           String sCurrentLine;
           while ((sCurrentLine = br.readLine()) != null) {
               list.add(sCurrentLine);
           }
       } catch (IOException e) {
           e.printStackTrace();
       }
       return list;
   }

}

CustomerAndFeedback.java

---------------------------------------

package chegg.customer.product;

import java.util.Map;

public class CustomerAndFeedback {

   private int customerId;
   private Map<String, Integer> productFeedBackMap;  

   public CustomerAndFeedback() {
      
   }

   public CustomerAndFeedback(int customerId,
           Map<String, Integer> productFeedBackMap) {
       this.customerId = customerId;
       this.productFeedBackMap = productFeedBackMap;
   }

   public int getCustomerId() {
       return customerId;
   }

   public void setCustomerId(int customerId) {
       this.customerId = customerId;
   }

   public Map<String, Integer> getProductFeedBackMap() {
       return productFeedBackMap;
   }

   public void setProductFeedBackMap(Map<String, Integer> productFeedBackMap) {
       this.productFeedBackMap = productFeedBackMap;
   }

   @Override
   public String toString() {
       return "CustomerAndFeedback [customerId=" + customerId
               + ", productFeedBackMap=" + productFeedBackMap + "]";
   }
        

}

UserCustomerRating .java

-----------------------------------

package chegg.customer.product;

import java.util.Comparator;

public class UserCustomerRating implements Comparable<UserCustomerRating> {

   private String product;
   private double ratingDifference;

   public UserCustomerRating(String product, double ratingDifference) {
       super();
       this.product = product;
       this.ratingDifference = ratingDifference;
   }

   public String getProduct() {
       return product;
   }

   public void setProduct(String product) {
       this.product = product;
   }

   public double getRatingDifference() {
       return ratingDifference;
   }

   public void setRatingDifference(double ratingDifference) {
       this.ratingDifference = ratingDifference;
   }

   @Override
   public int compareTo(UserCustomerRating o2) {
       int i = 0;

       if (this.getRatingDifference() > o2.getRatingDifference()) {
           i = 1;
       } else if (this.getRatingDifference() < o2.getRatingDifference()) {
           i = -1;
       } else {
           i = 0;
       }
       return i;
   }

   @Override
   public String toString() {
       return "UserCustomerRating [product=" + product + ", ratingDifference="
               + ratingDifference + "]";
   }
        

}

output

--------------

CustomerAndFeedback [customerId=0, productFeedBackMap={A=3, b=0, c=5, D=1, E=2}]
CustomerAndFeedback [customerId=1, productFeedBackMap={A=1, b=1, c=4, D=2, E=1}]
CustomerAndFeedback [customerId=2, productFeedBackMap={A=0, b=0, c=5, D=1, E=3}]
{A=2.0, b=1.0, c=4.666666666666667, D=1.3333333333333333, E=2.0}

Enter rating for Product : A
2
Enter rating for Product : b
3
Enter rating for Product : c
3
Enter rating for Product : D
2
Enter rating for Product : E
1
Most similar rating for product A and calcuated rating is : 0.0

Description :

----------------------

1. Need to rnu mainm method of ProductRating.java class

2. Please place the file on d drive with name product-rating.csv

3. following sample data i have taken for file

A,b,c,D,E
3,0,5,1,2
1,1,4,2,1
0,0,5,1,3

please let me know if you face dificulties while making undestanding on above code or rnniung the same.