|
|
|
@ -65,10 +65,22 @@ public class Database { |
|
|
|
fw.write("\n"); |
|
|
|
} |
|
|
|
|
|
|
|
public static void addMovie(Movie m){ |
|
|
|
public static int addMovie(Movie m){ |
|
|
|
int lastid = 0; |
|
|
|
try { |
|
|
|
|
|
|
|
FileWriter fw = new FileWriter( "c:\\tmp\\src\\files\\data.txt",true); |
|
|
|
ArrayList<Movie> list = getMovies(); |
|
|
|
for (Movie mv: list) |
|
|
|
{ |
|
|
|
if (mv.equals(m)) |
|
|
|
{ |
|
|
|
return 0; |
|
|
|
} |
|
|
|
if (mv.getMovieId() > lastid) |
|
|
|
lastid = mv.getMovieId(); |
|
|
|
} |
|
|
|
m.setMovieId(lastid + 1); |
|
|
|
FileWriter fw = new FileWriter( "C:\\tmp\\FavoriteMovies\\files\\data.txt",true); |
|
|
|
|
|
|
|
writeMovieToFile(fw, m); |
|
|
|
|
|
|
|
@ -78,6 +90,7 @@ public class Database { |
|
|
|
catch (IOException e1) { // TODO Auto-generated catch block |
|
|
|
e1.printStackTrace(); |
|
|
|
} |
|
|
|
return lastid+1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@ -93,6 +106,9 @@ public class Database { |
|
|
|
Movie m = Movie.parseString(line); |
|
|
|
list.add(m); |
|
|
|
} |
|
|
|
br.close(); |
|
|
|
fr.close(); |
|
|
|
|
|
|
|
} |
|
|
|
catch (IOException e1) { // TODO Auto-generated catch block |
|
|
|
e1.printStackTrace(); |
|
|
|
@ -122,7 +138,7 @@ public class Database { |
|
|
|
{ |
|
|
|
ArrayList<Star> list = new ArrayList<Star>(); |
|
|
|
try{ |
|
|
|
FileReader fr = new FileReader( "c:\\tmp\\src\\files\\star_data.txt"); |
|
|
|
FileReader fr = new FileReader( "C:\\tmp\\FavoriteMovies\\files\\star_data.txt"); |
|
|
|
BufferedReader br = new BufferedReader(fr); |
|
|
|
String line = null; |
|
|
|
while ((line=br.readLine())!=null) |
|
|
|
@ -130,6 +146,8 @@ public class Database { |
|
|
|
Star s = Star.parseString(line); |
|
|
|
list.add(s); |
|
|
|
} |
|
|
|
br.close(); |
|
|
|
fr.close(); |
|
|
|
} |
|
|
|
catch (IOException e1) { // TODO Auto-generated catch block |
|
|
|
e1.printStackTrace(); |
|
|
|
@ -137,14 +155,93 @@ public class Database { |
|
|
|
return list; |
|
|
|
} |
|
|
|
|
|
|
|
public static void addStar(Star s) |
|
|
|
public static ArrayList<Rating> getRatings() |
|
|
|
{ |
|
|
|
ArrayList<Rating> list = new ArrayList<Rating>(); |
|
|
|
try{ |
|
|
|
FileReader fr = new FileReader( "C:\\tmp\\FavoriteMovies\\files\\ratings_data.txt"); |
|
|
|
BufferedReader br = new BufferedReader(fr); |
|
|
|
String line = null; |
|
|
|
while ((line=br.readLine())!=null) |
|
|
|
{ |
|
|
|
Rating s = Rating.parseString(line); |
|
|
|
list.add(s); |
|
|
|
} |
|
|
|
br.close(); |
|
|
|
fr.close(); |
|
|
|
} |
|
|
|
catch (IOException e1) { // TODO Auto-generated catch block |
|
|
|
e1.printStackTrace(); |
|
|
|
} |
|
|
|
return list; |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean addStar(Star s) |
|
|
|
{ |
|
|
|
boolean movieFound = false; |
|
|
|
try |
|
|
|
{ |
|
|
|
|
|
|
|
ArrayList<Movie> list = getMovies(); |
|
|
|
for (Movie m: list) |
|
|
|
{ |
|
|
|
if (m.getMovieId() == s.getMovieId()) |
|
|
|
{ |
|
|
|
movieFound = true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (movieFound) |
|
|
|
{ |
|
|
|
FileWriter fw = new FileWriter( "C:\\tmp\\FavoriteMovies\\files\\star_data.txt",true); |
|
|
|
fw.write(String.valueOf(s.getMovieId())); |
|
|
|
fw.append(" ").write(s.getMovieTitle()); |
|
|
|
fw.append(" ").write(String.valueOf(s.getYear())); |
|
|
|
fw.append(" ").write(s.getNameSurname()); |
|
|
|
fw.append(" ").write("\n"); |
|
|
|
fw.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IOException e1) { // TODO Auto-generated catch block |
|
|
|
e1.printStackTrace(); |
|
|
|
movieFound = false; |
|
|
|
} |
|
|
|
return movieFound; |
|
|
|
} |
|
|
|
|
|
|
|
public static void addRating(Rating r) |
|
|
|
public static boolean addRating(Rating r) |
|
|
|
{ |
|
|
|
|
|
|
|
boolean movieFound = false; |
|
|
|
try |
|
|
|
{ |
|
|
|
|
|
|
|
ArrayList<Movie> list = getMovies(); |
|
|
|
for (Movie m: list) |
|
|
|
{ |
|
|
|
if (m.getMovieId() == r.getMovieId()) |
|
|
|
{ |
|
|
|
movieFound = true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (movieFound) |
|
|
|
{ |
|
|
|
//(movieId: int, NameSurname: string, relation: string, rating: int) |
|
|
|
FileWriter fw = new FileWriter( "C:\\tmp\\FavoriteMovies\\files\\ratings_data.txt",true); |
|
|
|
fw.write(String.valueOf(r.getMovieId())); |
|
|
|
fw.append(" ").write(r.getNameSurname()); |
|
|
|
fw.append(" ").write(r.getRelation()); |
|
|
|
fw.append(" ").write(String.valueOf(r.getRating())); |
|
|
|
fw.append(" ").write("\n"); |
|
|
|
fw.close(); |
|
|
|
UpdateRating(r.getMovieId(), r.getRating()); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IOException e1) { // TODO Auto-generated catch block |
|
|
|
e1.printStackTrace(); |
|
|
|
movieFound = false; |
|
|
|
} |
|
|
|
return movieFound; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public static int findMovieId(String title) |
|
|
|
@ -202,14 +299,58 @@ public class Database { |
|
|
|
public static ArrayList<Movie> findMoviesForStar(String starName) |
|
|
|
{ |
|
|
|
|
|
|
|
ArrayList<Movie> list = new ArrayList<Movie>(); |
|
|
|
// TODO 9: search all movies in which the star has acted and return a list |
|
|
|
ArrayList<Integer> movieIdList = new ArrayList<Integer>(); |
|
|
|
|
|
|
|
ArrayList<Star> starList = getStars(); |
|
|
|
for (Star s: starList) |
|
|
|
{ |
|
|
|
if (s.getNameSurname().equals(starName)) |
|
|
|
movieIdList.add(s.getMovieId()); |
|
|
|
} |
|
|
|
ArrayList<Movie> fullList = getMovies(); |
|
|
|
for (Movie movie : fullList) |
|
|
|
ArrayList<Movie> list = new ArrayList<Movie>(); |
|
|
|
for(Movie m: fullList) |
|
|
|
{ |
|
|
|
|
|
|
|
if (movieIdList.contains(m.getMovieId())) |
|
|
|
{ |
|
|
|
System.out.println("Adding " + m.getTitle() + " to list"); |
|
|
|
list.add(m); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return list; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void UpdateRating(int movieId, int newRating) |
|
|
|
{ |
|
|
|
// get all ratings |
|
|
|
ArrayList<Rating> ratings = getRatings(); |
|
|
|
// calculate avg rating |
|
|
|
int totalRating = 0; |
|
|
|
int numRatings = 0; |
|
|
|
for (Rating r: ratings) |
|
|
|
{ |
|
|
|
if (r.getMovieId() == movieId) |
|
|
|
{ |
|
|
|
totalRating += r.getRating(); |
|
|
|
numRatings++; |
|
|
|
} |
|
|
|
} |
|
|
|
// find movie by id |
|
|
|
ArrayList<Movie> list = getMovies(); |
|
|
|
// update movie |
|
|
|
for (Movie m: list) |
|
|
|
{ |
|
|
|
if (m.getMovieId() == movieId) |
|
|
|
{ |
|
|
|
m.setAvgRating(totalRating/numRatings); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
// save movies to file |
|
|
|
saveMoviesList(list); |
|
|
|
} |
|
|
|
|
|
|
|
} |