Creating a Java-Based Rating Management System for Items
VerifiedAdded 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.

Contents
Rating.java.......................................................................................................................................2
RatingManager.java.........................................................................................................................3
Rating.java.......................................................................................................................................2
RatingManager.java.........................................................................................................................3
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

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

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[];
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[];
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

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);
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);
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

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");
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");

}
// 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));
}
// 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));
}
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

}
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++) {
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++) {
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

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

}
}
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();
}
}
}
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();
}
}
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

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,
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,
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

* 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++;
*/
//// 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++;

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];
}
}
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];
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 13
Related Documents

Your All-in-One AI-Powered Toolkit for Academic Success.
 +13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.