You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.6 KiB

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 {
public static boolean userExists(String username)
{
boolean found = false;
try {
File filex=new File("c:\\tmp\\FavoriteMovies\\files\\login_cred.txt");
Scanner scan;
scan = new Scanner(filex);
scan.useDelimiter("[\n]");
if(scan.hasNext() && !found ) {
String line_ = scan.nextLine();
if(line_.equals(username))
found = true;
}
scan.close();
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return found;
}
private static void writeMovieToFile(FileWriter fw, Movie m) throws IOException
{
fw.write(String.valueOf(m.getMovieId()));
fw.append(" ").write(m.getTitle());
fw.append(" ").write(String.valueOf(m.getYear()));
fw.append(" ").write(String.valueOf(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(String.valueOf(m.getNumWatched()));
fw.write("\n");
}
public static void addMovie(Movie m){
try {
FileWriter fw = new FileWriter( "c:\\tmp\\src\\files\\data.txt",true);
writeMovieToFile(fw, m);
fw.close();
}
catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
}
}
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)
{
}
public static void addRating(Rating r)
{
}
public static int findMovieId(String title)
{
// TODO 6: search all titles and find matching movie id , return 0 if not found
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 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;
}
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<Movie> fullList = getMovies();
for (Movie movie : fullList)
{
}
return list;
}
}