Creating a Java-Based Rating Management System for Items

Verified

Added on  2019/09/18

|13
|1219
|353
Practical Assignment
AI Summary
This assignment presents a Java-based solution for a rating management system. The code includes three classes: `Rating`, `RatingManager`, and `ContentsRating`. The `Rating` class encapsulates the user ID, item ID, and rating value. The `RatingManager` class reads ratings from a file, calculates item and user ratings, and determines the average ratings. It also includes methods to retrieve specific item ratings, user ratings, average item ratings, average user ratings, and the highest-rated items. The `main` method demonstrates the usage of these functionalities by reading data from a file, calculating item and user ratings, and displaying the results. The code effectively demonstrates the implementation of a rating system and provides functionalities for data retrieval and analysis.
Document Page
Contents
Rating.java.......................................................................................................................................2
RatingManager.java.........................................................................................................................3
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Rating.java
public class Rating {
private int userId;
private int itemId;
private int value;
public Rating(int userId, int itemId, int value) {
this.userId = userId;
this.itemId = itemId;
this.value = value;
}
public int getUserId() {
return userId;
}
public int getItemId() {
return itemId;
}
Document Page
public int getValue() {
return value;
}
}
RatingManager.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class RatingManager {
private static int i = -1;
private static Rating ratingAr[];
Document Page
public static void main(String args[]) {
ratingAr = new Rating[100000];
String fileName = "src/ratings.txt";
RatingManager rm = read(fileName);
int itemID = 1827;
System.out.println("Item rating for "+itemID);
int rating[] = rm.getItemRatings(itemID);
for(int r: rating){
if(r == 0) break;
System.out.print(r+", ");
}
System.out.println("\n");
System.out.println("Average rating for "+itemID);
double avgRating = rm.getAverageItemRating(itemID);
System.out.print(avgRating+"\n\n");
int userID = 6393;
System.out.println("Average rating for user "+userID);
double avgUserRating = rm.getAverageUserRating(userID);
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
System.out.print(avgUserRating+"\n\n");
System.out.println("User rating for "+userID);
rating = rm.getUserRatings(userID);
for(int r: rating){
if(r == 0) break;
System.out.print(r+", ");
}
System.out.println("\n\n");
System.out.println("Highest rating items ");
int highestRItems[] = rm.getHighestRatedItems();
for(int i: highestRItems){
if(i == 0) continue;
System.out.print(i+", ");
}
System.out.println("\n\n");
Document Page
}
// Read ratings from a file and create a RatingManager object that stores these ratings
public static RatingManager read(String fileName) {
RatingManager rm = new RatingManager();
Scanner sc;
try {
sc = new Scanner(new File(fileName));
String line = null;
while (sc.hasNextLine()) {
line = sc.nextLine().trim();
if (!line.equals("")) {
StringTokenizer toks = new StringTokenizer(line, "\t");
int userID = Integer.parseInt(toks.nextToken().trim());
int itemID = Integer.parseInt(toks.nextToken().trim());
int r = Integer.parseInt(toks.nextToken().trim());
addRating(new Rating(userID, itemID, r));
}
Document Page
}
System.out.println("File is successfully loaded!");
} catch (FileNotFoundException ex) {
System.out.println("File not found exception!!");
}
return rm;
}
// Add a rating
public static void addRating(Rating rating) {
ratingAr[++i] = rating;
}
// Return all ratings given to item j. Search should be efficient
public int[] getItemRatings(int j) {
int rating[] = new int[100000];
int index = -1;
for (int k = 0; k < ratingAr.length; k++) {
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Rating r = ratingAr[k];
if (r != null && r.getItemId() == j) {
rating[++index] = r.getValue();
}
}
return rating;
}
//// Return the average rating of item j. If i has no ratings, -1 is
public double getAverageItemRating(int j) {
int count = 0;
int sum = 0;
double avg = 0;
for (int k = 0; k < ratingAr.length; k++) {
Rating r = ratingAr[k];
if (r != null && r.getItemId() == j) {
count++;
sum += r.getValue();
Document Page
}
}
avg = sum / count;
if (avg != 0) {
return avg;
}
return -1;
}
//// Return the average rating given by user i. If i
public double getAverageUserRating(int i) {
int count = 0;
int sum = 0;
double avg = 0;
for (int k = 0; k < ratingAr.length; k++) {
Rating r = ratingAr[k];
if (r != null && r.getUserId() == i) {
count++;
sum += r.getValue();
}
}
Document Page
avg = sum / count;
if (avg != 0) {
return avg;
}
return -1;
}
public int[] getUserRatings(int i) {
int ratings[] = new int[100000];
int index = -1;
for (int k = 0; k < ratingAr.length; k++) {
Rating r = ratingAr[k];
if (r != null && r.getUserId() == i) {
ratings[++index] = r.getValue();
}
}
return ratings;
}
/* (for example if the highest average rating is 4.9,
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
* the method should return all items with average rating 4.9)
*/
//// Return the list of all items having the highest average rating
public int[] getHighestRatedItems() {
int items[] = new int[100000];
double ratingAvg[] = new double[100000];
int index = -1;
double max = 0;
for (int i = 0; i < ratingAr.length; i++) {
int count = 0;
int sum = 0;
double avg = 0;
int item = ratingAr[i].getItemId();
for (int k = 0; k < ratingAr.length; k++) {
Rating r = ratingAr[k];
if (r != null && r.getItemId() == item) {
count++;
Document Page
sum += r.getValue();
}
}
avg = sum / count;
if (i == 0) {
max = avg;
}
ratingAvg[++index] = avg;
items[index] = item;
max = Math.max(max, avg);
}
int HighestRatedItems[] = new int[100000];
index = - 1;
for (int k = 0; k < ratingAvg.length; k++) {
if (max == ratingAvg[k]) {
HighestRatedItems[++index] = items[k];
chevron_up_icon
1 out of 13
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]