Skip to content

Commit b52511e

Browse files
authoredFeb 26, 2020
Add files via upload
1 parent b81e156 commit b52511e

25 files changed

+711
-0
lines changed
 
2.14 KB
Binary file not shown.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#BlueJ class context
2+
comment0.target=EfficientRater
3+
comment1.params=id
4+
comment1.target=EfficientRater(java.lang.String)
5+
comment2.params=item\ rating
6+
comment2.target=void\ addRating(java.lang.String,\ double)
7+
comment3.params=item
8+
comment3.target=boolean\ hasRating(java.lang.String)
9+
comment4.params=
10+
comment4.target=java.lang.String\ getID()
11+
comment5.params=item
12+
comment5.target=double\ getRating(java.lang.String)
13+
comment6.params=
14+
comment6.target=int\ numRatings()
15+
comment7.params=
16+
comment7.target=java.util.ArrayList\ getItemsRated()
17+
numComments=8
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/**
3+
* EfficientRater class contains various methods that are used to add and extract ratings
4+
* from the ratings HashMap.
5+
*
6+
* @author (your name)
7+
* @version (a version number or a date)
8+
*/
9+
10+
import java.util.*;
11+
12+
public class EfficientRater implements Rater {
13+
private String myID;
14+
private HashMap<String,Rating> myRatings;
15+
16+
public EfficientRater(String id) {
17+
myID = id;
18+
myRatings = new HashMap<String,Rating> ();
19+
}
20+
21+
public void addRating(String item, double rating) {
22+
myRatings.put(item, new Rating(item,rating));
23+
}
24+
25+
public boolean hasRating(String item) {
26+
if (myRatings.containsKey(item)) {
27+
return true;
28+
}
29+
30+
return false;
31+
}
32+
33+
public String getID() {
34+
return myID;
35+
}
36+
37+
public double getRating(String item) {
38+
for (String movieID : myRatings.keySet()) {
39+
if (movieID.equals(item)) {
40+
return myRatings.get(movieID).getValue();
41+
}
42+
}
43+
44+
return -1;
45+
}
46+
47+
public int numRatings() {
48+
return myRatings.size();
49+
}
50+
51+
public ArrayList<String> getItemsRated() {
52+
ArrayList<String> list = new ArrayList<String> ();
53+
for (String movieID : myRatings.keySet()) {
54+
list.add(myRatings.get(movieID).getItem());
55+
}
56+
57+
return list;
58+
}
59+
}
3.23 KB
Binary file not shown.

‎Week 3/Assignment 1/FirstRatings.ctxt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#BlueJ class context
2+
comment0.target=FirstRatings
3+
comment1.params=filename
4+
comment1.target=java.util.ArrayList\ loadMovies(java.lang.String)
5+
comment2.params=filename
6+
comment2.target=java.util.ArrayList\ loadRaters(java.lang.String)
7+
numComments=3

‎Week 3/Assignment 1/FirstRatings.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
/**
3+
* loadMovies and loadRaters methods add data from the related csv files into corresponding
4+
* ArrayLists.
5+
*
6+
* @ Konstantin Krumin
7+
* @ Version: 1.0 (February 14, 2020)
8+
*/
9+
10+
import edu.duke.*;
11+
import java.util.*;
12+
import org.apache.commons.csv.*;
13+
14+
public class FirstRatings {
15+
public ArrayList<Movie> loadMovies (String filename) {
16+
ArrayList<Movie> movieData = new ArrayList<Movie> ();
17+
18+
FileResource fr = new FileResource("data/" + filename + ".csv");
19+
CSVParser parser = fr.getCSVParser();
20+
21+
for (CSVRecord record: parser) {
22+
String currentID = record.get(0);
23+
String currentTitle = record.get(1);
24+
String currentYear = record.get(2);
25+
String currentCountry = record.get(3);
26+
String currentGenre = record.get(4);
27+
String currentDirector = record.get(5);
28+
int currentMinutes = Integer.parseInt(record.get(6));
29+
String currentPoster = record.get(7);
30+
31+
Movie currentMovie = new Movie(currentID, currentTitle, currentYear, currentGenre, currentDirector,
32+
currentCountry, currentPoster, currentMinutes);
33+
34+
movieData.add(currentMovie);
35+
}
36+
37+
return movieData;
38+
}
39+
40+
public ArrayList<Rater> loadRaters (String filename) {
41+
ArrayList<Rater> ratersData = new ArrayList<Rater> ();
42+
ArrayList<String> listOfIDs = new ArrayList<String> ();
43+
44+
FileResource fr = new FileResource("data/" + filename + ".csv");
45+
CSVParser parser = fr.getCSVParser();
46+
47+
for (CSVRecord record : parser) {
48+
String currentRaterID = record.get(0);
49+
String currentMovieID = record.get(1);
50+
double currentMovieRating = Double.parseDouble(record.get(2));
51+
52+
if (! listOfIDs.contains(currentRaterID)) {
53+
//Rater currentRater = new PlainRater(currentRaterID);
54+
Rater currentRater = new EfficientRater(currentRaterID);
55+
ratersData.add(currentRater);
56+
currentRater.addRating(currentMovieID, currentMovieRating);
57+
58+
} else {
59+
for (int k=0; k < ratersData.size(); k++) {
60+
if (ratersData.get(k).getID().equals(currentRaterID)) {
61+
ratersData.get(k).addRating(currentMovieID, currentMovieRating);
62+
}
63+
}
64+
}
65+
66+
listOfIDs.add(currentRaterID);
67+
}
68+
69+
return ratersData;
70+
}
71+
}

‎Week 3/Assignment 1/Movie.class

2.16 KB
Binary file not shown.

‎Week 3/Assignment 1/Movie.ctxt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#BlueJ class context
2+
comment0.target=Movie
3+
comment1.params=anID\ aTitle\ aYear\ theGenres
4+
comment1.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String)
5+
comment10.params=
6+
comment10.target=int\ getMinutes()
7+
comment11.params=
8+
comment11.target=java.lang.String\ toString()
9+
comment2.params=anID\ aTitle\ aYear\ theGenres\ aDirector\ aCountry\ aPoster\ theMinutes
10+
comment2.target=Movie(java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ java.lang.String,\ int)
11+
comment3.params=
12+
comment3.target=java.lang.String\ getID()
13+
comment4.params=
14+
comment4.target=java.lang.String\ getTitle()
15+
comment5.params=
16+
comment5.target=int\ getYear()
17+
comment6.params=
18+
comment6.target=java.lang.String\ getGenres()
19+
comment7.params=
20+
comment7.target=java.lang.String\ getCountry()
21+
comment8.params=
22+
comment8.target=java.lang.String\ getDirector()
23+
comment9.params=
24+
comment9.target=java.lang.String\ getPoster()
25+
numComments=12

‎Week 3/Assignment 1/Movie.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
4+
// An immutable passive data object (PDO) to represent item data
5+
public class Movie {
6+
private String id;
7+
private String title;
8+
private int year;
9+
private String genres;
10+
private String director;
11+
private String country;
12+
private String poster;
13+
private int minutes;
14+
15+
public Movie (String anID, String aTitle, String aYear, String theGenres) {
16+
// just in case data file contains extra whitespace
17+
id = anID.trim();
18+
title = aTitle.trim();
19+
year = Integer.parseInt(aYear.trim());
20+
genres = theGenres;
21+
}
22+
23+
public Movie (String anID, String aTitle, String aYear, String theGenres, String aDirector,
24+
String aCountry, String aPoster, int theMinutes) {
25+
// just in case data file contains extra whitespace
26+
id = anID.trim();
27+
title = aTitle.trim();
28+
year = Integer.parseInt(aYear.trim());
29+
genres = theGenres;
30+
director = aDirector;
31+
country = aCountry;
32+
poster = aPoster;
33+
minutes = theMinutes;
34+
}
35+
36+
// Returns ID associated with this item
37+
public String getID () {
38+
return id;
39+
}
40+
41+
// Returns title of this item
42+
public String getTitle () {
43+
return title;
44+
}
45+
46+
// Returns year in which this item was published
47+
public int getYear () {
48+
return year;
49+
}
50+
51+
// Returns genres associated with this item
52+
public String getGenres () {
53+
return genres;
54+
}
55+
56+
public String getCountry(){
57+
return country;
58+
}
59+
60+
public String getDirector(){
61+
return director;
62+
}
63+
64+
public String getPoster(){
65+
return poster;
66+
}
67+
68+
public int getMinutes(){
69+
return minutes;
70+
}
71+
72+
// Returns a string of the item's information
73+
public String toString () {
74+
String result = "Movie [id=" + id + ", title=" + title + ", year=" + year;
75+
result += ", genres= " + genres + "]";
76+
return result;
77+
}
78+
}
2.44 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#BlueJ class context
2+
comment0.target=MovieRunnerAverage
3+
comment1.params=
4+
comment1.target=void\ printAverageRatings()
5+
comment2.params=
6+
comment2.target=void\ getAverageRatingOneMovie()
7+
numComments=3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/**
3+
* MovieRunnerAverage class contains tests for the getAverageRatings method from the
4+
* secondRatings class.
5+
*
6+
* @ Konstantin Krumin
7+
* @ Version: 1.0 (February 18, 2020)
8+
*/
9+
10+
import java.util.*;
11+
12+
public class MovieRunnerAverage {
13+
public void printAverageRatings () {
14+
SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings");
15+
16+
System.out.println("Total number of movies : " + secondRatings.getMovieSize());
17+
System.out.println("Total number of raters : " + secondRatings.getRaterSize());
18+
19+
int MinNumOfRatings = 20; // variable
20+
ArrayList<Rating> averageRatings = secondRatings.getAverageRatings(MinNumOfRatings);
21+
Collections.sort(averageRatings);
22+
for (Rating rating : averageRatings) {
23+
System.out.println(rating.getValue() + " " + secondRatings.getTitle(rating.getItem()));
24+
}
25+
System.out.println("There are " + averageRatings.size() + " movies with " +
26+
MinNumOfRatings + " or more ratings");
27+
}
28+
29+
public void getAverageRatingOneMovie () {
30+
SecondRatings secondRatings = new SecondRatings ("ratedmoviesfull", "ratings");
31+
32+
String title = "Vacation"; // variable
33+
int MinNumOfRatings = 1; // variable
34+
35+
String movieID = secondRatings.getID(title);
36+
ArrayList<Rating> averageRatings = secondRatings.getAverageRatings(MinNumOfRatings);
37+
for (Rating rating : averageRatings) {
38+
if (rating.getItem().equals(movieID)) {
39+
System.out.println("For movie \"" + title + "\" the average rating is "
40+
+ rating.getValue());
41+
}
42+
}
43+
}
44+
}

‎Week 3/Assignment 1/PlainRater.class

1.79 KB
Binary file not shown.

‎Week 3/Assignment 1/PlainRater.ctxt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#BlueJ class context
2+
comment0.target=PlainRater
3+
comment1.params=id
4+
comment1.target=PlainRater(java.lang.String)
5+
comment2.params=item\ rating
6+
comment2.target=void\ addRating(java.lang.String,\ double)
7+
comment3.params=item
8+
comment3.target=boolean\ hasRating(java.lang.String)
9+
comment4.params=
10+
comment4.target=java.lang.String\ getID()
11+
comment5.params=item
12+
comment5.target=double\ getRating(java.lang.String)
13+
comment6.params=
14+
comment6.target=int\ numRatings()
15+
comment7.params=
16+
comment7.target=java.util.ArrayList\ getItemsRated()
17+
numComments=8

‎Week 3/Assignment 1/PlainRater.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* PlainRater class contains various methods that are used to add and extract ratings information.
4+
*
5+
*/
6+
7+
import java.util.*;
8+
9+
public class PlainRater implements Rater {
10+
private String myID;
11+
private ArrayList<Rating> myRatings;
12+
13+
public PlainRater(String id) {
14+
myID = id;
15+
myRatings = new ArrayList<Rating>();
16+
}
17+
18+
public void addRating(String item, double rating) {
19+
myRatings.add(new Rating(item,rating));
20+
}
21+
22+
public boolean hasRating(String item) {
23+
for(int k=0; k < myRatings.size(); k++){
24+
if (myRatings.get(k).getItem().equals(item)){
25+
return true;
26+
}
27+
}
28+
29+
return false;
30+
}
31+
32+
public String getID() {
33+
return myID;
34+
}
35+
36+
public double getRating(String item) {
37+
for(int k=0; k < myRatings.size(); k++){
38+
if (myRatings.get(k).getItem().equals(item)){
39+
return myRatings.get(k).getValue();
40+
}
41+
}
42+
43+
return -1;
44+
}
45+
46+
public int numRatings() {
47+
return myRatings.size();
48+
}
49+
50+
public ArrayList<String> getItemsRated() {
51+
ArrayList<String> list = new ArrayList<String>();
52+
for(int k=0; k < myRatings.size(); k++){
53+
list.add(myRatings.get(k).getItem());
54+
}
55+
56+
return list;
57+
}
58+
}

‎Week 3/Assignment 1/Rater.class

406 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)