-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMovieDataReader.java
More file actions
137 lines (128 loc) · 4.7 KB
/
Copy pathMovieDataReader.java
File metadata and controls
137 lines (128 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//--== CS400 File Header Information ==--
//Name: Shupeng Tang
//Email: stang72@wisc.edu
//Team: HG Blue
//Role: Data Wrangler
//TA: Hang
//Lecturer: Gary Dahl
//Notes to Grader: <optional extra notes>
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.zip.DataFormatException;
/**
* This class implements MovieDataReader Interface and Movie Interface, and enables
* the backend developer to get a list of movie objects contained in the csv file
* by passing in a reader object as input;
* @author shupengtang
*/
public class MovieDataReader implements MovieDataReaderInterface {
/**
* Method that reads movie data in CSV format from the Reader provided, and
* returns a list of movie objects;
*/
@Override
public List<MovieInterface> readDataSet(Reader inputFileReader) {
//creates a new scanner to read in data in reader
Scanner scan = new Scanner(inputFileReader);
//creates a Array list of movie objects
ArrayList<MovieInterface> movies = new ArrayList<MovieInterface>();
scan.nextLine();//ignore the first line of csv file
//scan in all the data and add per line as an object to movie list
while(scan.hasNext()) {
String line= scan.nextLine();
//split the strings on each line into a String array, with index 0 =
//the title of the movie, etc;
String [] splitted = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
//implement the movie interface for each movie object;
movies.add(new MovieInterface() {
/**
* This method gets the title of the movie object and returns it as a String
* @return title of the movie
*/
@Override
public String getTitle() {
String title = splitted[0];
return title;
}
/**
* This method gets the year of the movie object and returns it as a Integer
* @return year of the movie
*/
@Override
public Integer getYear() {
Integer year = Integer.valueOf(splitted[2]);
return year;
}
/**
* This method gets the genres of the movie, and returns them as a
* list of String objects;
* @return list of Strings that contains genres of the movie
*/
@Override
public List<String> getGenres() {
String genre = splitted[3];
//delete the "" mark that's included in some genres
if(genre.charAt(0)=='\"') genre = genre.substring(1);
if(genre.charAt(genre.length()-1)=='\"')genre = genre.substring(0, genre.length()-1);
List genres = Arrays.asList(genre.split(","));
return genres;
}
/**
* This method gets the director of the movie, and returns it as a String
* @return the director of the movie
*/
@Override
public String getDirector() {
String director = splitted[7];
return director;
}
/**
* This method gets the description of the movie, and returns it as a String
* @return the description of the movie
*/
@Override
public String getDescription() {
String description = splitted[11];
return description;
}
/**
* This method gets the rating of the movie, and returns it as a Float
* @return the description of the movie
*/
@Override
public Float getAvgVote() {
String temp = splitted[12];
//convert from String to Float
float vote = Float.parseFloat(temp);
return vote;
}
/**
* This method compares two movies and put them in order;
* @return int number that indicates the order of movies
*/
@Override
public int compareTo(MovieInterface otherMovie) {
if (this.getTitle().equals(otherMovie.getTitle())) {
return 0;
// sort by rating
} else if (this.getAvgVote() < otherMovie.getAvgVote()) {
return 0;
// sort by rating
} else if (this.getAvgVote() < otherMovie.getAvgVote()) {
return +1;
} else {
return -1;
}
}
});
}
//return the list of movies
return movies;
}
}