|
|
|
@ -1,10 +1,12 @@ |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.FileWriter; |
|
|
|
import java.io.FileReader; |
|
|
|
import java.io.File; |
|
|
|
import java.util.Scanner; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.io.BufferedReader; |
|
|
|
|
|
|
|
public class Database { |
|
|
|
|
|
|
|
@ -37,31 +39,38 @@ public class Database { |
|
|
|
return found; |
|
|
|
} |
|
|
|
|
|
|
|
public static void addMovie(Movie m){ |
|
|
|
try { |
|
|
|
private static void writeMovieToFile(FileWriter fw, Movie m) throws IOException |
|
|
|
{ |
|
|
|
|
|
|
|
FileWriter fw = new FileWriter( "C:\\Users\\skc\\eclipse-workspace\\first\\Favorite_Movies\\src\\files\\data.txt",true); |
|
|
|
fw.write(m.getMovieId()); |
|
|
|
fw.write(m.getMovieId()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getTitle()); |
|
|
|
fw.append(" ").write(m.getTitle()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getYear()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getLength()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getGenre()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getStudioName()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getProducerName()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getShortDescription()); |
|
|
|
|
|
|
|
fw.append(" ").write(String.valueOf(m.getAvgRating())); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getNumWatched()); |
|
|
|
fw.append(" ").write(m.getYear()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getLength()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getGenre()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getStudioName()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getProducerName()); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getShortDescription()); |
|
|
|
|
|
|
|
fw.append(" ").write(String.valueOf(m.getAvgRating())); |
|
|
|
|
|
|
|
fw.append(" ").write(m.getNumWatched()); |
|
|
|
|
|
|
|
fw.write("\n"); |
|
|
|
} |
|
|
|
|
|
|
|
public static void addMovie(Movie m){ |
|
|
|
try { |
|
|
|
|
|
|
|
FileWriter fw = new FileWriter( "c:\\tmp\\src\\files\\data.txt",true); |
|
|
|
|
|
|
|
fw.write("\n"); |
|
|
|
writeMovieToFile(fw, m); |
|
|
|
|
|
|
|
fw.close(); |
|
|
|
|
|
|
|
@ -72,6 +81,62 @@ public class Database { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public static ArrayList<Movie> getMovies() |
|
|
|
{ |
|
|
|
ArrayList<Movie> list = new ArrayList<Movie>(); |
|
|
|
try{ |
|
|
|
FileReader fr = new FileReader( "C:\\tmp\\FavoriteMovies\\files\\data.txt"); |
|
|
|
BufferedReader br = new BufferedReader(fr); |
|
|
|
String line = null; |
|
|
|
while ((line=br.readLine())!=null) |
|
|
|
{ |
|
|
|
Movie m = Movie.parseString(line); |
|
|
|
list.add(m); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IOException e1) { // TODO Auto-generated catch block |
|
|
|
e1.printStackTrace(); |
|
|
|
} |
|
|
|
return list; |
|
|
|
} |
|
|
|
|
|
|
|
public static void saveMoviesList(ArrayList<Movie> list) |
|
|
|
{ |
|
|
|
try { |
|
|
|
|
|
|
|
FileWriter fw = new FileWriter( "C:\\tmp\\FavoriteMovies\\files\\data.txt"); |
|
|
|
|
|
|
|
for (Movie m: list) |
|
|
|
{ |
|
|
|
writeMovieToFile(fw, m); |
|
|
|
|
|
|
|
} |
|
|
|
fw.close(); |
|
|
|
} |
|
|
|
catch (IOException e1) { // TODO Auto-generated catch block |
|
|
|
e1.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static ArrayList<Star> getStars() |
|
|
|
{ |
|
|
|
ArrayList<Star> list = new ArrayList<Star>(); |
|
|
|
try{ |
|
|
|
FileReader fr = new FileReader( "c:\\tmp\\src\\files\\star_data.txt"); |
|
|
|
BufferedReader br = new BufferedReader(fr); |
|
|
|
String line = null; |
|
|
|
while ((line=br.readLine())!=null) |
|
|
|
{ |
|
|
|
Star s = Star.parseString(line); |
|
|
|
list.add(s); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IOException e1) { // TODO Auto-generated catch block |
|
|
|
e1.printStackTrace(); |
|
|
|
} |
|
|
|
return list; |
|
|
|
} |
|
|
|
|
|
|
|
public static void addStar(Star s) |
|
|
|
{ |
|
|
|
|
|
|
|
@ -85,18 +150,50 @@ public class Database { |
|
|
|
public static int findMovieId(String title) |
|
|
|
{ |
|
|
|
// TODO 6: search all titles and find matching movie id , return 0 if not found |
|
|
|
return 1; |
|
|
|
System.out.println("Finding for title: " + title); |
|
|
|
ArrayList<Movie> list = getMovies(); |
|
|
|
for (Movie movie : list) |
|
|
|
{ |
|
|
|
System.out.println("title: " + movie.getTitle()); |
|
|
|
if (movie.getTitle().equals(title)) |
|
|
|
return movie.getMovieId(); |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
public static void incNumWatched(int movieId) |
|
|
|
public static boolean incNumWatched(int movieId) |
|
|
|
{ |
|
|
|
// TODO 7: find movie by id and increment numWatched by 1 |
|
|
|
boolean found = false; |
|
|
|
ArrayList<Movie> list = getMovies(); |
|
|
|
for (Movie movie : list) |
|
|
|
{ |
|
|
|
if (movie.getMovieId() == movieId) |
|
|
|
{ |
|
|
|
movie.setNumWatched(movie.getNumWatched()+1); |
|
|
|
found = true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (found) |
|
|
|
saveMoviesList(list); |
|
|
|
return found; |
|
|
|
} |
|
|
|
|
|
|
|
public static Movie findTopMovieByGenre(String genre) |
|
|
|
{ |
|
|
|
Movie m = new Movie(); |
|
|
|
m.setAvgRating(0); |
|
|
|
// TODO 8: search all movies by genre, and find the one with the top rating and return it |
|
|
|
ArrayList<Movie> list = getMovies(); |
|
|
|
for (Movie movie : list) |
|
|
|
{ |
|
|
|
if (movie.getGenre().equals(genre)) |
|
|
|
{ |
|
|
|
if (movie.getAvgRating() > m.getAvgRating()) |
|
|
|
m = movie; |
|
|
|
} |
|
|
|
} |
|
|
|
return m; |
|
|
|
} |
|
|
|
|
|
|
|
@ -105,6 +202,11 @@ public class Database { |
|
|
|
|
|
|
|
ArrayList<Movie> list = new ArrayList<Movie>(); |
|
|
|
// TODO 9: search all movies in which the star has acted and return a list |
|
|
|
ArrayList<Movie> fullList = getMovies(); |
|
|
|
for (Movie movie : fullList) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
return list; |
|
|
|
} |
|
|
|
|